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

fix: Add newline before pre codeblock start #462

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

jtanningbed
Copy link

Sometimes there is additional text before the 'pre' code block begins :
image

This results in a malformed generated markdown:
image

This just adds a newline before the start of the code block, should be fairly inconsequential.

New result:
image

There's another issue with handling whitespaces that are defined like: <span class="w"> </span>, seems like the parser is sending the entire content to handle_data if the code block is something like

<code>
    "python"
    <span class="w"> </span>
    "main.py"
</code>

the html will be parsed with handle_data("pythonmain.py"), so the whitespace is not preserved. That seems like it would require updating the underlying HTMLParser logic or overriding it so I didn't try to address it myself. Just wanted to raise it in case it wasn't known.
example:
image

@dmurat
Copy link

dmurat commented Jan 24, 2025

I would like to propose a more robust solution:

if tag == 'pre':
    if start:
        # Always start a new code block with a new line. Otherwise, it may not be rendered correctly.
        if not self.lastWasNL:
            self.o('\n')
        self.o('```\n')  # Markdown code block start
        self.inside_pre = True
    else:
        # Avoid adding unnecessary new lines at the end of the code block.
        if not self.lastWasNL:
            self.o('\n')
        self.o('```\n')  # Markdown code block end
        self.inside_pre = False

@unclecode unclecode added 🐞 Bug Something isn't working 💪 - Intermediate Difficulty level - Intermediate ⚡ High Priority - High labels Jan 25, 2025
@unclecode
Copy link
Owner

@jtanningbed @dmurat Thanks for sharing. This seems important to look. I will check it very soon.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
💪 - Intermediate Difficulty level - Intermediate 🐞 Bug Something isn't working ⚡ High Priority - High
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants