-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
89 lines (83 loc) · 2.34 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/**
* @typedef {import('hast').Root} HastRoot
* @typedef {import('hast-util-to-nlcst').ParserConstructor} ParserConstructor
* @typedef {import('hast-util-to-nlcst').ParserInstance} ParserInstance
* @typedef {import('nlcst').Root} NlcstRoot
* @typedef {import('unified').Processor<NlcstRoot>} Processor
* @typedef {import('unified').Transformer<HastRoot>} TransformBridge
* @typedef {import('unified').Transformer<HastRoot, NlcstRoot>} TransformMutate
* @typedef {import('vfile').VFile} VFile
*/
/**
* @typedef {ParserConstructor | ParserInstance} ToNlcstParser
*/
import {toNlcst} from 'hast-util-to-nlcst'
/**
* Plugin to bridge or mutate to retext.
*
* If a destination processor is given, runs the destination with the new nlcst
* tree (bridge-mode).
* If a parser is given, returns the nlcst tree: further plugins run on that
* tree (mutate-mode).
*
* @overload
* @param {Processor} processor
* @returns {TransformBridge}
*
* @overload
* @param {ToNlcstParser} parser
* @returns {TransformMutate}
*
* @param {ToNlcstParser | Processor} options
* Parser or processor.
* @returns {TransformBridge | TransformMutate}
* Transform.
*/
export default function rehypeRetext(options) {
if (!options) {
throw new Error(
'Expected `parser` (such as from `parse-english`) or `processor` (a unified pipeline) as `options`'
)
}
if ('run' in options) {
/**
* Bridge-mode.
*
* Runs the destination with the new nlcst tree.
* Discards result.
*
* @param {HastRoot} tree
* Tree.
* @param {VFile} file
* File.
* @returns {Promise<undefined>}
* Nothing.
* @satisfies {TransformBridge}
*/
return async function (tree, file) {
const frozen = options.freeze()
// @ts-expect-error: assume the parser is a retext parser.
const parser = /** @type {ParserConstructor | ParserInstance} */ (
frozen.parser || frozen.Parser
)
const nlcstTree = toNlcst(tree, file, parser)
await options.run(nlcstTree, file)
}
}
/**
* Mutate-mode.
*
* Further transformers run on the nlcst tree.
*
* @param {HastRoot} tree
* Tree.
* @param {VFile} file
* File.
* @returns {NlcstRoot}
* Nothing.
* @satisfies {TransformMutate}
*/
return function (tree, file) {
return toNlcst(tree, file, options)
}
}