Skip to content

Commit

Permalink
Markdown-it-py plugin to add language-none class code fences without …
Browse files Browse the repository at this point in the history
…a language. Always load Prismjs styles. Consequent changes in JS flow and tests.
  • Loading branch information
miteshashar committed Dec 13, 2022
1 parent c60b1e2 commit d63c59b
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 5 deletions.
5 changes: 3 additions & 2 deletions funnel/assets/js/utils/prism.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ const PrismEmbed = {
];
let asset = 0;
const loadPrismStyle = () => {
$('head').append($(`<link href="${CDN_CSS}" rel="stylesheet"></link>`));
if (!$(`link[href*="${CDN_CSS}"]`).length)
$('head').append($(`<link href="${CDN_CSS}" rel="stylesheet"></link>`));
};
const loadPrismScript = () => {
$.ajax({
Expand All @@ -41,8 +42,8 @@ const PrismEmbed = {
}
});
};
loadPrismStyle();
if (!window.Prism) {
loadPrismStyle();
loadPrismScript();
} else this.activatePrism();
},
Expand Down
7 changes: 6 additions & 1 deletion funnel/utils/markdown/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
from .mdit_plugins import ( # toc_plugin,
del_plugin,
embeds_plugin,
fence_extend_plugin,
ins_plugin,
mark_plugin,
sub_plugin,
Expand Down Expand Up @@ -170,11 +171,14 @@ def render(self, text: Optional[str]) -> Optional[Markup]:
MarkdownPlugin('markmap', embeds_plugin, {'name': 'markmap'})
MarkdownPlugin('vega-lite', embeds_plugin, {'name': 'vega-lite'})
MarkdownPlugin('mermaid', embeds_plugin, {'name': 'mermaid'})
MarkdownPlugin('fence_ext', fence_extend_plugin)
# MarkdownPlugin('toc', toc_plugin)

# --- Markdown configurations ----------------------------------------------------------

MarkdownConfig(name='basic', options_update={'html': False, 'breaks': True})
MarkdownConfig(
name='basic', options_update={'html': False, 'breaks': True}, plugins={'fence_ext'}
)
MarkdownConfig(
name='document',
preset='gfm-like',
Expand All @@ -196,6 +200,7 @@ def render(self, text: Optional[str]) -> Optional[Markup]:
'markmap',
'vega-lite',
'mermaid',
'fence_ext',
# 'toc',
},
enable_rules={'smartquotes'},
Expand Down
1 change: 1 addition & 0 deletions funnel/utils/markdown/mdit_plugins/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

from .del_tag import *
from .embeds import *
from .fence_ext import *
from .ins_tag import *
from .mark_tag import *
from .sub_tag import *
Expand Down
13 changes: 13 additions & 0 deletions funnel/utils/markdown/mdit_plugins/fence_ext.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
"""Markdown-it-py plugin to add language-none class to code fence tokens."""

from markdown_it import MarkdownIt
from markdown_it.renderer import RendererHTML


def fence_extend_plugin(md: MarkdownIt, **opts) -> None:
def fence(self, tokens, idx, options, env):
output = RendererHTML.fence(self, tokens, idx, options, env)
output = output.replace('<pre><code>', '<pre><code class="language-none">')
return output

md.add_render_rule('fence', fence)
4 changes: 2 additions & 2 deletions tests/unit/utils/markdown/data/code.toml
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ line 2 of code
line 3 of code
</code></pre>
<h3>Block code &quot;fences&quot;</h3>
<pre><code>Sample text here...
<pre><code class="language-none">Sample text here...
It is a sample text that has multiple lines
</code></pre>
<h3>Syntax highlighting</h3>
Expand Down Expand Up @@ -120,7 +120,7 @@ line 2 of code
line 3 of code
</code></pre>
<h3 id="h:block-code-fences">Block code “fences” <a class="header-anchor" href="#h:block-code-fences">¶</a></h3>
<pre><code>Sample text here...
<pre><code class="language-none">Sample text here...
It is a sample text that has multiple lines
</code></pre>
<h3 id="h:syntax-highlighting">Syntax highlighting <a class="header-anchor" href="#h:syntax-highlighting">¶</a></h3>
Expand Down

0 comments on commit d63c59b

Please sign in to comment.