Skip to content

Commit e157cd2

Browse files
authored
doc: Add syntax highlighting
For better readability
1 parent 6ec0b76 commit e157cd2

File tree

1 file changed

+15
-15
lines changed

1 file changed

+15
-15
lines changed

docs/examples/renderer_rules.md

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22
## Default renderer rules
33
Rules on how to translate markdown content to HTML elements are stored in `renderer.rules`:
44

5-
```
5+
```js
66
const MarkdownIt = require('markdown-it');
77
const md = new MarkdownIt();
88

99
console.log(Object.keys(md.renderer.rules))
1010
```
1111
Output:
12-
```
12+
```js
1313
[
1414
'code_inline',
1515
'code_block',
@@ -32,7 +32,7 @@ Let's use a Hello World example:
3232
[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)
3333

3434
Now take a closer look at the first element in the resulting list:
35-
```
35+
```js
3636
{
3737
"type": "bullet_list_open",
3838
"tag": "ul",
@@ -67,7 +67,7 @@ Create a rule to add the CSS class "lorem_ipsum" to every <ul>
6767
```
6868

6969
Rules are functions that accept a number of parameters:
70-
```
70+
```js
7171
const MarkdownIt = require('markdown-it');
7272
const md = new MarkdownIt();
7373

@@ -85,7 +85,7 @@ We assign the new rule to the key that corresponds to the html tag we want to mo
8585

8686
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:
8787

88-
```
88+
```js
8989
const MarkdownIt = require('markdown-it');
9090
const md = new MarkdownIt();
9191

@@ -104,7 +104,7 @@ CSS classes are attributes on HTML elements. If we think back to the object repr
104104

105105
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:
106106

107-
```
107+
```js
108108
const MarkdownIt = require('markdown-it');
109109
const md = new MarkdownIt();
110110

@@ -119,7 +119,7 @@ md.renderer.rules.bullet_list_open = function(tokens, idx, options, env, self) {
119119
};
120120
```
121121
Let's test the finished rule:
122-
```
122+
```js
123123
const MarkdownIt = require('markdown-it');
124124
const md = new MarkdownIt();
125125

@@ -136,7 +136,7 @@ md.renderer.rules.bullet_list_open = function(tokens, idx, options, env, self) {
136136
console.log(md.render("- Hello World"));
137137
```
138138
Output:
139-
```
139+
```html
140140
<ul class="lorem_ipsum">
141141
<li>Hello World</li>
142142
</ul>
@@ -145,15 +145,15 @@ Output:
145145
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.
146146

147147
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
149149
<ul>
150150
<li>Hello World</li>
151151
<ul>
152152
```
153153

154154
To fix this we can use a wrapper element which can be properly displayed as a block:
155155

156-
```
156+
```html
157157
<ul>
158158
<li>
159159
<span>Hello World</span>
@@ -177,7 +177,7 @@ list_item_close
177177

178178
Now use this information to add the new rules:
179179

180-
```
180+
```js
181181
const MarkdownIt = require('markdown-it');
182182
const md = new MarkdownIt();
183183

@@ -196,7 +196,7 @@ md.renderer.rules.list_item_close = function(tokens, idx, options, env, self) {
196196
```
197197
Testing our modification:
198198

199-
```
199+
```js
200200
const MarkdownIt = require('markdown-it');
201201
const md = new MarkdownIt();
202202

@@ -216,7 +216,7 @@ md.renderer.rules.list_item_close = function(tokens, idx, options, env, self) {
216216
console.log(md.render("- Hello World"));
217217
```
218218
Output:
219-
```
219+
```html
220220
<ul>
221221
<li>
222222
<span>Hello World</span>
@@ -225,7 +225,7 @@ Output:
225225
```
226226

227227
Of course using string manipulation might get really messy for bigger changes. So consider using `markdown-it`s Token class instead:
228-
```
228+
```js
229229
const MarkdownIt = require('markdown-it');
230230
const Token = require('markdown-it/lib/token');
231231
const md = new MarkdownIt();
@@ -253,7 +253,7 @@ console.log(md.render("- Hello World"));
253253

254254
Output:
255255

256-
```
256+
```html
257257
<ul>
258258
<li>
259259
<span>Hello World<span>

0 commit comments

Comments
 (0)