You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/examples/renderer_rules.md
+15-15Lines changed: 15 additions & 15 deletions
Original file line number
Diff line number
Diff line change
@@ -2,14 +2,14 @@
2
2
## Default renderer rules
3
3
Rules on how to translate markdown content to HTML elements are stored in `renderer.rules`:
4
4
5
-
```
5
+
```js
6
6
constMarkdownIt=require('markdown-it');
7
7
constmd=newMarkdownIt();
8
8
9
9
console.log(Object.keys(md.renderer.rules))
10
10
```
11
11
Output:
12
-
```
12
+
```js
13
13
[
14
14
'code_inline',
15
15
'code_block',
@@ -32,7 +32,7 @@ Let's use a Hello World example:
32
32
[Link to Demo](https://markdown-it.github.io/#md3=%7B%22source%22%3A%22-%20Hello%20World%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%3Afalse%2C%22typographer%22%3Afalse%2C%22_highlight%22%3Afalse%2C%22_strict%22%3Afalse%2C%22_view%22%3A%22debug%22%7D%7D)
33
33
34
34
Now take a closer look at the first element in the resulting list:
35
-
```
35
+
```js
36
36
{
37
37
"type":"bullet_list_open",
38
38
"tag":"ul",
@@ -67,7 +67,7 @@ Create a rule to add the CSS class "lorem_ipsum" to every <ul>
67
67
```
68
68
69
69
Rules are functions that accept a number of parameters:
70
-
```
70
+
```js
71
71
constMarkdownIt=require('markdown-it');
72
72
constmd=newMarkdownIt();
73
73
@@ -85,7 +85,7 @@ We assign the new rule to the key that corresponds to the html tag we want to mo
85
85
86
86
It is good practice however to save the default renderer for your element and only make minimal chances to the rules in place, instead of reinventing the wheel:
87
87
88
-
```
88
+
```js
89
89
constMarkdownIt=require('markdown-it');
90
90
constmd=newMarkdownIt();
91
91
@@ -104,7 +104,7 @@ CSS classes are attributes on HTML elements. If we think back to the object repr
104
104
105
105
Looking at [the API documention for Token objects](https://markdown-it.github.io/markdown-it/#Token.attrJoin) we find the `attrJoin` method. This method allows us to join an existing attributes value with a new value or create the attribute if it doens't exist yet. Simply pushing the value (for example with `token.attr.push(["key", "value"]`) would overwrite any previous change:
Let's imagine we are using CSS pseudo classes such as `:before` and `:after` to style our list because using `list-style-type` doesn't provide the bullet types we want and `list-style-image` isn't flexible enough to position itself properly across all major browsers.
146
146
147
147
To keep a proper line wrapping in our list we have set all elements in our `li` to display as a block (`li * {display: block;}`). This works for our pseudo classes and other `HTMLElements`. However, it does not work for `TextNodes`. So having this output will produce weird line indents:
148
-
```
148
+
```html
149
149
<ul>
150
150
<li>Hello World</li>
151
151
<ul>
152
152
```
153
153
154
154
To fix this we can use a wrapper element which can be properly displayed as a block:
0 commit comments