forked from CrossDark/CrossDown
1.6.3锚点搞定
This commit is contained in:
parent
0fef493d04
commit
dfbf9371a5
@ -175,6 +175,24 @@ class BoxBlock(BlockProcessor):
|
||||
return False # equivalent to our test() routine returning False
|
||||
|
||||
|
||||
class _Anchor(InlineProcessor):
|
||||
def handleMatch(self, match, match_line):
|
||||
tag = xml.etree.ElementTree.Element('span') # 创建标签
|
||||
tag.text = match.group(1)
|
||||
tag.set('id', match.group(1)) # 设置id
|
||||
|
||||
return tag, match.start(), match.end()
|
||||
|
||||
|
||||
class LinkLine(InlineProcessor):
|
||||
def handleMatch(self, match, match_line):
|
||||
tag = xml.etree.ElementTree.Element('a') # 创建标签
|
||||
tag.set('href', '#' + match.group(1)) # 设置id
|
||||
tag.text = match.group(1)
|
||||
|
||||
return tag, match.start(), match.end()
|
||||
|
||||
|
||||
class Basic(Extension):
|
||||
"""
|
||||
渲染基本样式
|
||||
@ -223,14 +241,10 @@ class Box(Extension):
|
||||
class Anchor(Extension):
|
||||
def extendMarkdown(self, md):
|
||||
md.registerExtension(self) # 注册扩展
|
||||
md.inlinePatterns.register(ID(
|
||||
r'\{#([^{}#]+)}', tag='span', property_='id', value=1), 'hide', 0
|
||||
) # 定义锚点
|
||||
md.inlinePatterns.register(ID(
|
||||
r'\{([^{}#]+)}', tag='span', property_='id', value=1), 'hide', 0
|
||||
) # 定义锚点
|
||||
md.inlinePatterns.register(_Anchor(r'\{#([^{}#]+)}'), 'anchor', 0) # 定义锚点
|
||||
md.inlinePatterns.register(LinkLine(r'\{([^{}#]+)}'), 'line_link', 0) # 添加页内链接
|
||||
|
||||
|
||||
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()))
|
||||
return md.convert(text), md.Meta
|
||||
|
17
README.html
17
README.html
@ -72,7 +72,11 @@
|
||||
<li><a href="#2.2">2.2 赋值</a></li>
|
||||
</ul>
|
||||
</li>
|
||||
<li><a href="#3">3 锚点</a></li>
|
||||
<li><a href="#3">3 锚点</a><ul>
|
||||
<li><a href="#3.1">3.1 定义</a></li>
|
||||
<li><a href="#3.2">3.2 页内链接</a></li>
|
||||
</ul>
|
||||
</li>
|
||||
<li><a href="#4">4 代码块</a><ul>
|
||||
<li><a href="#4.1">4.1 单行</a><ul>
|
||||
<li><a href="#4.1.1">4.1.1 LaTex</a></li>
|
||||
@ -161,7 +165,10 @@
|
||||
<h2 id="2.2">2.2 赋值</h2>
|
||||
<p>直接在文本中使用 <abbr title="长的文本">缩写</abbr> 即可</p>
|
||||
<h1 id="3">3 锚点</h1>
|
||||
<p>{#锚点名}</p>
|
||||
<h2 id="3.1">3.1 定义</h2>
|
||||
<p><span id="锚点名">锚点名</span></p>
|
||||
<h2 id="3.2">3.2 页内链接</h2>
|
||||
<p><a href="#锚点名">锚点名</a></p>
|
||||
<h1 id="4">4 代码块</h1>
|
||||
<h2 id="4.1">4.1 <code>单行</code></h2>
|
||||
<h3 id="4.1.1">4.1.1 LaTex</h3>
|
||||
@ -216,7 +223,7 @@
|
||||
</blockquote>
|
||||
</blockquote>
|
||||
</blockquote>
|
||||
<p>引文内添加klzzwxhklzzwxhklzzwxh:00440041klzzwxh:0042</p>
|
||||
<p>引文内添加klzzwxhklzzwxhklzzwxh:00470044klzzwxh:0045</p>
|
||||
</blockquote>
|
||||
<h1 id="7">7 提纲</h1>
|
||||
<h2 id="7.1">7.1 提纲号</h2>
|
||||
@ -227,10 +234,10 @@
|
||||
<p>7.1.3. 错误示范</p>
|
||||
<p>不能出现两个及以上连续的点,例如:</p>
|
||||
<p>7..1…4 错误示范</p>
|
||||
<p>提纲号会被自动配置为锚点,可直接使用{7}76.1}</p>
|
||||
<p>提纲号会被自动配置为锚点,可直接使用<a href="#7">7</a>76.1}</p>
|
||||
<h1 id="8">8 注释</h1>
|
||||
<h2 id="8.1">8.1 强注释</h2>
|
||||
<p>|=klzzwxhklzzwxhklzzwxh:00310026klzzwxh:0029=|</p>
|
||||
<p>|=klzzwxhklzzwxhklzzwxh:00340029klzzwxh:0032=|</p>
|
||||
<h2 id="8.2">8.2 弱注释</h2>
|
||||
<!-- 这是注释 -->
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user