Skip to content

Commit 9fd2dfa

Browse files
committed
Refactor some code
1 parent c13fe7f commit 9fd2dfa

File tree

5 files changed

+98
-111
lines changed

5 files changed

+98
-111
lines changed

lib/revert.js

+3-9
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,17 @@
77
* @typedef {import('./state.js').State} State
88
*/
99

10-
/**
11-
* @typedef {Extract<Nodes, Reference>} References
12-
*/
13-
1410
// Make VS Code show references to the above types.
1511
''
1612

17-
// To do: next major: always return array.
18-
1913
/**
2014
* Return the content of a reference without definition as plain text.
2115
*
2216
* @param {State} state
2317
* Info passed around.
24-
* @param {References} node
18+
* @param {Extract<Nodes, Reference>} node
2519
* Reference node (image, link).
26-
* @returns {Array<ElementContent> | ElementContent}
20+
* @returns {Array<ElementContent>}
2721
* hast content.
2822
*/
2923
export function revert(state, node) {
@@ -37,7 +31,7 @@ export function revert(state, node) {
3731
}
3832

3933
if (node.type === 'imageReference') {
40-
return {type: 'text', value: '![' + node.alt + suffix}
34+
return [{type: 'text', value: '![' + node.alt + suffix}]
4135
}
4236

4337
const contents = state.all(node)

test/core.js

+84-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,20 @@
1+
/**
2+
* @typedef {import('hast').Element} Element
3+
* @typedef {import('mdast').Paragraph} Paragraph
4+
* @typedef {import('mdast').Nodes} Nodes
5+
*/
6+
17
import assert from 'node:assert/strict'
28
import test from 'node:test'
39
import {h} from 'hastscript'
410
import {toHast} from '../index.js'
5-
import * as mod from '../index.js'
611

712
test('toHast', async function (t) {
813
await t.test('should expose the public api', async function () {
9-
assert.deepEqual(Object.keys(mod).sort(), ['defaultHandlers', 'toHast'])
14+
assert.deepEqual(Object.keys(await import('../index.js')).sort(), [
15+
'defaultHandlers',
16+
'toHast'
17+
])
1018
})
1119

1220
await t.test('should throw on non-nodes', async function () {
@@ -241,4 +249,78 @@ test('toHast', async function (t) {
241249
)
242250
}
243251
)
252+
253+
/** @type {Paragraph} */
254+
const customMdast = {
255+
type: 'paragraph',
256+
children: [
257+
// @ts-expect-error: check how a custom literal is handled.
258+
{type: 'a', value: 'alpha'},
259+
// @ts-expect-error: check how a custom parent is handled.
260+
{type: 'b', children: [{type: 'image', url: 'bravo'}]},
261+
{type: 'text', value: 'charlie'}
262+
]
263+
}
264+
265+
await t.test('should override default handlers', async function () {
266+
assert.deepEqual(
267+
toHast(
268+
{type: 'paragraph', children: [{type: 'text', value: 'alpha'}]},
269+
{
270+
handlers: {
271+
paragraph(h, /** @type {Paragraph} */ node) {
272+
/** @type {Element} */
273+
const result = {
274+
type: 'element',
275+
tagName: 'p',
276+
properties: {},
277+
children: [{type: 'text', value: 'bravo'}]
278+
}
279+
h.patch(node, result)
280+
return h.applyData(node, result)
281+
}
282+
}
283+
}
284+
),
285+
h('p', 'bravo')
286+
)
287+
})
288+
289+
await t.test('should support unknown nodes by default', async function () {
290+
assert.deepEqual(
291+
toHast(customMdast),
292+
h('p', ['alpha', h('div', [h('img', {src: 'bravo'})]), 'charlie'])
293+
)
294+
})
295+
296+
await t.test('should support `unknownHandler`', async function () {
297+
assert.deepEqual(
298+
toHast(customMdast, {
299+
// To do: improved test.
300+
// @ts-expect-error `hast` expected, but this returns unknown mdast nodes.
301+
unknownHandler(_, /** @type {Nodes} */ node) {
302+
return node
303+
}
304+
}),
305+
h('p', [
306+
{type: 'a', value: 'alpha'},
307+
// To do: register custom?
308+
// @ts-expect-error: custom.
309+
{type: 'b', children: [{type: 'image', url: 'bravo'}]},
310+
'charlie'
311+
])
312+
)
313+
})
314+
315+
await t.test('should support `passThrough`', async function () {
316+
assert.deepEqual(
317+
toHast(customMdast, {passThrough: ['a', 'b']}),
318+
h('p', [
319+
{type: 'a', value: 'alpha'},
320+
// @ts-expect-error: custom.
321+
{type: 'b', children: [h('img', {src: 'bravo'})]},
322+
'charlie'
323+
])
324+
)
325+
})
244326
})

test/handlers-option.js

-90
This file was deleted.

test/image-reference.js

+10-8
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,13 @@ test('imageReference', async function (t) {
88
'should fall back on `imageReference`s without definition',
99
async function () {
1010
assert.deepEqual(
11-
toHast(
12-
// @ts-expect-error: check how missing `referenceType` is handled.
13-
{type: 'imageReference', identifier: 'alpha', alt: 'bravo'}
14-
),
15-
{type: 'text', value: '![bravo]'}
11+
toHast({
12+
type: 'imageReference',
13+
referenceType: 'shortcut',
14+
identifier: 'alpha',
15+
alt: 'bravo'
16+
}),
17+
{type: 'root', children: [{type: 'text', value: '![bravo]'}]}
1618
)
1719
}
1820
)
@@ -25,7 +27,7 @@ test('imageReference', async function (t) {
2527
referenceType: 'full',
2628
alt: 'delta'
2729
}),
28-
{type: 'text', value: '![delta][charlie]'}
30+
{type: 'root', children: [{type: 'text', value: '![delta][charlie]'}]}
2931
)
3032
})
3133

@@ -39,7 +41,7 @@ test('imageReference', async function (t) {
3941
referenceType: 'collapsed',
4042
alt: 'foxtrot'
4143
}),
42-
{type: 'text', value: '![foxtrot][]'}
44+
{type: 'root', children: [{type: 'text', value: '![foxtrot][]'}]}
4345
)
4446
}
4547
)
@@ -89,7 +91,7 @@ test('imageReference', async function (t) {
8991
referenceType: 'full',
9092
alt: 'mike'
9193
}),
92-
{type: 'text', value: '![mike][Lima]'}
94+
{type: 'root', children: [{type: 'text', value: '![mike][Lima]'}]}
9395
)
9496
}
9597
)

test/index.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
/* eslint-disable import/no-unassigned-import */
2-
import './core.js'
32
import './blockquote.js'
43
import './break.js'
54
import './code.js'
5+
import './core.js'
66
import './definition.js'
77
import './delete.js'
88
import './emphasis.js'
@@ -26,5 +26,4 @@ import './text.js'
2626
import './thematic-break.js'
2727
import './toml.js'
2828
import './yaml.js'
29-
import './handlers-option.js'
3029
/* eslint-enable import/no-unassigned-import */

0 commit comments

Comments
 (0)