3.4.1 emm自定义InlineHilite扩展

This commit is contained in:
跨越晨昏 2024-11-19 19:27:42 +08:00
parent dd01c2b57e
commit b4d4fff952
2 changed files with 16 additions and 11 deletions

View File

@ -275,15 +275,20 @@ class CodeExtension(Extension):
md.treeprocessors.register(CodeLine(variable=self.variable), 'code_line', 100) md.treeprocessors.register(CodeLine(variable=self.variable), 'code_line', 100)
def variable_formatter(source, language, css_class, md): def inline_formatter(source, language, css_class, md): # 自定义的单行代码格式化器
if language != '': if language != '': # 调用默认格式化函数
return InlineHilitePattern.highlight_code(src=source, language=language, classname=css_class, md=md) return md.inlinePatterns['backtick'].highlight_code(src=source, language=language, classname=css_class, md=md)
match tuple(source): match tuple(source):
case '{', '#', *archers, '}': # 匹配到{#锚点} case '{', '#', *archers, '}': # 匹配到{#锚点}
archer = str(archers.items()) archer = ''.join(archers)
return f'<span id="{archer}">{archer}</span>' return f'<span id="{archer}">{archer}</span>'
case '{', '-', *inline_links, '}': # 匹配到{-行内链接}
inline_link = ''.join(inline_links)
return f'<a href=#{inline_link}>{inline_link}</a>'
case '{', *variable, '}': # 匹配到{变量}
return ''.join(variable)
case _: case _:
return f'<code>{source}</code>' # Or string return f'<code>{source}</code>'
Extensions = { Extensions = {
@ -319,7 +324,7 @@ Extensions = {
{ {
'name': '*', 'name': '*',
'class': 'block', 'class': 'block',
'format': variable_formatter, 'format': inline_formatter,
}, },
] ]
), ),
@ -341,7 +346,7 @@ Extensions = {
# 自定义 # 自定义
'基本风格': BasicExtension(), '基本风格': BasicExtension(),
'锚点': AnchorExtension(), # '锚点': AnchorExtension(),
} }

View File

@ -187,12 +187,12 @@
<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>`<span id="锚点">锚点</span>`</p> <p><span id="锚点">锚点</span></p>
<h2 id="3.2">3.2 链接</h2> <h2 id="3.2">3.2 链接</h2>
<p><code>{-锚点}</code></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: `#!py3 import pymdownx; pymdownx.<strong>version</strong>`.</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>
<p>The mock shebang will be treated like text here: <code>#!js var test = 0;</code>.</p> <p>The mock shebang will be treated like text here: <code>#!js var test = 0;</code>.</p>
<h3 id="4.1.1">4.1.1 LaTex</h3> <h3 id="4.1.1">4.1.1 LaTex</h3>
<p>这是<span class="arithmatex"><span class="MathJax_Preview">CO_2</span><script type="math/tex">CO_2</script></span>二氧化碳,或者可以写成这样CO<sub>2</sub></p> <p>这是<span class="arithmatex"><span class="MathJax_Preview">CO_2</span><script type="math/tex">CO_2</script></span>二氧化碳,或者可以写成这样CO<sub>2</sub></p>
@ -314,7 +314,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>提纲号会被自动配置为锚点,可直接使用<a href="#7">7</a><a href="#7.1">7.1</a></p> <p>提纲号会被自动配置为锚点,可直接使用{-7}{-7.1}</p>
<h1 id="8">8 注释</h1> <h1 id="8">8 注释</h1>
<!-- 这是注释 --> <!-- 这是注释 -->