-
Notifications
You must be signed in to change notification settings - Fork 71
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Option to skip adding <pre><code>
to highlighted code
#256
Comments
Thanks for opening your first issue here! Engagement like this is essential for open source projects! 🤗 |
An alternative could be to subclass class CustomRendererHTML(RendererHTML):
def fence(self, tokens: Sequence[Token], idx: int, options: OptionsDict, env: EnvType) -> str:
token = tokens[idx]
info = unescapeAll(token.info).strip() if token.info else ''
langName = info.split(maxsplit=1)[0] if info else ''
if options.highlight:
return options.highlight(
token.content, langName, ''
) or escapeHtml(token.content)
return escapeHtml(token.content) Then this class can be selected to get the desired behavior: |
Upvoting this for my meeting this problem. Also, "highlight" is something that is almost undocumented. Maybe it needs more attention. |
Just implemented this, it seems ok: pygments_style = get_style_by_name('catppuccin-mocha')
def highlight_func(code: str, lang: str, _) -> str | None:
"""Highlight function using pygments."""
if not lang:
return None
lexer = get_lexer_by_name(lang)
formatter = HtmlFormatter(style=pygments_style, noclasses=True, nowrap=True)
return highlight(code, lexer, formatter)
md = MarkdownIt('js-default', {'highlight': highlight_func}).enable('table')
|
@dimitrilarue No, that’s the opposite of what I need. The extra classes from Pygments such as |
Context
When using the
highlight
option to provide a custom syntax highlighter, markdown-it-py wraps the HTML output of the highlighter in<pre><code>
unless it already starts with<pre
:markdown-it-py/markdown_it/renderer.py
Line 270 in 73a0147
But that heuristic fails for
pygments.highlight
, whose output does not begin with<pre
:So markdown-it-py turns this into
<pre><code><div class="highlight"><pre>…</pre></div></code></pre>
, and existing CSS themes for Pygments need to be rewritten to account for the unnecessarily duplicated<pre>
.Proposal
Can we have an option to skip adding
<pre><code>
that’s not subject to the heuristic?Tasks and updates
No response
The text was updated successfully, but these errors were encountered: