Skip to content

Commit e0d1430

Browse files
committed
feat(markdown): parseMarkdown
Signed-off-by: Lexus Drumgold <[email protected]>
1 parent 8b5871a commit e0d1430

14 files changed

+442
-99
lines changed

README.md

Lines changed: 80 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,10 @@
2020
- [Use](#use)
2121
- [API](#api)
2222
- [`fromDocs(value[, options])`](#fromdocsvalue-options)
23+
- [`parseMarkdown<T>(value, options)`](#parsemarkdowntvalue-options)
24+
- [`MarkdownOptions`](#markdownoptions)
2325
- [`Options`](#options)
26+
- [`ParseMarkdownOptions`](#parsemarkdownoptions)
2427
- [`Transform`](#transform)
2528
- [Syntax](#syntax)
2629
- [Docblock](#docblock)
@@ -203,14 +206,11 @@ console.log(inspect(tree))
203206
root[9]
204207
├─0 comment[3] (1:1-5:4, 0-122)
205208
│ │ code: null
206-
│ ├─0 blockTag<file>[1] (2:4-2:27, 7-30)
207-
│ │ │ tag: "@file"
209+
│ ├─0 blockTag<@file>[1] (2:4-2:27, 7-30)
208210
│ │ └─0 text "FibonacciSequence" (2:10-2:27, 13-30)
209-
│ ├─1 blockTag<module>[1] (3:4-3:29, 34-59)
210-
│ │ │ tag: "@module"
211+
│ ├─1 blockTag<@module>[1] (3:4-3:29, 34-59)
211212
│ │ └─0 text "FibonacciSequence" (3:12-3:29, 42-59)
212-
│ └─2 blockTag<see>[1] (4:4-4:59, 63-118)
213-
│ │ tag: "@see"
213+
│ └─2 blockTag<@see>[1] (4:4-4:59, 63-118)
214214
│ └─0 text "https://codewars.com/kata/55695bc4f75bbaea5100016b" (4:9-4:59, 68-118)
215215
├─1 comment[2] (7:1-19:4, 124-414)
216216
│ │ code: null
@@ -229,97 +229,84 @@ root[9]
229229
│ │ └─1 code "1, 1, 2, 3, 5, 8, 13, ..., 89, 144, 233, 377, ..." (13:4-15:7, 292-358)
230230
│ │ lang: "txt"
231231
│ │ meta: null
232-
│ └─1 blockTag<implements>[1] (18:4-18:42, 372-410)
233-
│ │ tag: "@implements"
232+
│ └─1 blockTag<@implements>[1] (18:4-18:42, 372-410)
234233
│ └─0 typeExpression "Iterator<number, number>" (18:16-18:42, 384-410)
235234
├─2 comment[4] (21:3-27:6, 479-583)
236235
│ │ code: null
237236
│ ├─0 description[1] (22:6-22:35, 488-517)
238237
│ │ └─0 paragraph[1] (22:6-22:35, 488-517)
239238
│ │ └─0 text "First managed sequence value." (22:6-22:35, 488-517)
240-
│ ├─1 blockTag<public>[0] (24:6-24:13, 528-535)
241-
│ │ tag: "@public"
242-
│ ├─2 blockTag<instance>[0] (25:6-25:15, 541-550)
243-
│ │ tag: "@instance"
244-
│ └─3 blockTag<member>[2] (26:6-26:27, 556-577)
245-
│ │ tag: "@member"
239+
│ ├─1 blockTag<@public>[0] (24:6-24:13, 528-535)
240+
│ ├─2 blockTag<@instance>[0] (25:6-25:15, 541-550)
241+
│ └─3 blockTag<@member>[2] (26:6-26:27, 556-577)
246242
│ ├─0 typeExpression "number" (26:14-26:22, 564-572)
247243
│ └─1 text "fib1" (26:23-26:27, 573-577)
248244
├─3 comment[4] (30:3-36:6, 609-714)
249245
│ │ code: null
250246
│ ├─0 description[1] (31:6-31:36, 618-648)
251247
│ │ └─0 paragraph[1] (31:6-31:36, 618-648)
252248
│ │ └─0 text "Second managed sequence value." (31:6-31:36, 618-648)
253-
│ ├─1 blockTag<public>[0] (33:6-33:13, 659-666)
254-
│ │ tag: "@public"
255-
│ ├─2 blockTag<instance>[0] (34:6-34:15, 672-681)
256-
│ │ tag: "@instance"
257-
│ └─3 blockTag<member>[2] (35:6-35:27, 687-708)
258-
│ │ tag: "@member"
249+
│ ├─1 blockTag<@public>[0] (33:6-33:13, 659-666)
250+
│ ├─2 blockTag<@instance>[0] (34:6-34:15, 672-681)
251+
│ └─3 blockTag<@member>[2] (35:6-35:27, 687-708)
259252
│ ├─0 typeExpression "number" (35:14-35:22, 695-703)
260253
│ └─1 text "fib2" (35:23-35:27, 704-708)
261254
├─4 comment[4] (39:3-45:6, 740-834)
262255
│ │ code: null
263256
│ ├─0 description[1] (40:6-40:25, 749-768)
264257
│ │ └─0 paragraph[1] (40:6-40:25, 749-768)
265258
│ │ └─0 text "Max sequence value." (40:6-40:25, 749-768)
266-
│ ├─1 blockTag<private>[0] (42:6-42:14, 779-787)
267-
│ │ tag: "@private"
268-
│ ├─2 blockTag<instance>[0] (43:6-43:15, 793-802)
269-
│ │ tag: "@instance"
270-
│ └─3 blockTag<member>[2] (44:6-44:26, 808-828)
271-
│ │ tag: "@member"
259+
│ ├─1 blockTag<@private>[0] (42:6-42:14, 779-787)
260+
│ ├─2 blockTag<@instance>[0] (43:6-43:15, 793-802)
261+
│ └─3 blockTag<@member>[2] (44:6-44:26, 808-828)
272262
│ ├─0 typeExpression "number" (44:14-44:22, 816-824)
273263
│ └─1 text "max" (44:23-44:26, 825-828)
274264
├─5 comment[2] (48:3-52:6, 862-995)
275265
│ │ code: null
276266
│ ├─0 description[1] (49:6-49:47, 871-912)
277267
│ │ └─0 paragraph[1] (49:6-49:47, 871-912)
278268
│ │ └─0 text "Create a new fibonacci sequence iterator." (49:6-49:47, 871-912)
279-
│ └─1 blockTag<param>[2] (51:6-51:72, 923-989)
280-
│ │ tag: "@param"
269+
│ └─1 blockTag<@param>[2] (51:6-51:72, 923-989)
281270
│ ├─0 typeExpression "number" (51:13-51:21, 930-938)
282271
│ └─1 text "[max=Number.MAX_SAFE_INTEGER] - Max sequence value" (51:22-51:72, 939-989)
283272
├─6 comment[4] (58:3-65:6, 1122-1259)
284273
│ │ code: null
285274
│ ├─0 description[1] (59:6-59:24, 1131-1149)
286275
│ │ └─0 paragraph[1] (59:6-59:24, 1131-1149)
287276
│ │ └─0 text "Iterable protocol." (59:6-59:24, 1131-1149)
288-
│ ├─1 blockTag<public>[0] (61:6-61:13, 1160-1167)
289-
│ │ tag: "@public"
290-
│ ├─2 blockTag<instance>[0] (62:6-62:15, 1173-1182)
291-
│ │ tag: "@instance"
292-
│ └─3 blockTag<return>[2] (64:6-64:66, 1193-1253)
293-
│ │ tag: "@return"
277+
│ ├─1 blockTag<@public>[0] (61:6-61:13, 1160-1167)
278+
│ ├─2 blockTag<@instance>[0] (62:6-62:15, 1173-1182)
279+
│ └─3 blockTag<@return>[2] (64:6-64:66, 1193-1253)
294280
│ ├─0 typeExpression "IterableIterator<number>" (64:14-64:40, 1201-1227)
295281
│ └─1 text "Current sequence iterator" (64:41-64:66, 1228-1253)
296282
├─7 comment[4] (70:3-77:6, 1340-1504)
297283
│ │ code: null
298284
│ ├─0 description[1] (71:6-71:51, 1349-1394)
299285
│ │ └─0 paragraph[1] (71:6-71:51, 1349-1394)
300286
│ │ └─0 text "Get the next value in the fibonacci sequence." (71:6-71:51, 1349-1394)
301-
│ ├─1 blockTag<public>[0] (73:6-73:13, 1405-1412)
302-
│ │ tag: "@public"
303-
│ ├─2 blockTag<instance>[0] (74:6-74:15, 1418-1427)
304-
│ │ tag: "@instance"
305-
│ └─3 blockTag<return>[2] (76:6-76:66, 1438-1498)
306-
│ │ tag: "@return"
287+
│ ├─1 blockTag<@public>[0] (73:6-73:13, 1405-1412)
288+
│ ├─2 blockTag<@instance>[0] (74:6-74:15, 1418-1427)
289+
│ └─3 blockTag<@return>[2] (76:6-76:66, 1438-1498)
307290
│ ├─0 typeExpression "IteratorResult<number, number>" (76:14-76:46, 1446-1478)
308291
│ └─1 text "Next sequence value" (76:47-76:66, 1479-1498)
309292
└─8 comment[2] (79:5-83:8, 1559-1639)
310293
│ code: null
311294
├─0 description[1] (80:8-80:33, 1570-1595)
312295
│ └─0 paragraph[1] (80:8-80:33, 1570-1595)
313296
│ └─0 text "Temporary sequence value." (80:8-80:33, 1570-1595)
314-
└─1 blockTag<const>[2] (82:8-82:29, 1610-1631)
315-
│ tag: "@const"
297+
└─1 blockTag<@const>[2] (82:8-82:29, 1610-1631)
316298
├─0 typeExpression "number" (82:15-82:23, 1617-1625)
317299
└─1 text "value" (82:24-82:29, 1626-1631)
318300
```
319301

320302
## API
321303

322-
This package exports the identifier [`fromDocs`](#fromdocsvalue-options). There is no default export.
304+
This package exports the following identifiers:
305+
306+
- [`fromDocs`](#fromdocsvalue-options)
307+
- [`parseMarkdown`](#parsemarkdowntvalue-options)
308+
309+
There is no default export.
323310

324311
### `fromDocs(value[, options])`
325312

@@ -334,24 +321,65 @@ Turn docblocks into a syntax tree.
334321

335322
docast tree ([`Root`][docast-tree])
336323

324+
### `parseMarkdown<T>(value, options)`
325+
326+
Turn markdown into [`mdast`][mdast] child nodes, with respect for comment delimiters.
327+
328+
#### Type Parameters
329+
330+
- `T` ([`RootContent`][mdast-content]) &mdash; `mdast` child node type
331+
332+
#### Parameters
333+
334+
- `value` ([`VFile`][vfile] | `string`) &mdash; markdown to parse
335+
- `options` ([`ParseMarkdownOptions`](#parsemarkdownoptions)) &mdash; configuration options
336+
337+
#### Returns
338+
339+
`mdast` child node array (`T[]`)
340+
341+
### `MarkdownOptions`
342+
343+
Markdown configuration options (TypeScript type).
344+
345+
#### Properties
346+
347+
- `mdastExtensions` ([`mdast.Extension[]`][mdast-extension], optional) &mdash; markdown extensions to change how
348+
micromark tokens are converted to nodes
349+
- `micromarkExtensions` ([`micromark.Extension[]`][micromark-extension], optional) &mdash; micromark extensions to
350+
change how markdown is parsed
351+
337352
### `Options`
338353

339354
Configuration options (TypeScript type).
340355

356+
#### Extends
357+
358+
- [`MarkdownOptions`](#markdownoptions)
359+
341360
#### Properties
342361

343362
- `codeblocks` (`OneOrMany<RegExp | string>`, optional) &mdash; block tag names, or regular expressions, matching block
344363
tags that should have their text converted to [`Code`][mdast-code] when parsed as markdown
345-
- **default**: `'example'`
364+
- **default**: `'@example'`
346365
- `from` ([`docast.Point`][docast-point], optional) &mdash; parser start point. node positions will be relative to this
347366
point
348367
- **default**: `{ column: 1, line: 1, offset: 0 }`
349-
- `mdastExtensions` ([`mdast.Extension[]`][mdast-extension], optional) &mdash; markdown extensions to change how
350-
micromark tokens are converted to nodes
351-
- `micromarkExtensions` ([`micromark.Extension[]`][micromark-extension], optional) &mdash; micromark extensions to
352-
change how markdown is parsed
353368
- `transforms` ([`Transform[]`](#transform), optional) &mdash; tree transforms
354369

370+
### `ParseMarkdownOptions`
371+
372+
Options for parsing markdown with respect for comment delimiters (TypeScript type).
373+
374+
#### Extends
375+
376+
- [`MarkdownOptions`](#markdownoptions)
377+
378+
#### Properties
379+
380+
- `code` (`boolean`, optional) &mdash; parse markdown value as fenced code
381+
- `position` ([`Position`][docast-position]) &mdash; position of markdown value
382+
355383
### `Transform`
356384

357385
Change the AST after parsing is complete (TypeScript type).
@@ -362,7 +390,7 @@ Change the AST after parsing is complete (TypeScript type).
362390

363391
#### Returns
364392

365-
Nothing
393+
nothing (`void`)
366394

367395
## Syntax
368396

@@ -390,14 +418,17 @@ See [`CONTRIBUTING.md`](CONTRIBUTING.md).
390418

391419
[docast-parse]: https://github.com/flex-development/docast-parse
392420
[docast-point]: https://github.com/flex-development/docast#point
421+
[docast-position]: https://github.com/flex-development/docast#position
393422
[docast-tree]: https://github.com/flex-development/docast#root
394423
[docast]: https://github.com/flex-development/docast
395424
[docblock]: https://github.com/flex-development/docast#docblock-comment
396425
[esm]: https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c
397426
[esmsh]: https://esm.sh/
398427
[mdast-code]: https://github.com/syntax-tree/mdast#code
428+
[mdast-content]: https://github.com/syntax-tree/mdast#content-model
399429
[mdast-extension]: https://github.com/syntax-tree/mdast-util-from-markdown#extension
400430
[mdast-util-from-markdown]: https://github.com/syntax-tree/mdast-util-from-markdown
431+
[mdast]: https://github.com/syntax-tree/mdast
401432
[micromark-extension]: https://github.com/micromark/micromark#extensions
402433
[micromark]: https://github.com/micromark/micromark
403434
[typescript]: https://www.typescriptlang.org

src/__snapshots__/util.markdown.snap

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
2+
3+
exports[`unit:parseMarkdown > should return mdast child node array 1`] = `
4+
root[12] (111:6-128:42, 2397-3216)
5+
├─0 paragraph[5] (111:6-111:53, 2397-2444)
6+
│ ├─0 text "Get a " (111:6-111:12, 2397-2403)
7+
│ ├─1 linkReference[1] (111:12-111:24, 2403-2415)
8+
│ │ │ label: "1"
9+
│ │ │ identifier: "1"
10+
│ │ │ referenceType: "full"
11+
│ │ └─0 emphasis[1] (111:13-111:20, 2404-2411)
12+
│ │ └─0 text "point" (111:14-111:19, 2405-2410)
13+
│ ├─2 text " in the document by " (111:24-111:44, 2415-2435)
14+
│ ├─3 inlineCode "offset" (111:44-111:52, 2435-2443)
15+
│ └─4 text "." (111:52-111:53, 2443-2444)
16+
├─1 break (111:53-112:1, 2444-2445)
17+
├─2 break (112:5-113:1, 2449-2450)
18+
│ data: {"blank":true}
19+
├─3 paragraph[13] (113:6-114:76, 2455-2606)
20+
│ ├─0 text "The given " (113:6-113:16, 2455-2465)
21+
│ ├─1 inlineCode "offset" (113:16-113:24, 2465-2473)
22+
│ ├─2 text " must be an integer in the range " (113:24-113:57, 2473-2506)
23+
│ ├─3 inlineCode "[0, document.length]" (113:57-113:79, 2506-2528)
24+
│ ├─4 text "." (113:79-113:80, 2528-2529)
25+
│ ├─5 break (113:80-113:81, 2529-2530)
26+
│ │ data: {"hard":true}
27+
│ ├─6 text "If invalid, " (114:6-114:18, 2536-2548)
28+
│ ├─7 inlineCode "point.column" (114:18-114:32, 2548-2562)
29+
│ ├─8 text " and " (114:32-114:37, 2562-2567)
30+
│ ├─9 inlineCode "point.line" (114:37-114:49, 2567-2579)
31+
│ ├─10 text " will have a value of " (114:49-114:71, 2579-2601)
32+
│ ├─11 inlineCode "-1" (114:71-114:75, 2601-2605)
33+
│ └─12 text "." (114:75-114:76, 2605-2606)
34+
├─4 break (114:76-115:1, 2606-2607)
35+
├─5 break (115:5-116:1, 2611-2612)
36+
│ data: {"blank":true}
37+
├─6 blockquote[7] (116:6-124:35, 2617-3064)
38+
│ ├─0 paragraph[4] (116:8-116:63, 2619-2674)
39+
│ │ ├─0 strong[1] (116:8-116:17, 2619-2628)
40+
│ │ │ └─0 text "Point" (116:10-116:15, 2621-2626)
41+
│ │ ├─1 text " represents one place in a source " (116:17-116:51, 2628-2662)
42+
│ │ ├─2 linkReference[1] (116:51-116:62, 2662-2673)
43+
│ │ │ │ label: "2"
44+
│ │ │ │ identifier: "2"
45+
│ │ │ │ referenceType: "full"
46+
│ │ │ └─0 emphasis[1] (116:52-116:58, 2663-2669)
47+
│ │ │ └─0 text "file" (116:53-116:57, 2664-2668)
48+
│ │ └─3 text "." (116:62-116:63, 2673-2674)
49+
│ ├─1 break (116:63-117:1, 2674-2675)
50+
│ ├─2 break (117:7-118:1, 2681-2682)
51+
│ │ data: {"blank":true}
52+
│ ├─3 paragraph[13] (118:8-121:35, 2689-2945)
53+
│ │ ├─0 text "The " (118:8-118:12, 2689-2693)
54+
│ │ ├─1 inlineCode "line" (118:12-118:18, 2693-2699)
55+
│ │ ├─2 text " field (" (118:18-118:26, 2699-2707)
56+
│ │ ├─3 inlineCode "1" (118:26-118:29, 2707-2710)
57+
│ │ ├─4 text "-indexed integer) represents a line in a source file. The " (118:29-119:18, 2710-2775)
58+
│ │ ├─5 inlineCode "column" (119:18-119:26, 2775-2783)
59+
│ │ ├─6 text " field (" (119:26-119:34, 2783-2791)
60+
│ │ ├─7 inlineCode "1" (119:34-119:37, 2791-2794)
61+
│ │ ├─8 text "-indexed integer) represents a column in a source file. The " (119:37-120:25, 2794-2861)
62+
│ │ ├─9 inlineCode "offset" (120:25-120:33, 2861-2869)
63+
│ │ ├─10 text " field (" (120:33-120:41, 2869-2877)
64+
│ │ ├─11 inlineCode "0" (120:41-120:44, 2877-2880)
65+
│ │ └─12 text "-indexed integer) represents a character in a source file." (120:44-121:35, 2880-2945)
66+
│ ├─4 break (121:35-122:1, 2945-2946)
67+
│ ├─5 break (122:7-123:1, 2952-2953)
68+
│ │ data: {"blank":true}
69+
│ └─6 paragraph[3] (123:8-124:35, 2960-3064)
70+
│ ├─0 text "The term character means a (UTF-16) code unit which is defined in the " (123:8-124:1, 2960-3030)
71+
│ ├─1 linkReference[1] (124:8-124:20, 3037-3049)
72+
│ │ │ label: "3"
73+
│ │ │ identifier: "3"
74+
│ │ │ referenceType: "full"
75+
│ │ └─0 text "Web IDL" (124:9-124:16, 3038-3045)
76+
│ └─2 text " specification." (124:20-124:35, 3049-3064)
77+
├─7 break (124:35-125:1, 3064-3065)
78+
├─8 break (125:5-126:1, 3069-3070)
79+
│ data: {"blank":true}
80+
├─9 definition (126:6-126:53, 3075-3122)
81+
│ identifier: "1"
82+
│ label: "1"
83+
│ title: null
84+
│ url: "https://github.com/syntax-tree/unist#point"
85+
├─10 definition (127:6-127:52, 3128-3174)
86+
│ identifier: "2"
87+
│ label: "2"
88+
│ title: null
89+
│ url: "https://github.com/syntax-tree/unist#file"
90+
└─11 definition (128:6-128:42, 3180-3216)
91+
identifier: "3"
92+
label: "3"
93+
title: null
94+
url: "https://webidl.spec.whatwg.org/"
95+
`;

src/__tests__/index.e2e.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@ import * as testSubject from '../index'
77

88
describe('e2e:docast-util-from-docs', () => {
99
it('should expose public api', () => {
10-
expect(testSubject).to.have.keys(['fromDocs'])
10+
expect(testSubject).to.have.keys(['fromDocs', 'parseMarkdown'])
1111
})
1212
})

0 commit comments

Comments
 (0)