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

Incorrect Regex in "Add default classes for each HTML element" Wiki Page #1005

Open
alifyasa opened this issue Apr 15, 2024 · 0 comments
Open

Comments

@alifyasa
Copy link

Description

The wiki example contains incorrect regex patterns, leading to unexpected results.

Example

Let's say we are trying to convert mdText with the following default classes:

const mdText = `
This is a paragraph

\`\`\`
This is a code block.
\`\`\`
`
const classMap = {
    p: 'text-sm',
}

If we use the code in the wiki example, the <pre> tag would be incorrectly replaced.

const oldBindings = Object.keys(classMap)
    .map(key => ({
        type: 'output',
        regex: new RegExp(`<${key}(.*)>`, 'g'),
        replace: `<${key} class="${classMap[key]}" $1>`
    }));
const oldConv = new Showdown.Converter({
    extensions: [...oldBindings]
});
console.log(oldConv.makeHtml(mdText))
// Output is:
// <p class="text-sm" >This is a paragraph</p>
// <p class="text-sm" re><code>This is a code block.
// </code></pre>

Notice that <pre> is replaced by <p class="text-sm" re>.

Suggestion

I suggest using this regex pattern:

const newBindings = Object.keys(classMap)
    .map(key => ({
        type: 'output',
        regex: new RegExp(`(?:<${key}>)|(?:<${key} (.*)>)`, 'g'),
        replace: `<${key} class="${classMap[key]}" $1>`
    }));
const newConv = new Showdown.Converter({
    extensions: [...newBindings]
})
console.log(newConv.makeHtml(mdText))
// Output is:
// <p class="text-sm" >This is a paragraph</p>
// <pre><code>This is a code block.
// </code></pre>

This new regex pattern ensures proper replacement without affecting other HTML tags.

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

1 participant