# markdown-it-attr A [markdown-it](https://github.com/markdown-it/markdown-it) plugin to write id, classes, and attributes. ## Syntax `{#id .class1 .class2 attr=val attr1='val"s"' attr2="val's" attr3}` ### Where to put `{...}` |type|where|example| |:-:|:-:|--| |inline tag|**AFTER** tag|em / strong| |inline block|beginning / end|li / td / th| |block|**BEFORE** block|h1 / ul / table| Note: There is no way to add attributes to `tr` without extension. See [Extension: Attributes for tr](#tr-extension) for more info. ## Examples ### Attributes for inline tag Add `{...}` **AFTER** the inline tag. #### em / strong Example Input: ```markdown *x*{.a} **y**{.b} ***z***{.c} ``` Output: ```html

x y z

``` ### Attributes for inline block Add `{...}` at the **beginning** or the **end** in the inline block. #### list item Example Input: ```markdown - {.a} x1 - x2 {.b} - x3 ``` Output: ```html ``` #### th / td Example Input: ```markdown |h1{.a}|h2{.b}| |------|------| |d1{.c}|d2{.d}| ``` Output: ```html
h1 h2
d1 d2
``` ### Attributes for block Add `{...}` **BEFORE** the block. #### header Example Input: ```markdown {.a} # h1 ``` Output: ```html

h1

``` #### list Example Input: ```markdown {.b} - l1 - l2 ``` Output: ```html ``` #### table Example Input: ```markdown {.c} |h1|h2| |--|--| |d1|d2| ``` Output: ```html
h1 h2
d1 d2
``` ## Usage ```js const md = require('markdown-it')(); const mia = require('@sup39/markdown-it-attr'); console.log(md.use(mia).render(` {#head-id} # head `)); ``` Expected output: ```html

head

```

Extension: Attributes for tr

To make adding attributes to `tr` work, it is required to use [@sup39/markdown-it-raw-table](https://github.com/sup39/markdown-it-raw-table) plugin, in order to prevent forcing td count to be equal to th count, which eliminates the attributes of `tr`. ```js const md = require('markdown-it')(); const mia = require('@sup39/markdown-it-attr'); const mrt = require('@sup39/markdown-it-raw-table'); // enable raw_table_tr rule mia.inlineAttrsApplyRules.find(e=>e.name==='raw_table_tr').disabled = false; console.log(md.use(mia).use(mrt).render(` | h1 | h2 | h3 {.ch} | | -- | -- | -- | | x1 | x2 {.c2} | x3 {rowspan=2} | {.r1} | x4 {colspan=2 .c4} | {.r2} `)); ``` Expected output: ```html
h1 h2 h3
x1 x2 x3
x4
```
h1 h2 h3
x1 x2 x3
x4
Note that adding attributes to `tr` of the `th` row is NOT available.