Skip to content

Commit 59ecd14

Browse files
committed
Add support for file in options
Related-to: remarkjs/remark#1272.
1 parent d37bb59 commit 59ecd14

File tree

4 files changed

+38
-5
lines changed

4 files changed

+38
-5
lines changed

lib/state.js

+4
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
* @typedef {import('mdast').Nodes} MdastNodes
1212
* @typedef {import('mdast').Parents} MdastParents
1313
*
14+
* @typedef {import('vfile').VFile} VFile
15+
*
1416
* @typedef {import('./footer.js').FootnoteBackContentTemplate} FootnoteBackContentTemplate
1517
* @typedef {import('./footer.js').FootnoteBackLabelTemplate} FootnoteBackLabelTemplate
1618
*/
@@ -56,6 +58,8 @@
5658
* at that place.
5759
* It can also break polyfills.
5860
* Using a prefix solves these problems.
61+
* @property {VFile | null | undefined} [file]
62+
* Corresponding virtual file representing the input document (optional).
5963
* @property {FootnoteBackContentTemplate | string | null | undefined} [footnoteBackContent]
6064
* Content of the backreference back to references (default: `defaultFootnoteBackContent`).
6165
*

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@
4040
"micromark-util-sanitize-uri": "^2.0.0",
4141
"trim-lines": "^3.0.0",
4242
"unist-util-position": "^5.0.0",
43-
"unist-util-visit": "^5.0.0"
43+
"unist-util-visit": "^5.0.0",
44+
"vfile": "^6.0.0"
4445
},
4546
"devDependencies": {
4647
"@types/node": "^20.0.0",

readme.md

+4
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,8 @@ Configuration (TypeScript type).
351351
* `clobberPrefix` (`string`, default: `'user-content-'`)
352352
— prefix to use before the `id` property on footnotes to prevent them from
353353
*clobbering*
354+
* `file` ([`VFile`][vfile], optional)
355+
— corresponding virtual file representing the input document
354356
* `footnoteBackContent`
355357
([`FootnoteBackContentTemplate`][api-footnote-back-content-template]
356358
or `string`, default:
@@ -1687,6 +1689,8 @@ abide by its terms.
16871689
16881690
[remark-rehype]: https://github.com/remarkjs/remark-rehype
16891691
1692+
[vfile]: https://github.com/vfile/vfile
1693+
16901694
[clobber-example]: https://github.com/rehypejs/rehype-sanitize#example-headings-dom-clobbering
16911695
16921696
[github-markdown-css]: https://github.com/sindresorhus/github-markdown-css

test/core.js

+28-4
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
import assert from 'node:assert/strict'
77
import test from 'node:test'
88
import {h} from 'hastscript'
9-
import {toHast} from 'mdast-util-to-hast'
9+
import {toHast, defaultHandlers} from 'mdast-util-to-hast'
10+
import {VFile} from 'vfile'
1011

1112
test('toHast', async function (t) {
1213
await t.test('should expose the public api', async function () {
@@ -272,16 +273,16 @@ test('toHast', async function (t) {
272273
{type: 'paragraph', children: [{type: 'text', value: 'alpha'}]},
273274
{
274275
handlers: {
275-
paragraph(h, /** @type {Paragraph} */ node) {
276+
paragraph(state, /** @type {Paragraph} */ node) {
276277
/** @type {Element} */
277278
const result = {
278279
type: 'element',
279280
tagName: 'p',
280281
properties: {},
281282
children: [{type: 'text', value: 'bravo'}]
282283
}
283-
h.patch(node, result)
284-
return h.applyData(node, result)
284+
state.patch(node, result)
285+
return state.applyData(node, result)
285286
}
286287
}
287288
}
@@ -290,6 +291,29 @@ test('toHast', async function (t) {
290291
)
291292
})
292293

294+
await t.test('should support files', async function () {
295+
const file = new VFile('alpha')
296+
297+
assert.deepEqual(
298+
toHast(
299+
{type: 'paragraph', children: [{type: 'text', value: 'alpha'}]},
300+
{
301+
file,
302+
handlers: {
303+
paragraph(state, /** @type {Paragraph} */ node) {
304+
assert(state.options.file)
305+
state.options.file.message('Warning!')
306+
return defaultHandlers.paragraph(state, node)
307+
}
308+
}
309+
}
310+
),
311+
h('p', 'alpha')
312+
)
313+
314+
assert.deepEqual(file.messages.map(String), ['1:1: Warning!'])
315+
})
316+
293317
await t.test('should support unknown nodes by default', async function () {
294318
assert.deepEqual(
295319
toHast(customMdast),

0 commit comments

Comments
 (0)