docast utility to parse docblocks
npm install @flex-development/docast-util-from-docs








[docast][docast] utility that turns docblocks into a syntax tree.
- What is this?
- When should I use this?
- Install
- Use
- API
- [fromDocs(value[, options])](#fromdocsvalue-options)
- Options
- Transform
- Syntax
- Docblock
- Markdown
- Syntax tree
- Types
- Contribute
This package is a utility that takes [docblock][docblock] input and turns it into a [docast][docast] syntax tree.
This utility turns docblocks into tokens, and then turns those tokens into nodes. Markdown in docblocks is turned into
tokens using [micromark][micromark], and turned into nodes using [mdast-util-from-markdown][mdast-util-from-markdown].
This package is also the backbone of [docast-parse][docast-parse], a [unified][unified] compliant file parser that
focuses on making it easier to transform docblocks by abstracting these internals away.
TODO: ecosystem
If you want to handle syntax trees manually, use this. For an easier time processing docblocks, use
[docast-parse][docast-parse] instead.
This package is [ESM only][esm].
``sh`
yarn add @flex-development/docast-util-from-docs
yarn add -D @flex-development/docast @types/mdast @types/unist micromark-util-types
From Git:
`sh`
yarn add @flex-development/docast-util-from-docs@flex-development/docast-util-from-docs
See Git - Protocols | Yarn
for details on requesting a specific branch, commit, or tag.
Say we have the following TypeScript file fibonacci-sequence.ts:
``ts
/**
* @file FibonacciSequence
* @module FibonacciSequence
* @see https://codewars.com/kata/55695bc4f75bbaea5100016b
*/
/**
* Fibonacci sequence iterator.
*
* :::info
* A fibonacci sequence starts with two 1s. Every element afterwards is the`
* sum of the two previous elements:
* txt`
* 1, 1, 2, 3, 5, 8, 13, ..., 89, 144, 233, 377, ...
*
* :::
*
* @implements {Iterator
*/
class FibonacciSequence implements Iterator
/**
* First managed sequence value.
*
* @public
* @instance
* @member {number} fib1
*/
public fib1: number
/**
* Second managed sequence value.
*
* @public
* @instance
* @member {number} fib2
*/
public fib2: number
/**
* Max sequence value.
*
* @private
* @instance
* @member {number} max
*/
readonly #max: number
/**
* Create a new fibonacci sequence iterator.
*
* @param {number} [max=Number.MAX_SAFE_INTEGER] - Max sequence value
*/
constructor(max: number = Number.MAX_SAFE_INTEGER) {
this.#max = max < 0 ? 0 : max
this.fib1 = this.fib2 = 1
}
/**
* Iterable protocol.
*
* @public
* @instance
*
* @return {IterableIterator
*/
public [Symbol.iterator](): IterableIterator
return this
}
/**
* Get the next value in the fibonacci sequence.
*
* @public
* @instance
*
* @return {IteratorResult
*/
public next(): IteratorResult
/**
* Temporary sequence value.
*
* @const {number} value
*/
const value: number = this.fib1
// reset current sequence values
this.fib1 = this.fib2
this.fib2 = value + this.fib1
return { done: value >= this.#max, value }
}
}
export default FibonacciSequence
``
…and our module example.mjs looks as follows:
`js
import { fromDocs } from '@flex-development/docast-util-from-docs'
import { directiveFromMarkdown } from 'mdast-util-directive'
import { directive } from 'micromark-extension-directive'
import { read } from 'to-vfile'
import { inspect } from 'unist-util-inspect'
const file = await read('fibonacci-sequence.ts')
const tree = fromDocs(file, {
mdastExtensions: [directiveFromMarkdown()],
micromarkExtensions: [directive()]
})
console.log(inspect(tree))
`
…now running node example.mjs yields:
`sh`
root[9]
├─0 comment[3] (1:1-5:4, 0-122)
│ │ code: null
│ ├─0 blockTag
│ │ │ tag: "@file"
│ │ └─0 text "FibonacciSequence" (2:10-2:27, 13-30)
│ ├─1 blockTag
│ │ │ tag: "@module"
│ │ └─0 text "FibonacciSequence" (3:12-3:29, 42-59)
│ └─2 blockTag
│ │ tag: "@see"
│ └─0 text "https://codewars.com/kata/55695bc4f75bbaea5100016b" (4:9-4:59, 68-118)
├─1 comment[2] (7:1-19:4, 124-414)
│ │ code: null
│ ├─0 description[4] (8:4-16:7, 131-365)
│ │ ├─0 paragraph[1] (8:4-8:32, 131-159)
│ │ │ └─0 text "Fibonacci sequence iterator." (8:4-8:32, 131-159)
│ │ ├─1 break (8:32-9:1, 159-160)
│ │ ├─2 break (9:3-10:1, 162-163)
│ │ │ data: {"blank":true}
│ │ └─3 containerDirective
│ │ │ attributes: {}
│ │ ├─0 paragraph[3] (11:4-12:37, 177-288)
│ │ │ ├─0 text "A fibonacci sequence starts with two " (11:4-11:41, 177-214)
│ │ │ ├─1 inlineCode "1" (11:41-11:44, 214-217)
│ │ │ └─2 text "s. Every element afterwards is the sum of the two previous elements:" (11:44-12:37, 217-288)
│ │ └─1 code "1, 1, 2, 3, 5, 8, 13, ..., 89, 144, 233, 377, ..." (13:4-15:7, 292-358)
│ │ lang: "txt"
│ │ meta: null
│ └─1 blockTag
│ │ tag: "@implements"
│ └─0 typeExpression "Iterator
├─2 comment[4] (21:3-27:6, 479-583)
│ │ code: null
│ ├─0 description[1] (22:6-22:35, 488-517)
│ │ └─0 paragraph[1] (22:6-22:35, 488-517)
│ │ └─0 text "First managed sequence value." (22:6-22:35, 488-517)
│ ├─1 blockTag
│ │ tag: "@public"
│ ├─2 blockTag
│ │ tag: "@instance"
│ └─3 blockTag
│ │ tag: "@member"
│ ├─0 typeExpression "number" (26:14-26:22, 564-572)
│ └─1 text "fib1" (26:23-26:27, 573-577)
├─3 comment[4] (30:3-36:6, 609-714)
│ │ code: null
│ ├─0 description[1] (31:6-31:36, 618-648)
│ │ └─0 paragraph[1] (31:6-31:36, 618-648)
│ │ └─0 text "Second managed sequence value." (31:6-31:36, 618-648)
│ ├─1 blockTag
│ │ tag: "@public"
│ ├─2 blockTag
│ │ tag: "@instance"
│ └─3 blockTag
│ │ tag: "@member"
│ ├─0 typeExpression "number" (35:14-35:22, 695-703)
│ └─1 text "fib2" (35:23-35:27, 704-708)
├─4 comment[4] (39:3-45:6, 740-834)
│ │ code: null
│ ├─0 description[1] (40:6-40:25, 749-768)
│ │ └─0 paragraph[1] (40:6-40:25, 749-768)
│ │ └─0 text "Max sequence value." (40:6-40:25, 749-768)
│ ├─1 blockTag
│ │ tag: "@private"
│ ├─2 blockTag
│ │ tag: "@instance"
│ └─3 blockTag
│ │ tag: "@member"
│ ├─0 typeExpression "number" (44:14-44:22, 816-824)
│ └─1 text "max" (44:23-44:26, 825-828)
├─5 comment[2] (48:3-52:6, 862-995)
│ │ code: null
│ ├─0 description[1] (49:6-49:47, 871-912)
│ │ └─0 paragraph[1] (49:6-49:47, 871-912)
│ │ └─0 text "Create a new fibonacci sequence iterator." (49:6-49:47, 871-912)
│ └─1 blockTag[2] (51:6-51:72, 923-989)
│ │ tag: "@param"
│ ├─0 typeExpression "number" (51:13-51:21, 930-938)
│ └─1 text "[max=Number.MAX_SAFE_INTEGER] - Max sequence value" (51:22-51:72, 939-989)
├─6 comment[4] (58:3-65:6, 1122-1259)
│ │ code: null
│ ├─0 description[1] (59:6-59:24, 1131-1149)
│ │ └─0 paragraph[1] (59:6-59:24, 1131-1149)
│ │ └─0 text "Iterable protocol." (59:6-59:24, 1131-1149)
│ ├─1 blockTag
│ │ tag: "@public"
│ ├─2 blockTag
│ │ tag: "@instance"
│ └─3 blockTag
│ │ tag: "@return"
│ ├─0 typeExpression "IterableIterator
│ └─1 text "Current sequence iterator" (64:41-64:66, 1228-1253)
├─7 comment[4] (70:3-77:6, 1340-1504)
│ │ code: null
│ ├─0 description[1] (71:6-71:51, 1349-1394)
│ │ └─0 paragraph[1] (71:6-71:51, 1349-1394)
│ │ └─0 text "Get the next value in the fibonacci sequence." (71:6-71:51, 1349-1394)
│ ├─1 blockTag
│ │ tag: "@public"
│ ├─2 blockTag
│ │ tag: "@instance"
│ └─3 blockTag
│ │ tag: "@return"
│ ├─0 typeExpression "IteratorResult
│ └─1 text "Next sequence value" (76:47-76:66, 1479-1498)
└─8 comment[2] (79:5-83:8, 1559-1639)
│ code: null
├─0 description[1] (80:8-80:33, 1570-1595)
│ └─0 paragraph[1] (80:8-80:33, 1570-1595)
│ └─0 text "Temporary sequence value." (80:8-80:33, 1570-1595)
└─1 blockTag
│ tag: "@const"
├─0 typeExpression "number" (82:15-82:23, 1617-1625)
└─1 text "value" (82:24-82:29, 1626-1631)
This package exports the identifier fromDocs. There is no default export.
Turn docblocks into a syntax tree.
#### Parameters
- value ([VFile][vfile] | string) — source document or file containing docblocks to parseoptions
- (Options, optional) — configuration options
#### Returns
docast tree ([Root][docast-tree])
Configuration options (TypeScript type).
#### Properties
- codeblocks (OneOrMany, optional) — block tag node names and tags, or regularCode
expressions, matching block tags that should have their text converted to [][mdast-code] when parsed as markdown'example'
- default: mdastExtensions
- ([MdastExtension[]][mdast-util-extension], optional) — markdown extensions to change howmicromarkExtensions
micromark tokens are converted to nodes
- ([MicromarkExtension[]][micromark-extension], optional) — micromark extensions to changetransforms
how markdown is parsed
- ([Transform[]](#transform), optional) — tree transforms
Change the AST after parsing is complete (TypeScript type).
#### Parameters
- tree ([Root][docast-tree]) — tree to transform
#### Returns
Nothing
TODO: docblock syntax
Markdown is parsed according to CommonMark. Extensions can add support for other syntax and nodes. If you’re interested
in extending markdown, more information is available in the [mdast-util-from-markdown][mdast-util-from-markdown] andmicromark
[][micromark] readmes.
The syntax tree is [docast][docast].
This package is fully typed with [TypeScript][typescript].
See CONTRIBUTING.md`.
[docast-parse]: https://github.com/flex-development/docast-parse
[docast-tree]: https://github.com/flex-development/docast#root
[docast]: https://github.com/flex-development/docast
[docblock]: https://github.com/flex-development/docast#docblock-comment
[esm]: https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c
[mdast-code]: https://github.com/syntax-tree/mdast#code
[mdast-util-extension]: https://github.com/syntax-tree/mdast-util-from-markdown#extension
[mdast-util-from-markdown]: https://github.com/syntax-tree/mdast-util-from-markdown
[micromark-extension]: https://github.com/micromark/micromark#extensions
[micromark]: https://github.com/micromark/micromark
[typescript]: https://www.typescriptlang.org
[unified]: https://github.com/unifiedjs/unified
[vfile]: https://github.com/vfile/vfile#api