@portabletext/markdown 1.0.1 → 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.
Files changed (3) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +148 -23
  3. package/package.json +20 -21
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2016 - 2025 Sanity.io
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
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 markdown = `
89
+ const blocks = markdownToPortableText(`
44
90
  # Hello World
45
91
 
46
- This is a **bold** and *italic* text with a [link](https://example.com).
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
- > A blockquote
52
- `
53
-
54
- const blocks = markdownToPortableText(markdown)
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 blocks = [
316
+ const markdown = portableTextToMarkdown([
224
317
  {
225
318
  _type: 'block',
226
- _key: 'abc123',
319
+ _key: 'k9f2x1',
227
320
  style: 'h1',
228
- children: [{_type: 'span', _key: 'def456', text: 'Hello World', marks: []}],
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
- const markdown = portableTextToMarkdown(blocks)
234
- // # Hello World
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}` | `![alt](src "title")` |
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.*`):
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@portabletext/markdown",
3
- "version": "1.0.1",
3
+ "version": "1.0.3",
4
4
  "description": "Convert Portable Text to and from Markdown",
5
5
  "keywords": [
6
6
  "portable-text",
@@ -32,38 +32,37 @@
32
32
  "dist",
33
33
  "src"
34
34
  ],
35
- "scripts": {
36
- "build": "pkg-utils build --strict --check --clean",
37
- "check:lint": "biome lint .",
38
- "check:types": "tsc",
39
- "check:types:watch": "tsc --watch",
40
- "clean": "del .turbo && del dist && del node_modules",
41
- "dev": "pkg-utils watch",
42
- "lint:fix": "biome lint --write .",
43
- "prepublishOnly": "turbo run build",
44
- "test": "vitest --run",
45
- "test:unit": "vitest --run --project unit",
46
- "test:unit:watch": "vitest --project unit",
47
- "test:watch": "vitest"
48
- },
49
35
  "dependencies": {
50
- "@portabletext/schema": "workspace:^",
51
36
  "@portabletext/toolkit": "^4.0.0",
52
- "markdown-it": "^14.1.0"
37
+ "markdown-it": "^14.1.0",
38
+ "@portabletext/schema": "^2.0.0"
53
39
  },
54
40
  "devDependencies": {
55
- "@portabletext/test": "workspace:^",
56
41
  "@portabletext/types": "^3.0.0",
57
42
  "@sanity/pkg-utils": "^10.0.0",
58
43
  "@sanity/tsconfig": "^1.0.0",
59
44
  "@types/markdown-it": "^14.1.2",
60
- "typescript": "catalog:",
61
- "vitest": "^4.0.9"
45
+ "typescript": "5.9.3",
46
+ "vitest": "^4.0.9",
47
+ "@portabletext/test": "^1.0.0"
62
48
  },
63
49
  "engines": {
64
50
  "node": ">=20.19 <22 || >=22.12"
65
51
  },
66
52
  "publishConfig": {
67
53
  "access": "public"
54
+ },
55
+ "scripts": {
56
+ "build": "pkg-utils build --strict --check --clean",
57
+ "check:lint": "biome lint .",
58
+ "check:types": "tsc",
59
+ "check:types:watch": "tsc --watch",
60
+ "clean": "del .turbo && del dist && del node_modules",
61
+ "dev": "pkg-utils watch",
62
+ "lint:fix": "biome lint --write .",
63
+ "test": "vitest --run",
64
+ "test:unit": "vitest --run --project unit",
65
+ "test:unit:watch": "vitest --project unit",
66
+ "test:watch": "vitest"
68
67
  }
69
- }
68
+ }