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

Default classes for each HTML element improvement #509

Closed
Kameelridder opened this issue Mar 26, 2018 · 3 comments
Closed

Default classes for each HTML element improvement #509

Kameelridder opened this issue Mar 26, 2018 · 3 comments
Assignees

Comments

@Kameelridder
Copy link

Kameelridder commented Mar 26, 2018

DESCRIPTION:

The example to add default classes to the output as discussed on the wiki pages (https://github.com/showdownjs/showdown/wiki/Add-default-classes-for-each-HTML-element) uses a regular expression that will need you to set noHeaderId option to false. If this option is set to false, you are no longer able to use reference tags like a href="#titlename" to access the title from a table of contents.

CURRENT EXAMPLE CODE:

const bindings = Object.keys(classMap)
  .map(key => ({
    type: 'output',
    regex: new RegExp(`<${key}>`, 'g'),
    replace: `<${key} class="${classMap[key]}">`
  }));
const conv = new showdown.Converter({
  extensions: [...bindings],
  noHeaderId: true // important to add this, else regex match doesn't work
});

SOLUTION PROPOSAL:

If the regular expression is updated to also match other attributes in the tag (.*). The replace string can be updated to print those attributes to the output using $1.

regex: new RegExp('<${key}(.*)>', 'g'),
replace: '<${key} class="${classMap[key]}" $1>

If you change this, the noHeaderId option can be set to false again, allowing tables of contents to work again.

@tivie tivie self-assigned this Mar 26, 2018
@tivie
Copy link
Member

tivie commented Mar 26, 2018

Thank you. Will update as soon as possible.

@tivie
Copy link
Member

tivie commented Sep 15, 2018

@tivie tivie closed this as completed Sep 15, 2018
@juliandavidmr
Copy link

using replace: '<${key} class="${classMap[key]}" $1> you get an incorrect output, because $1 refers to the first group of the regular expression, which turns out to be the name of the html tag.

This is resolved by changing from $1 to $2, that is: <${key} class="${classMap[key]}" $2>

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

No branches or pull requests

3 participants