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

Code comments starting with "#" in <span> tags are appearing as h1 headers #470

Open
humanismusic opened this issue Jul 11, 2024 · 1 comment

Comments

@humanismusic
Copy link

humanismusic commented Jul 11, 2024

Hi,

I'm testing turndown on a page that includes sample python code with comments, example:

<span class="comment"># here's a comment</span>.

There are no pre or code tags used in the HTML. Each time I run turndown, it results in this line being output as is and displaying like an H1:

here's a comment

Here is the rule I'm using to attempt to excude these:

// handle potential code comments or lines starting with "#" in <span> tags
    turndownService.addRule('spanWithHash', {
        filter: (node) => {
            const hasHash = node.nodeName === 'SPAN' && node.textContent.trim().startsWith('#');
            if (hasHash) {
                console.log("Found span filter #: ", node.textContent.trim()); // log for debugging
            }
            return hasHash;
        },
        replacement: (content, node) => {
            return content.trim().substring(1);
        }
    });

I also tried escaping etc before deciding to try remove the # all together. Despite this, the text within these tags is still being treated as headers in the resulting markdown.

Could you help identify why this rule isn't working as intended?

@martincizek
Copy link
Collaborator

I'm testing turndown on a page that includes sample python code with comments, example:

<span class="comment"># here's a comment</span>.

There are no pre or code tags used in the HTML. Every time it results in h1 output like so:

here's a comment

Can't reproduce it, can you please make up a complete input that reproduce the problem?
image

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

No branches or pull requests

2 participants