1.6.1锚点开始头疼

This commit is contained in:
跨越晨昏 2024-10-06 22:22:53 +08:00
parent 235d922883
commit abb07507ae
3 changed files with 82 additions and 54 deletions

View File

@ -20,7 +20,7 @@ Extensions = {
"Tables": "markdown.extensions.tables", "Tables": "markdown.extensions.tables",
# "Smart Strong": "markdown.extensions.smart_strong", # "Smart Strong": "markdown.extensions.smart_strong",
"Admonition": "markdown.extensions.admonition", "Admonition": "markdown.extensions.admonition",
"CodeHilite": "markdown.extensions.codehilite", # "CodeHilite": "markdown.extensions.codehilite",
# "HeaderId": "markdown.extensions.headerid", # "HeaderId": "markdown.extensions.headerid",
"Meta-Data": "markdown.extensions.meta", "Meta-Data": "markdown.extensions.meta",
"New Line to Break": "markdown.extensions.nl2br", "New Line to Break": "markdown.extensions.nl2br",
@ -42,6 +42,7 @@ class Simple(InlineProcessor):
""" """
可通过简单的正则表达式和HTML标签实现的样式 可通过简单的正则表达式和HTML标签实现的样式
""" """
def __init__(self, pattern: str, tag: str): def __init__(self, pattern: str, tag: str):
""" """
初始化 初始化
@ -62,6 +63,7 @@ class Nest(InlineProcessor):
""" """
需要嵌套HTML标签实现的样式 需要嵌套HTML标签实现的样式
""" """
def __init__(self, pattern: str, outer_tag: str, inner_tag: str): def __init__(self, pattern: str, outer_tag: str, inner_tag: str):
""" """
初始化 初始化
@ -86,21 +88,24 @@ class ID(InlineProcessor):
""" """
需要对HTML标签设置ID实现的样式 需要对HTML标签设置ID实现的样式
""" """
def __init__(self, pattern: str, tag: str, property_: str):
def __init__(self, pattern: str, tag: str, property_: str, value: Union[str, bool, int] = None):
""" """
初始化 初始化
:param pattern: 正则表达式 :param pattern: 正则表达式
:param tag: html标签 :param tag: html标签
:param property_: html标签属性名称 :param property_: html标签属性名称
:param value: html标签属性的值
""" """
super().__init__(pattern) super().__init__(pattern)
self.tag = tag self.tag = tag
self.property = property_ self.property = property_
self.value = value
def handleMatch(self, match, match_line): def handleMatch(self, match, match_line):
tag = xml.etree.ElementTree.Element(self.tag) # 创建标签 tag = xml.etree.ElementTree.Element(self.tag) # 创建标签
tag.text = match.group(1) # 设置标签内容 tag.text = match.group(1) # 设置标签内容
tag.set(self.property, match.group(2)) # 设置标签属性 tag.set(self.property, match.group(2) if self.value is None else self.value) # 设置标签属性,属性的值默认为第二个匹配组
return tag, match.start(), match.end() return tag, match.start(), match.end()
@ -218,15 +223,31 @@ class Box(Extension):
def extendMarkdown(self, md): def extendMarkdown(self, md):
md.registerExtension(self) # 注册扩展 md.registerExtension(self) # 注册扩展
# 红框警告 # 红框警告
md.inlinePatterns.register(ID(
r'!{3,}(.+?)!{3,}', tag='div', property_='style', value='display: inline-block; border: 1px solid red;'
), 'warning_in_line', 0
) # 行内
md.parser.blockprocessors.register(BoxBlock( md.parser.blockprocessors.register(BoxBlock(
md.parser, r'^ *!{3,} *\n', r'\n *!{3,}\s*$', 'display: inline-block; border: 1px solid red;' md.parser, r'^ *!{3,} *\n', r'\n *!{3,}\s*$', 'display: inline-block; border: 1px solid red;'
), 'warning_box', 175) # 块 ), 'warning_box', 175) # 块
# 黄框提醒 # 黄框提醒
md.inlinePatterns.register(ID(
r'!{2,}(.+?)!{2,}', tag='div', property_='style', value='display: inline-block; border: 1px solid yellow;'
), 'reminding_in_line', 0
) # 行内
md.parser.blockprocessors.register(BoxBlock( md.parser.blockprocessors.register(BoxBlock(
md.parser, r'^ *!{2,} *\n', r'\n *!{2,}\s*$', 'display: inline-block; border: 1px solid yellow;' md.parser, r'^ *!{2,} *\n', r'\n *!{2,}\s*$', 'display: inline-block; border: 1px solid yellow;'
), 'reminding_box', 175) ), 'reminding_box', 175) # 块
class Anchor(Extension):
def extendMarkdown(self, md):
md.registerExtension(self) # 注册扩展
md.inlinePatterns.register(ID(
r'\{#([^{}#]+)}', tag='span', property_='id'), 'hide', 0
) # 定义锚点
def main(text: str) -> Tuple[str, Dict[str, List[str]]]: def main(text: str) -> Tuple[str, Dict[str, List[str]]]:
md = Markdown(extensions=[Basic(), Box()] + list(Extensions.values()), safe_mode=False) md = Markdown(extensions=[Basic(), Box(), Anchor()] + list(Extensions.values()), safe_mode=False)
return md.convert(text), md.Meta return md.convert(text), md.Meta

View File

@ -175,28 +175,25 @@
<p><code>{强调文本}</code></p> <p><code>{强调文本}</code></p>
<h2 id="4.2">4.2 多行</h2> <h2 id="4.2">4.2 多行</h2>
<h3 id="4.2.1">4.2.1 YAML</h3> <h3 id="4.2.1">4.2.1 YAML</h3>
<div class="codehilite"><pre><span></span><code><span class="nt">A</span><span class="p">:</span> <pre><code class="language-yaml">A:
<span class="w"> </span><span class="l l-Scalar l-Scalar-Plain">1. a</span> 1. a
<span class="w"> </span><span class="l l-Scalar l-Scalar-Plain">2. b</span> 2. b
<span class="w"> </span><span class="l l-Scalar l-Scalar-Plain">3. c</span> 3. c
<span class="nt">B</span><span class="p">:</span> B:
<span class="w"> </span><span class="p p-Indicator">-</span><span class="w"> </span><span class="l l-Scalar l-Scalar-Plain">a</span> - a
<span class="w"> </span><span class="p p-Indicator">-</span><span class="w"> </span><span class="l l-Scalar l-Scalar-Plain">b</span> - b
<span class="w"> </span><span class="p p-Indicator">-</span><span class="w"> </span><span class="l l-Scalar l-Scalar-Plain">c</span> - c
</code></pre></div> </code></pre>
<h3 id="4.2.2">4.2.2 Python</h3> <h3 id="4.2.2">4.2.2 Python</h3>
<div class="codehilite"><pre><span></span><code><span class="nb">print</span><span class="p">(</span><span class="s1">&#39;CrossDown&#39;</span><span class="p">)</span> <pre><code class="language-python">print('CrossDown')
</code></pre></div> </code></pre>
<h3 id="4.2.3">4.2.3 Mermaid</h3> <h3 id="4.2.3">4.2.3 Mermaid</h3>
<div class="codehilite"><pre><span></span><code><span class="nf">graph</span><span class="w"> </span><span class="n">LR</span> <pre><code class="language-mermaid">graph LR
<span class="w"> </span><span class="n">A</span><span class="o">--&gt;</span><span class="n">B</span> A--&gt;B
<span class="w"> </span><span class="n">A</span><span class="o">--&gt;</span><span class="n">C</span> A--&gt;C
<span class="w"> </span><span class="n">B</span><span class="o">--&gt;</span><span class="n">D</span> B--&gt;D
<span class="w"> </span><span class="n">C</span><span class="o">--&gt;</span><span class="n">D</span> C--&gt;D
</code></pre></div> </code></pre>
<h1 id="5">5 转义</h1> <h1 id="5">5 转义</h1>
<p>\ </p> <p>\ </p>
<p>\a </p> <p>\a </p>
@ -219,7 +216,7 @@
</blockquote> </blockquote>
</blockquote> </blockquote>
</blockquote> </blockquote>
<p>引文内添加klzzwxhklzzwxhklzzwxh:00420039klzzwxh:0040</p> <p>引文内添加klzzwxhklzzwxhklzzwxh:00440041klzzwxh:0042</p>
</blockquote> </blockquote>
<h1 id="7">7 提纲</h1> <h1 id="7">7 提纲</h1>
<h2 id="7.1">7.1 提纲号</h2> <h2 id="7.1">7.1 提纲号</h2>
@ -248,6 +245,12 @@
<li>d</li> <li>d</li>
</ol> </ol>
<h2 id="9.2">9.2 无序列表</h2> <h2 id="9.2">9.2 无序列表</h2>
<ul>
<li>A</li>
<li>B</li>
<li>C</li>
<li>D</li>
</ul>
<h1 id="10">10 表格</h1> <h1 id="10">10 表格</h1>
<table> <table>
<thead> <thead>
@ -295,11 +298,12 @@
<p>否则所有定义将被集中在文章末尾</p> <p>否则所有定义将被集中在文章末尾</p>
<h1 id="14">14 外框</h1> <h1 id="14">14 外框</h1>
<h2 id="14.1">14.1 警告</h2> <h2 id="14.1">14.1 警告</h2>
<p>!!!警告!!!</p> <p>这是一个<div style="display: inline-block; border: 1px solid red;">警告</div>……</p>
<div style="display: inline-block; border: 1px solid red;"> <div style="display: inline-block; border: 1px solid red;">
<p>这是一条警告</p> <p>这是一条警告</p>
</div> </div>
<h2 id="14.2">14.2 提醒</h2> <h2 id="14.2">14.2 提醒</h2>
<p>这是一个<div style="display: inline-block; border: 1px solid yellow;">提醒</div>……</p>
<div style="display: inline-block; border: 1px solid yellow;"> <div style="display: inline-block; border: 1px solid yellow;">
<p>这是一条提醒</p> <p>这是一条提醒</p>
</div> </div>

View File

@ -201,6 +201,7 @@ graph LR
4. d 4. d
9.2 无序列表 9.2 无序列表
- A - A
- B - B
- C - C
@ -246,7 +247,7 @@ graph LR
14.1 警告 14.1 警告
!!!警告!!! 这是一个!!!警告!!!……
!!! !!!
这是一条警告 这是一条警告
@ -254,6 +255,8 @@ graph LR
14.2 提醒 14.2 提醒
这是一个!!提醒!!……
!! !!
这是一条提醒 这是一条提醒
!! !!