Skip to content

Commit d1d95a1

Browse files
committed
Remove support for file as parameter
Please pass it in `options` instead.
1 parent 9910e6b commit d1d95a1

File tree

2 files changed

+60
-97
lines changed

2 files changed

+60
-97
lines changed

lib/index.js

Lines changed: 59 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,13 @@
2828
*
2929
* @typedef Options
3030
* Configuration.
31-
* @property {Array<string>} [passThrough]
31+
* @property {Array<string> | null | undefined} [passThrough]
3232
* List of custom hast node types to pass through (keep) in hast.
3333
*
3434
* If the passed through nodes have children, those children are expected to
35-
* be hast and will be handled.
35+
* be hast again and will be handled.
36+
* @property {VFile | null | undefined} [file]
37+
* Corresponding virtual file.
3638
*
3739
* @typedef State
3840
* Info passed around about the current state.
@@ -42,8 +44,6 @@
4244
* Add a hast node to the parser.
4345
* @property {boolean} stitches
4446
* Whether there are stitches.
45-
* @property {VFile | undefined} file
46-
* Virtual file.
4747
* @property {Options} options
4848
* User configuration.
4949
*/
@@ -61,88 +61,67 @@ import {zwitch} from 'zwitch'
6161
const parseOptions = {sourceCodeLocationInfo: true, scriptingEnabled: false}
6262

6363
/**
64-
* Given a hast tree and an optional vfile (for positional info), return a new
65-
* parsed-again hast tree.
64+
* Pass a hast tree through an HTML parser, which will fix nesting, and
65+
* turn raw nodes into actual nodes.
6666
*
67-
* @param tree
68-
* Original hast tree.
69-
* @param file
70-
* Virtual file for positional info, optional.
71-
* @param options
67+
* @param {Node} tree
68+
* Original hast tree to transform.
69+
* @param {Options | null | undefined} [options]
7270
* Configuration.
71+
* @returns {Node}
72+
* Parsed again tree.
7373
*/
74-
// To do: remove support for overload.
75-
export const raw =
76-
/**
77-
* @type {(
78-
* ((tree: Node, file: VFile | null | undefined, options?: Options) => Node) &
79-
* ((tree: Node, options?: Options) => Node)
80-
* )}
81-
*/
82-
(
83-
/**
84-
* @param {Node} tree
85-
* @param {VFile | Options | null | undefined} [file]
86-
* @param {Options | null | undefined} [options]
87-
*/
88-
function (tree, file, options) {
89-
if (isOptions(file)) {
90-
options = file
91-
file = undefined
92-
}
93-
94-
const document = documentMode(tree)
95-
/** @type {(node: Node, state: State) => void} */
96-
const one = zwitch('type', {
97-
handlers: {root, element, text, comment, doctype, raw: handleRaw},
98-
unknown
99-
})
100-
101-
/** @type {State} */
102-
const state = {
103-
parser: document
104-
? new Parser(parseOptions)
105-
: Parser.getFragmentParser(null, parseOptions),
106-
handle(node) {
107-
one(node, state)
108-
},
109-
stitches: false,
110-
file: file || undefined,
111-
options: options || {}
112-
}
74+
export function raw(tree, options) {
75+
const document = documentMode(tree)
76+
/** @type {(node: Node, state: State) => void} */
77+
const one = zwitch('type', {
78+
handlers: {root, element, text, comment, doctype, raw: handleRaw},
79+
unknown
80+
})
81+
82+
/** @type {State} */
83+
const state = {
84+
parser: document
85+
? new Parser(parseOptions)
86+
: Parser.getFragmentParser(null, parseOptions),
87+
handle(node) {
88+
one(node, state)
89+
},
90+
stitches: false,
91+
options: options || {}
92+
}
11393

114-
one(tree, state)
115-
resetTokenizer(state, pointStart())
116-
117-
const p5 = document ? state.parser.document : state.parser.getFragment()
118-
const result = fromParse5(p5, {
119-
// To do: support `space`?
120-
file
121-
})
122-
123-
if (state.stitches) {
124-
visit(result, 'comment', (node, index, parent) => {
125-
const stitch = /** @type {Stitch} */ (/** @type {unknown} */ (node))
126-
if (stitch.value.stitch && parent !== null && index !== null) {
127-
// @ts-expect-error: assume the stitch is allowed.
128-
parent.children[index] = stitch.value.stitch
129-
return index
130-
}
131-
})
94+
one(tree, state)
95+
resetTokenizer(state, pointStart())
96+
97+
const p5 = document ? state.parser.document : state.parser.getFragment()
98+
const result = fromParse5(p5, {
99+
// To do: support `space`?
100+
file: state.options.file
101+
})
102+
103+
if (state.stitches) {
104+
visit(result, 'comment', (node, index, parent) => {
105+
const stitch = /** @type {Stitch} */ (/** @type {unknown} */ (node))
106+
if (stitch.value.stitch && parent !== null && index !== null) {
107+
// @ts-expect-error: assume the stitch is allowed.
108+
parent.children[index] = stitch.value.stitch
109+
return index
132110
}
111+
})
112+
}
133113

134-
// Unpack if possible and when not given a `root`.
135-
if (
136-
tree.type !== 'root' &&
137-
result.type === 'root' &&
138-
result.children.length === 1
139-
) {
140-
return result.children[0]
141-
}
114+
// Unpack if possible and when not given a `root`.
115+
if (
116+
tree.type !== 'root' &&
117+
result.type === 'root' &&
118+
result.children.length === 1
119+
) {
120+
return result.children[0]
121+
}
142122

143-
return result
144-
}
145-
)
123+
return result
124+
}
146125

147126
/**
148127
* Transform all nodes
@@ -288,11 +267,7 @@ function stitch(node, state) {
288267
// Recurse, because to somewhat handle `[<x>]</x>` (where `[]` denotes the
289268
// passed through node).
290269
if ('children' in node && 'children' in clone) {
291-
const fakeRoot = raw(
292-
{type: 'root', children: node.children},
293-
state.file,
294-
state.options
295-
)
270+
const fakeRoot = raw({type: 'root', children: node.children}, state.options)
296271
// @ts-expect-error Assume a given parent yields a parent.
297272
clone.children = fakeRoot.children
298273
}
@@ -626,18 +601,6 @@ function createParse5Location(node) {
626601
}
627602
}
628603

629-
/**
630-
* Check if `value` is an options object.
631-
*
632-
* @param {VFile | Options | null | undefined} value
633-
* Thing.
634-
* @return {value is Options}
635-
* Whether `value` is an options object.
636-
*/
637-
function isOptions(value) {
638-
return Boolean(value && !('message' in value && 'messages' in value))
639-
}
640-
641604
/**
642605
* @template {Node} NodeType
643606
* Node type.

test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ test('integration', () => {
337337
const mdast = fromMarkdown(doc)
338338
const hast = toHast(mdast, {allowDangerousHtml: true})
339339
assert(hast, 'should transform to hast')
340-
const hast2 = raw(hast, new VFile(doc))
340+
const hast2 = raw(hast, {file: new VFile(doc)})
341341

342342
assert.deepEqual(
343343
hast2,

0 commit comments

Comments
 (0)