3.2.4 添加锚点

This commit is contained in:
跨越晨昏 2024-10-28 19:43:16 +08:00
parent d6f061c434
commit 2824832906
3 changed files with 24 additions and 28 deletions

View File

@ -29,6 +29,7 @@ from pymdownx.smartsymbols import SmartSymbolsExtension
from pymdownx.superfences import fence_div_format from pymdownx.superfences import fence_div_format
from pymdownx.tasklist import TasklistExtension from pymdownx.tasklist import TasklistExtension
from pymdownx.tilde import DeleteSubExtension from pymdownx.tilde import DeleteSubExtension
from pymdownx.magiclink import MagiclinkExtension
from .Define import Variable from .Define import Variable
@ -173,7 +174,10 @@ class Syllabus(BlockProcessor):
return False return False
class _Anchor(InlineProcessor): class Anchor(InlineProcessor):
"""
{#定义锚点}
"""
def handleMatch(self, m: Match[str], data: str) -> (Tuple[xml.etree.ElementTree.Element, int, int] | def handleMatch(self, m: Match[str], data: str) -> (Tuple[xml.etree.ElementTree.Element, int, int] |
Tuple[None, None, None]): Tuple[None, None, None]):
""" """
@ -190,6 +194,9 @@ class _Anchor(InlineProcessor):
class LinkLine(InlineProcessor): class LinkLine(InlineProcessor):
"""
{行内链接}
"""
def handleMatch(self, m: Match[str], data: str) -> (Tuple[xml.etree.ElementTree.Element, int, int] | def handleMatch(self, m: Match[str], data: str) -> (Tuple[xml.etree.ElementTree.Element, int, int] |
Tuple[None, None, None]): Tuple[None, None, None]):
""" """
@ -225,7 +232,7 @@ class Pre(Extension):
md.preprocessors.register(PreProcess(self.variable), 'pre_process', 1000) md.preprocessors.register(PreProcess(self.variable), 'pre_process', 1000)
class Basic(Extension): class BasicExtension(Extension):
""" """
渲染基本样式 渲染基本样式
""" """
@ -245,36 +252,17 @@ class Basic(Extension):
md.parser.blockprocessors.register(Syllabus(md.parser), 'syllabus', 182) # 渲染提纲 md.parser.blockprocessors.register(Syllabus(md.parser), 'syllabus', 182) # 渲染提纲
class Anchor(Extension): class AnchorExtension(Extension):
def extendMarkdown(self, md: Markdown): def extendMarkdown(self, md: Markdown):
""" """
添加扩展 添加扩展
:param md: 转换器 :param md: 转换器
""" """
md.registerExtension(self) # 注册扩展 md.registerExtension(self) # 注册扩展
md.inlinePatterns.register(_Anchor(r'\{{#([^{}#]+)}}'), 'anchor', 0) # 定义锚点 md.inlinePatterns.register(Anchor(r'\{{#([^{}#]+)}}'), 'anchor', 0) # 定义锚点
md.inlinePatterns.register(LinkLine(r'\{{([^{}#]+)}}'), 'line_link', 0) # 添加页内链接 md.inlinePatterns.register(LinkLine(r'\{{([^{}#]+)}}'), 'line_link', 0) # 添加页内链接
class Code(Extension):
def __init__(self, variable: Variable):
"""
初始化
:param variable: 变量字典
"""
super().__init__()
self.variable = variable
def extendMarkdown(self, md: Markdown):
"""
添加扩展
:param md: 转换器
"""
md.registerExtension(self) # 注册扩展
# md.treeprocessors.register(CodeLine(variable=self.variable), 'code_line', 0) # 渲染单行代码块
# md.treeprocessors.register(CodeBlock(), 'code_block', 1000) # 渲染多行代码块
Extensions = { Extensions = {
# 自带 # 自带
'元数据': meta.MetaExtension(), '元数据': meta.MetaExtension(),
@ -313,9 +301,11 @@ Extensions = {
'上标': InsertSupExtension(), '上标': InsertSupExtension(),
'高级列表': FancyListExtension(), '高级列表': FancyListExtension(),
'高级标题': SaneHeadersExtension(), '高级标题': SaneHeadersExtension(),
'超级链接': MagiclinkExtension(),
# 自定义 # 自定义
'基本风格': Basic(), '基本风格': BasicExtension(),
'锚点': AnchorExtension(),
} }

View File

@ -164,6 +164,8 @@
<p><a href="链接地址">链接文本</a></p> <p><a href="链接地址">链接文本</a></p>
<p><a href="https://crossdark.com">CrossDark</a></p> <p><a href="https://crossdark.com">CrossDark</a></p>
<p><a href="https://crossdark.net/">https://crossdark.net/</a></p> <p><a href="https://crossdark.net/">https://crossdark.net/</a></p>
<p><a href="https://crossdark.net/">https://crossdark.net/</a></p>
<p><a href="&#109;&#97;&#105;&#108;&#116;&#111;&#58;&#108;&#105;&#117;&#104;&#97;&#110;&#98;&#111;&#51;&#51;&#51;&#64;&#105;&#99;&#108;&#111;&#117;&#100;&#46;&#99;&#111;&#109;">&#108;&#105;&#117;&#104;&#97;&#110;&#98;&#111;&#51;&#51;&#51;&#64;&#105;&#99;&#108;&#111;&#117;&#100;&#46;&#99;&#111;&#109;</a></p>
<h3 id="1.3.2">1.3.2 图片</h3> <h3 id="1.3.2">1.3.2 图片</h3>
<p><img alt="链接图片" src="链接地址" /></p> <p><img alt="链接图片" src="链接地址" /></p>
<p><img alt="sea" src="https://crossdark.com/wp-content/uploads/2024/05/1715259682-sea.jpg" /></p> <p><img alt="sea" src="https://crossdark.com/wp-content/uploads/2024/05/1715259682-sea.jpg" /></p>
@ -175,9 +177,9 @@
<p>直接在文本中使用 <abbr title="长的文本">缩写</abbr> 即可</p> <p>直接在文本中使用 <abbr title="长的文本">缩写</abbr> 即可</p>
<h1 id="3">3 锚点</h1> <h1 id="3">3 锚点</h1>
<h2 id="3.1">3.1 定义</h2> <h2 id="3.1">3.1 定义</h2>
<p>{{#锚点名}}</p> <p><span id="锚点名">锚点名</span></p>
<h2 id="3.2">3.2 页内链接</h2> <h2 id="3.2">3.2 页内链接</h2>
<p>{{锚点名}}</p> <p><a href="#锚点名">锚点名</a></p>
<h1 id="4">4 代码块</h1> <h1 id="4">4 代码块</h1>
<h2 id="4.1">4.1 <code>单行</code></h2> <h2 id="4.1">4.1 <code>单行</code></h2>
<p>Here is some code: <code class="highlight"><span class="kn">import</span> <span class="nn">pymdownx</span><span class="p">;</span> <span class="n">pymdownx</span><span class="o">.</span><span class="n">__version__</span></code>.</p> <p>Here is some code: <code class="highlight"><span class="kn">import</span> <span class="nn">pymdownx</span><span class="p">;</span> <span class="n">pymdownx</span><span class="o">.</span><span class="n">__version__</span></code>.</p>
@ -304,7 +306,7 @@
<p>7.1.3. 错误示范</p> <p>7.1.3. 错误示范</p>
<p>不能出现两个及以上连续的点,例如:</p> <p>不能出现两个及以上连续的点,例如:</p>
<p>7..1...4 错误示范</p> <p>7..1...4 错误示范</p>
<p>提纲号会被自动配置为锚点,可直接使用{{7}}{{7.1}}</p> <p>提纲号会被自动配置为锚点,可直接使用<a href="#7">7</a><a href="#7.1">7.1</a></p>
<h1 id="8">8 注释</h1> <h1 id="8">8 注释</h1>
<!-- 这是注释 --> <!-- 这是注释 -->
@ -444,7 +446,7 @@
<hr /> <hr />
<ol> <ol>
<li id="fn:脚注"> <li id="fn:脚注">
<p>一段长的文本用于说明&#160;<a class="footnote-backref" href="#fnref:脚注" title="Jump back to footnote 1 in the text">link</a></p> <p>一段长的文本用于说明&#160;<a class="footnote-backref" href="#fnref:脚注" title="Jump back to footnote 1 in the text">&#8617;</a></p>
</li> </li>
</ol> </ol>
</div> </div>

View File

@ -59,6 +59,10 @@ ___
<https://crossdark.net/> <https://crossdark.net/>
https://crossdark.net/
liuhanbo333@icloud.com
1.3.2 图片 1.3.2 图片
![链接图片](链接地址) ![链接图片](链接地址)