Skip to content
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

Open
andersk opened this issue Mar 18, 2023 · 3 comments
Open

Option to skip adding <pre><code> to highlighted code #256

andersk opened this issue Mar 18, 2023 · 3 comments
Labels
enhancement New feature or request

Comments

@andersk
Copy link

andersk commented Mar 18, 2023

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:

if highlighted.startswith("<pre"):

But that heuristic fails for pygments.highlight, whose output does not begin with <pre:

>>> pygments.highlight('print("hello")', pygments.lexers.get_lexer_by_name("python"), pygments.formatters.HtmlFormatter())
'<div class="highlight"><pre><span></span><span class="nb">print</span><span class="p">(</span><span class="s2">&quot;hello&quot;</span><span class="p">)</span>\n</pre></div>\n'

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

@andersk andersk added the enhancement New feature or request label Mar 18, 2023
@welcome
Copy link

welcome bot commented Mar 18, 2023

Thanks for opening your first issue here! Engagement like this is essential for open source projects! 🤗

If you haven't done so already, check out EBP's Code of Conduct. Also, please try to follow the issue template as it helps other community members to contribute more effectively.

If your issue is a feature request, others may react to it, to raise its prominence (see Feature Voting).

Welcome to the EBP community! 🎉

@pydsigner
Copy link

An alternative could be to subclass RendererHTML and override fence():

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: markdown_it.MarkdownIt(renderer_cls=CustomRendererHTML)

@ZeroAurora
Copy link

Upvoting this for my meeting this problem. Also, "highlight" is something that is almost undocumented. Maybe it needs more attention.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants