Open alifyasa opened 7 months ago
The wiki example contains incorrect regex patterns, leading to unexpected results.
Let's say we are trying to convert mdText with the following default classes:
mdText
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.
<pre>
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>.
<p class="text-sm" re>
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.
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:If we use the code in the wiki example, the
<pre>
tag would be incorrectly replaced.Notice that
<pre>
is replaced by<p class="text-sm" re>
.Suggestion
I suggest using this regex pattern:
This new regex pattern ensures proper replacement without affecting other HTML tags.