@portabletext/markdown 1.0.2 → 1.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +148 -23
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -8,6 +8,54 @@
|
|
|
8
8
|
npm install @portabletext/markdown
|
|
9
9
|
```
|
|
10
10
|
|
|
11
|
+
## Quick start
|
|
12
|
+
|
|
13
|
+
**Markdown → Portable Text**
|
|
14
|
+
|
|
15
|
+
```ts
|
|
16
|
+
import {markdownToPortableText} from '@portabletext/markdown'
|
|
17
|
+
|
|
18
|
+
const blocks = markdownToPortableText('# Hello **world**')
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
```json
|
|
22
|
+
[
|
|
23
|
+
{
|
|
24
|
+
"_type": "block",
|
|
25
|
+
"_key": "f4s8k2",
|
|
26
|
+
"style": "h1",
|
|
27
|
+
"children": [
|
|
28
|
+
{"_type": "span", "_key": "a9c3x1", "text": "Hello ", "marks": []},
|
|
29
|
+
{"_type": "span", "_key": "b7d2m5", "text": "world", "marks": ["strong"]}
|
|
30
|
+
],
|
|
31
|
+
"markDefs": []
|
|
32
|
+
}
|
|
33
|
+
]
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
**Portable Text → Markdown**
|
|
37
|
+
|
|
38
|
+
```ts
|
|
39
|
+
import {portableTextToMarkdown} from '@portabletext/markdown'
|
|
40
|
+
|
|
41
|
+
const markdown = portableTextToMarkdown([
|
|
42
|
+
{
|
|
43
|
+
_type: 'block',
|
|
44
|
+
_key: 'f4s8k2',
|
|
45
|
+
style: 'h1',
|
|
46
|
+
children: [
|
|
47
|
+
{_type: 'span', _key: 'a9c3x1', text: 'Hello ', marks: []},
|
|
48
|
+
{_type: 'span', _key: 'b7d2m5', text: 'world', marks: ['strong']},
|
|
49
|
+
],
|
|
50
|
+
markDefs: [],
|
|
51
|
+
},
|
|
52
|
+
])
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
```md
|
|
56
|
+
# Hello **world**
|
|
57
|
+
```
|
|
58
|
+
|
|
11
59
|
## Supported features
|
|
12
60
|
|
|
13
61
|
| Feature | Markdown → Portable Text | Portable Text → Markdown |
|
|
@@ -35,23 +83,70 @@ npm install @portabletext/markdown
|
|
|
35
83
|
|
|
36
84
|
### `markdownToPortableText`
|
|
37
85
|
|
|
38
|
-
Converts a Markdown string to an array of Portable Text blocks.
|
|
39
|
-
|
|
40
86
|
```ts
|
|
41
87
|
import {markdownToPortableText} from '@portabletext/markdown'
|
|
42
88
|
|
|
43
|
-
const
|
|
89
|
+
const blocks = markdownToPortableText(`
|
|
44
90
|
# Hello World
|
|
45
91
|
|
|
46
|
-
This is
|
|
92
|
+
This is **bold** and *italic* text with a [link](https://example.com).
|
|
47
93
|
|
|
48
94
|
- First item
|
|
49
95
|
- Second item
|
|
96
|
+
`)
|
|
97
|
+
```
|
|
50
98
|
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
99
|
+
```json
|
|
100
|
+
[
|
|
101
|
+
{
|
|
102
|
+
"_type": "block",
|
|
103
|
+
"_key": "k9f2x1",
|
|
104
|
+
"style": "h1",
|
|
105
|
+
"children": [
|
|
106
|
+
{"_type": "span", "_key": "s1a2b3", "text": "Hello World", "marks": []}
|
|
107
|
+
],
|
|
108
|
+
"markDefs": []
|
|
109
|
+
},
|
|
110
|
+
{
|
|
111
|
+
"_type": "block",
|
|
112
|
+
"_key": "m3n4p5",
|
|
113
|
+
"style": "normal",
|
|
114
|
+
"children": [
|
|
115
|
+
{"_type": "span", "_key": "s2c3d4", "text": "This is ", "marks": []},
|
|
116
|
+
{"_type": "span", "_key": "s3e4f5", "text": "bold", "marks": ["strong"]},
|
|
117
|
+
{"_type": "span", "_key": "s4g5h6", "text": " and ", "marks": []},
|
|
118
|
+
{"_type": "span", "_key": "s5i6j7", "text": "italic", "marks": ["em"]},
|
|
119
|
+
{"_type": "span", "_key": "s6k7l8", "text": " text with a ", "marks": []},
|
|
120
|
+
{"_type": "span", "_key": "s7m8n9", "text": "link", "marks": ["a1b2c3"]},
|
|
121
|
+
{"_type": "span", "_key": "s8o9p0", "text": ".", "marks": []}
|
|
122
|
+
],
|
|
123
|
+
"markDefs": [
|
|
124
|
+
{"_type": "link", "_key": "a1b2c3", "href": "https://example.com"}
|
|
125
|
+
]
|
|
126
|
+
},
|
|
127
|
+
{
|
|
128
|
+
"_type": "block",
|
|
129
|
+
"_key": "q1r2s3",
|
|
130
|
+
"style": "normal",
|
|
131
|
+
"listItem": "bullet",
|
|
132
|
+
"level": 1,
|
|
133
|
+
"children": [
|
|
134
|
+
{"_type": "span", "_key": "s9q0r1", "text": "First item", "marks": []}
|
|
135
|
+
],
|
|
136
|
+
"markDefs": []
|
|
137
|
+
},
|
|
138
|
+
{
|
|
139
|
+
"_type": "block",
|
|
140
|
+
"_key": "t4u5v6",
|
|
141
|
+
"style": "normal",
|
|
142
|
+
"listItem": "bullet",
|
|
143
|
+
"level": 1,
|
|
144
|
+
"children": [
|
|
145
|
+
{"_type": "span", "_key": "s0s1t2", "text": "Second item", "marks": []}
|
|
146
|
+
],
|
|
147
|
+
"markDefs": []
|
|
148
|
+
}
|
|
149
|
+
]
|
|
55
150
|
```
|
|
56
151
|
|
|
57
152
|
The conversion is driven by two concepts:
|
|
@@ -215,23 +310,60 @@ markdownToPortableText(markdown, {
|
|
|
215
310
|
|
|
216
311
|
### `portableTextToMarkdown`
|
|
217
312
|
|
|
218
|
-
Converts an array of Portable Text blocks to a Markdown string.
|
|
219
|
-
|
|
220
313
|
```ts
|
|
221
314
|
import {portableTextToMarkdown} from '@portabletext/markdown'
|
|
222
315
|
|
|
223
|
-
const
|
|
316
|
+
const markdown = portableTextToMarkdown([
|
|
224
317
|
{
|
|
225
318
|
_type: 'block',
|
|
226
|
-
_key: '
|
|
319
|
+
_key: 'k9f2x1',
|
|
227
320
|
style: 'h1',
|
|
228
|
-
children: [{_type: 'span', _key: '
|
|
321
|
+
children: [{_type: 'span', _key: 's1a2b3', text: 'Hello World', marks: []}],
|
|
229
322
|
markDefs: [],
|
|
230
323
|
},
|
|
231
|
-
|
|
324
|
+
{
|
|
325
|
+
_type: 'block',
|
|
326
|
+
_key: 'm3n4p5',
|
|
327
|
+
style: 'normal',
|
|
328
|
+
children: [
|
|
329
|
+
{_type: 'span', _key: 's2c3d4', text: 'This is ', marks: []},
|
|
330
|
+
{_type: 'span', _key: 's3e4f5', text: 'bold', marks: ['strong']},
|
|
331
|
+
{_type: 'span', _key: 's4g5h6', text: ' and ', marks: []},
|
|
332
|
+
{_type: 'span', _key: 's5i6j7', text: 'italic', marks: ['em']},
|
|
333
|
+
{_type: 'span', _key: 's6k7l8', text: ' text with a ', marks: []},
|
|
334
|
+
{_type: 'span', _key: 's7m8n9', text: 'link', marks: ['a1b2c3']},
|
|
335
|
+
{_type: 'span', _key: 's8o9p0', text: '.', marks: []},
|
|
336
|
+
],
|
|
337
|
+
markDefs: [{_type: 'link', _key: 'a1b2c3', href: 'https://example.com'}],
|
|
338
|
+
},
|
|
339
|
+
{
|
|
340
|
+
_type: 'block',
|
|
341
|
+
_key: 'q1r2s3',
|
|
342
|
+
style: 'normal',
|
|
343
|
+
listItem: 'bullet',
|
|
344
|
+
level: 1,
|
|
345
|
+
children: [{_type: 'span', _key: 's9q0r1', text: 'First item', marks: []}],
|
|
346
|
+
markDefs: [],
|
|
347
|
+
},
|
|
348
|
+
{
|
|
349
|
+
_type: 'block',
|
|
350
|
+
_key: 't4u5v6',
|
|
351
|
+
style: 'normal',
|
|
352
|
+
listItem: 'bullet',
|
|
353
|
+
level: 1,
|
|
354
|
+
children: [{_type: 'span', _key: 's0s1t2', text: 'Second item', marks: []}],
|
|
355
|
+
markDefs: [],
|
|
356
|
+
},
|
|
357
|
+
])
|
|
358
|
+
```
|
|
232
359
|
|
|
233
|
-
|
|
234
|
-
|
|
360
|
+
```md
|
|
361
|
+
# Hello World
|
|
362
|
+
|
|
363
|
+
This is **bold** and _italic_ text with a [link](https://example.com).
|
|
364
|
+
|
|
365
|
+
- First item
|
|
366
|
+
- Second item
|
|
235
367
|
```
|
|
236
368
|
|
|
237
369
|
The conversion is driven by **Renderers**: functions that render Portable Text elements to Markdown strings. The library includes default renderers for common types; provide your own for custom block types.
|
|
@@ -319,13 +451,6 @@ portableTextToMarkdown(blocks, {
|
|
|
319
451
|
| `DefaultImageRenderer` | `{src: string, alt?: string, title?: string}` | `` |
|
|
320
452
|
| `DefaultTableRenderer` | `{rows: [...], headerRows?: number}` | Markdown table |
|
|
321
453
|
|
|
322
|
-
**Other exports:** The library also exports mark renderers, block style renderers, and TypeScript types:
|
|
323
|
-
|
|
324
|
-
- `DefaultStrongRenderer`, `DefaultEmRenderer`, `DefaultCodeRenderer`, `DefaultUnderlineRenderer`, `DefaultStrikeThroughRenderer`, `DefaultLinkRenderer`
|
|
325
|
-
- `DefaultNormalRenderer`, `DefaultBlockquoteRenderer`, `DefaultH1Renderer`–`DefaultH6Renderer`
|
|
326
|
-
- `DefaultListItemRenderer`, `DefaultHardBreakRenderer`, `DefaultBlockSpacingRenderer`
|
|
327
|
-
- `BlockSpacingRenderer`, `PortableTextRenderers`, `PortableTextMarkRenderer`, etc.
|
|
328
|
-
|
|
329
454
|
#### What renderers receive
|
|
330
455
|
|
|
331
456
|
**Block renderers** (`block.*`):
|