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

typeset plugin reads headings in code blocks #5446

Closed
4 tasks done
pawamoy opened this issue Apr 30, 2023 · 5 comments
Closed
4 tasks done

typeset plugin reads headings in code blocks #5446

pawamoy opened this issue Apr 30, 2023 · 5 comments
Labels
bug Issue reports a bug resolved Issue is resolved, yet unreleased if open

Comments

@pawamoy
Copy link
Sponsor Contributor

pawamoy commented Apr 30, 2023

Context

I'm writing some docs for mkdocstrings where we show how you can inject documentation for an object directly in a heading line:

## ::: path.to.object

Bug description

The typeset plugin naively picks up Markdown lines starting with # without checking if they're part of a fenced code block. It then converts those lines using the extension configured in mkdocs.yml, in this case mkdocstrings, which triggers all the code scanning + HTML rendering machinery, causing issues.

Related links

https://github.com/squidfunk/mkdocs-material-insiders/blob/d82bb8e0a5ef39bed97e6a8849b8146dfd7afbb1/material/plugins/typeset/plugin.py#L74-L77

Reproduction

example.zip

Steps to reproduce

cd repro-typeset
python -m venv .venv
. .venv/bin/activate
python -m pip install mkdocs-material mkdocstrings[python]
mkdocs build

Browser

No response

Before submitting

@squidfunk squidfunk added the bug Issue reports a bug label May 1, 2023
@pawamoy
Copy link
Sponsor Contributor Author

pawamoy commented May 12, 2023

Here's a suggestion (probably naive as well):

headings = []
in_code_block = False
for line in page.markdown.split("\n"):
    if line.startswith("```"):
        if in_code_block:
            end_ticks = len(line) - len(line.lstrip("`"))
            if end_ticks == start_ticks:
                in_code_block = False
        else:
            start_ticks = len(line) - len(line.lstrip("`"))
            in_code_block = True
    elif line.startswith("#") and not in_code_block:
        headings.append(line)

html = self.markdown.convert("\n".join(headings))

@squidfunk
Copy link
Owner

Thanks for reporting and providing a possible solution! The reported problem should be fixed in 86264095f. I've switched the current approach that does Markdown parsing to use two regexes to remove the anchor links from the headlines (which would otherwise result in nested anchors = invalid HTML). Can you confirm that the issue is gone?

@squidfunk squidfunk added the resolved Issue is resolved, yet unreleased if open label May 12, 2023
@pawamoy
Copy link
Sponsor Contributor Author

pawamoy commented May 12, 2023

Ah, that's great! Some extensions/plugins generate headings and these would not have been caught by the previous version of the code. Working on HTML allows to pick up every headings, static or generated 👍 I'll try the fix and report back 🙂 Thanks!

@squidfunk
Copy link
Owner

The issue is resolved, as discussed on Gitter with @pawamoy.

@squidfunk
Copy link
Owner

Released as part of 9.1.12-insiders-4.34.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Issue reports a bug resolved Issue is resolved, yet unreleased if open
Projects
None yet
Development

No branches or pull requests

2 participants