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

[Tutorial] Bug in example code 'Add default class for each HTML element' - adding classes only to first element #1026

Open
wykopx opened this issue Jan 20, 2025 · 0 comments

Comments

@wykopx
Copy link

wykopx commented Jan 20, 2025

On the tutorial page:

https://showdownjs.com/docs/tutorials/add-default-class-to-html/

The code works only for first occurence of each tag in the classMap.

const classMap = {
  a: 'exampleClass'
}

So if you have let's say two different list items:

<a> example 1 </a>
<a> example 2 </a>

it will add classes only to the first one because of the buggy RegExp

<a class="exampleClass"> example 1 </a>
<a> example 2 </a>

Image

There is an issue with the code. The line

regex: new RegExp(`<${key}(.*)>`, 'g'),

should be replaced with corrected RegExp version:

regex: new RegExp(`(?:<${key}>)|(?:<${key} (.*?)>)`, 'g'),

Code that adds classes in all elements in markdown text:

const bindings = Object.keys(classMap)
  .map(key => ({
  	type: 'output',
	regex: new RegExp(`(?:<${key}>)|(?:<${key} (.*?)>)`, 'g'),
  	replace: `<${key} class="${classMap[key]}" $1>`
  }));
@wykopx wykopx changed the title [Tutorial] Bug in example code 'Add default class for each HTML element' [Tutorial] Bug in example code 'Add default class for each HTML element' - adding classes only to first element Jan 20, 2025
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