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
Markdoc nodes enable you to customize how your document renders without using any custom syntax—it consists entirely of Markdown. Customizing nodes lets you extend your implementation incrementally.
9
9
10
-
## Customizing Markdoc nodes
11
-
12
-
Nodes are elements that Markdoc inherits from Markdown, specifically the [CommonMark specification](https://commonmark.org/).
13
-
14
-
You define custom nodes by passing a custom Node to your [`Config`](/docs/syntax#config), like:
if (attributes.id&&typeofattributes.id==='string') {
42
-
returnattributes.id;
43
-
}
44
-
return children
45
-
.filter((child) =>typeof child ==='string')
46
-
.join('')
47
-
.replace(/[?]/g, '')
48
-
.replace(/\s+/g, '-')
49
-
.toLowerCase();
50
-
}
51
-
52
-
exportconstheading= {
53
-
children: ['inline'],
54
-
attributes: {
55
-
id: { type:String },
56
-
level: { type:Number, required:true, default:1 }
57
-
},
58
-
transform(node, config) {
59
-
constattributes=node.transformAttributes(config);
60
-
constchildren=node.transformChildren(config);
61
-
62
-
constid=generateID(children, attributes);
63
-
64
-
returnnewTag(
65
-
`h${node.attributes['level']}`,
66
-
{ ...attributes, id },
67
-
children
68
-
);
69
-
}
70
-
};
71
-
```
72
-
73
-
After registering this custom node, you can then use it in your Markdoc, like:
74
-
75
-
{% side-by-side %}
76
-
77
-
{% markdoc-example %}
78
-
79
-
```md
80
-
#### My header
81
-
```
82
-
83
-
{% /markdoc-example %}
84
-
85
-
#### My header
86
-
87
-
{% /side-by-side %}
88
-
89
-
## Options
90
-
91
-
These are the optional fields you can use to customize your `Node`:
92
-
93
-
{% table %}
94
-
95
-
- Option
96
-
- Type
97
-
- Description {% width="40%" %}
98
-
99
-
---
100
-
101
-
-`render`
102
-
-`string`
103
-
- Name of the output (for example, HTML tag, React component name) to render
104
-
105
-
---
106
-
107
-
-`children`
108
-
-`string[]`
109
-
- Determines which tag or node types can be rendered as children of this node. Used in schema validation.
110
-
111
-
---
112
-
113
-
-`attributes`
114
-
-`{ [string]: SchemaAttribute }`
115
-
- Determines which [values (and their types)](/docs/attributes) can be passed to this node.
116
-
117
-
---
118
-
119
-
-`transform`
120
-
-```js
121
-
(Ast.Node, ?Options) =>
122
-
| RenderableTreeNode
123
-
| RenderableTreeNode[]
124
-
|null
125
-
```
126
-
- Customize the Markdoc transform function for this node, returning the custom output you want to eventually render. This is called during the [`transform` step](/docs/render#transform).
127
-
128
-
---
129
-
130
-
- `validate`
131
-
- ```js
132
-
(Node, ?Options) => ValidationError[];
133
-
```
134
-
- Extend Markdoc validation. This validates that the content meets validation requirements, and is called during the [`validate` step](/docs/render#validate)
135
-
136
-
{% /table %}
137
10
138
11
## Built-in nodes
139
12
@@ -292,6 +165,135 @@ Markdoc comes out of the box with built-in nodes for each of the [CommonMark](ht
292
165
293
166
{% /table %}
294
167
168
+
169
+
## Customizing Markdoc nodes
170
+
171
+
Nodes are elements that Markdoc inherits from Markdown, specifically the [CommonMark specification](https://commonmark.org/).
172
+
173
+
You define custom nodes by passing a custom Node to your [`Config`](/docs/syntax#config), like:
if (attributes.id&&typeofattributes.id==='string') {
201
+
returnattributes.id;
202
+
}
203
+
return children
204
+
.filter((child) =>typeof child ==='string')
205
+
.join('')
206
+
.replace(/[?]/g, '')
207
+
.replace(/\s+/g, '-')
208
+
.toLowerCase();
209
+
}
210
+
211
+
exportconstheading= {
212
+
children: ['inline'],
213
+
attributes: {
214
+
id: { type:String },
215
+
level: { type:Number, required:true, default:1 }
216
+
},
217
+
transform(node, config) {
218
+
constattributes=node.transformAttributes(config);
219
+
constchildren=node.transformChildren(config);
220
+
221
+
constid=generateID(children, attributes);
222
+
223
+
returnnewTag(
224
+
`h${node.attributes['level']}`,
225
+
{ ...attributes, id },
226
+
children
227
+
);
228
+
}
229
+
};
230
+
```
231
+
232
+
After registering this custom node, you can then use it in your Markdoc, like:
233
+
234
+
{% side-by-side %}
235
+
236
+
{% markdoc-example %}
237
+
238
+
```md
239
+
#### My header
240
+
```
241
+
242
+
{% /markdoc-example %}
243
+
244
+
#### My header
245
+
246
+
{% /side-by-side %}
247
+
248
+
## Options
249
+
250
+
These are the optional fields you can use to customize your `Node`:
251
+
252
+
{% table %}
253
+
254
+
- Option
255
+
- Type
256
+
- Description {% width="40%" %}
257
+
258
+
---
259
+
260
+
-`render`
261
+
-`string`
262
+
- Name of the output (for example, HTML tag, React component name) to render
263
+
264
+
---
265
+
266
+
-`children`
267
+
-`string[]`
268
+
- Determines which tag or node types can be rendered as children of this node. Used in schema validation.
269
+
270
+
---
271
+
272
+
-`attributes`
273
+
-`{ [string]: SchemaAttribute }`
274
+
- Determines which [values (and their types)](/docs/attributes) can be passed to this node.
275
+
276
+
---
277
+
278
+
-`transform`
279
+
-```js
280
+
(Ast.Node, ?Options) =>
281
+
| RenderableTreeNode
282
+
| RenderableTreeNode[]
283
+
|null
284
+
```
285
+
- Customize the Markdoc transform function for this node, returning the custom output you want to eventually render. This is called during the [`transform` step](/docs/render#transform).
286
+
287
+
---
288
+
289
+
- `validate`
290
+
- ```js
291
+
(Node, ?Options) => ValidationError[];
292
+
```
293
+
- Extend Markdoc validation. This validates that the content meets validation requirements, and is called during the [`validate` step](/docs/render#validate)
0 commit comments