Skip to content

Commit 39fd2bf

Browse files
committed
Change types to use mdast types
1 parent c930d3d commit 39fd2bf

16 files changed

+208
-149
lines changed

lib/footer.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ export function footer(h) {
4343
tail.children.push(backReference)
4444
} else {
4545
// @ts-expect-error Indeed, link directly added in block content.
46+
// Which we do because that way at least the handlers will be called
47+
// for the other HTML we’re generating (as markdown).
4648
content.push(backReference)
4749
}
4850

lib/handlers/break.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/**
2-
* @typedef {import('unist').Node} Node
2+
* @typedef {import('hast').Element} Element
3+
* @typedef {import('hast').Text} Text
34
* @typedef {import('mdast').Break} Break
45
* @typedef {import('../index.js').Handler} Handler
56
*/
@@ -9,7 +10,7 @@ import {u} from 'unist-builder'
910
/**
1011
* @type {Handler}
1112
* @param {Break} node
12-
* @returns {Array.<Node>}
13+
* @returns {Array<Element|Text>}
1314
*/
1415
export function hardBreak(h, node) {
1516
return [h(node, 'br'), u('text', '\n')]

lib/handlers/list.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,8 @@ export function list(h, node) {
3131
if (
3232
item.type === 'element' &&
3333
item.tagName === 'li' &&
34-
// @ts-expect-error looks like properties.
35-
item.properties.className &&
36-
// @ts-expect-error looks like properties.
34+
item.properties &&
35+
Array.isArray(item.properties.className) &&
3736
item.properties.className.includes('task-list-item')
3837
) {
3938
props.className = ['contains-task-list']

lib/handlers/root.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,6 @@ import {wrap} from '../wrap.js'
1212
* @param {Root} node
1313
*/
1414
export function root(h, node) {
15-
// @ts-expect-error top-level is a root.
15+
// @ts-expect-error `root`s are also fine.
1616
return h.augment(node, u('root', wrap(all(h, node))))
1717
}

lib/index.js

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/**
2-
* @typedef {import('unist').Node} Node
3-
* @typedef {import('unist').Parent} Parent
2+
* @typedef {import('mdast').Root|import('mdast').Parent['children'][number]} MdastNode
3+
* @typedef {import('hast').Root|import('hast').Parent['children'][number]} HastNode
4+
* @typedef {import('mdast').Parent} Parent
45
* @typedef {import('mdast').Definition} Definition
56
* @typedef {import('mdast').FootnoteDefinition} FootnoteDefinition
67
* @typedef {import('hast').Properties} Properties
@@ -20,7 +21,7 @@
2021
*
2122
* @typedef {Object.<string, unknown> & EmbeddedHastFields} Data unist data with embedded hast fields
2223
*
23-
* @typedef {Node & {data?: Data}} NodeWithData unist node with embedded hast data
24+
* @typedef {MdastNode & {data?: Data}} NodeWithData unist node with embedded hast data
2425
*
2526
* @callback Handler
2627
* @param {H} h Handle context
@@ -29,14 +30,14 @@
2930
* @returns {Content|Array.<Content>|null|undefined} hast node
3031
*
3132
* @callback HFunctionProps
32-
* @param {Node|PositionLike|null|undefined} node mdast node or unist position
33+
* @param {MdastNode|PositionLike|null|undefined} node mdast node or unist position
3334
* @param {string} tagName HTML tag name
3435
* @param {Properties} props Properties
3536
* @param {Array.<Content>?} [children] hast content
3637
* @returns {Element}
3738
*
3839
* @callback HFunctionNoProps
39-
* @param {Node|PositionLike|null|undefined} node mdast node or unist position
40+
* @param {MdastNode|PositionLike|null|undefined} node mdast node or unist position
4041
* @param {string} tagName HTML tag name
4142
* @param {Array.<Content>?} [children] hast content
4243
* @returns {Element}
@@ -74,7 +75,7 @@ const own = {}.hasOwnProperty
7475

7576
/**
7677
* Factory to transform.
77-
* @param {Node} tree mdast node
78+
* @param {MdastNode} tree mdast node
7879
* @param {Options} [options] Configuration
7980
* @returns {H} `h` function
8081
*/
@@ -85,7 +86,6 @@ function factory(tree, options) {
8586
const footnoteById = {}
8687

8788
h.dangerous = dangerous
88-
// @ts-expect-error: too loose.
8989
h.definition = definitions(tree)
9090
h.footnoteById = footnoteById
9191
/** @type {Array.<string>} */
@@ -182,9 +182,9 @@ function factory(tree, options) {
182182
/**
183183
* Transform `tree` (an mdast node) to a hast node.
184184
*
185-
* @param {Node} tree mdast node
185+
* @param {MdastNode} tree mdast node
186186
* @param {Options} [options] Configuration
187-
* @returns {Node} hast node
187+
* @returns {HastNode|null|undefined} hast node
188188
*/
189189
export function toHast(tree, options) {
190190
const h = factory(tree, options)
@@ -198,6 +198,5 @@ export function toHast(tree, options) {
198198
node.children.push(u('text', '\n'), foot)
199199
}
200200

201-
// @ts-expect-error: too loose.
202201
return Array.isArray(node) ? {type: 'root', children: node} : node
203202
}

lib/traverse.js

Lines changed: 38 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
/**
2-
* @typedef {import('unist').Node} Node
3-
* @typedef {import('unist').Parent} Parent
4-
* @typedef {import('mdast').Literal} Literal
5-
* @typedef {import('mdast').Text} Text
2+
* @typedef {import('mdast').Root|import('mdast').Parent['children'][number]} MdastNode
63
* @typedef {import('./index.js').H} H
74
* @typedef {import('./index.js').Handler} Handler
85
* @typedef {import('./index.js').Content} Content
@@ -15,10 +12,19 @@ const own = {}.hasOwnProperty
1512
/**
1613
* Transform an unknown node.
1714
* @type {Handler}
18-
* @param {Node} node
15+
* @param {MdastNode} node
1916
*/
2017
function unknown(h, node) {
21-
if (text(node)) {
18+
const data = node.data || {}
19+
20+
if (
21+
'value' in node &&
22+
!(
23+
own.call(data, 'hName') ||
24+
own.call(data, 'hProperties') ||
25+
own.call(data, 'hChildren')
26+
)
27+
) {
2228
return h.augment(node, u('text', node.value))
2329
}
2430

@@ -27,7 +33,7 @@ function unknown(h, node) {
2733

2834
/**
2935
* @type {Handler}
30-
* @param {Node} node
36+
* @param {MdastNode} node
3137
*/
3238
export function one(h, node, parent) {
3339
const type = node && node.type
@@ -50,69 +56,50 @@ export function one(h, node, parent) {
5056
return (typeof fn === 'function' ? fn : unknown)(h, node, parent)
5157
}
5258

53-
/**
54-
* Check if the node should be renderered as a text node.
55-
* @param {Node} node
56-
* @returns {node is Literal}
57-
*/
58-
function text(node) {
59-
const data = node.data || {}
60-
61-
if (
62-
own.call(data, 'hName') ||
63-
own.call(data, 'hProperties') ||
64-
own.call(data, 'hChildren')
65-
) {
66-
return false
67-
}
68-
69-
return 'value' in node
70-
}
71-
7259
/**
7360
* @type {Handler}
74-
* @param {unknown} node
61+
* @param {MdastNode} node
7562
*/
7663
function returnNode(h, node) {
77-
// @ts-expect-error pass through any unknown node.
78-
return node.children ? {...node, children: all(h, node)} : node
64+
// @ts-expect-error: Pass through custom node.
65+
return 'children' in node ? {...node, children: all(h, node)} : node
7966
}
8067

8168
/**
8269
* @param {H} h
83-
* @param {Node} parent
70+
* @param {MdastNode} parent
8471
*/
8572
export function all(h, parent) {
86-
/** @type {Array.<Node>} */
87-
// @ts-expect-error looks like a parent.
88-
const nodes = parent.children || []
8973
/** @type {Array.<Content>} */
9074
const values = []
91-
let index = -1
9275

93-
while (++index < nodes.length) {
94-
// @ts-expect-error looks like a parent.
95-
const result = one(h, nodes[index], parent)
76+
if ('children' in parent) {
77+
const nodes = parent.children
78+
let index = -1
9679

97-
if (result) {
98-
if (index && nodes[index - 1].type === 'break') {
99-
if (!Array.isArray(result) && result.type === 'text') {
100-
result.value = result.value.replace(/^\s+/, '')
101-
}
80+
while (++index < nodes.length) {
81+
const result = one(h, nodes[index], parent)
10282

103-
if (!Array.isArray(result) && result.type === 'element') {
104-
const head = result.children[0]
83+
if (result) {
84+
if (index && nodes[index - 1].type === 'break') {
85+
if (!Array.isArray(result) && result.type === 'text') {
86+
result.value = result.value.replace(/^\s+/, '')
87+
}
88+
89+
if (!Array.isArray(result) && result.type === 'element') {
90+
const head = result.children[0]
10591

106-
if (head && head.type === 'text') {
107-
head.value = head.value.replace(/^\s+/, '')
92+
if (head && head.type === 'text') {
93+
head.value = head.value.replace(/^\s+/, '')
94+
}
10895
}
10996
}
110-
}
11197

112-
if (Array.isArray(result)) {
113-
values.push(...result)
114-
} else {
115-
values.push(result)
98+
if (Array.isArray(result)) {
99+
values.push(...result)
100+
} else {
101+
values.push(result)
102+
}
116103
}
117104
}
118105
}

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@
3737
"@types/hast": "^2.0.0",
3838
"@types/mdast": "^3.0.0",
3939
"@types/mdurl": "^1.0.0",
40-
"@types/unist": "^2.0.0",
4140
"mdast-util-definitions": "^5.0.0",
4241
"mdurl": "^1.0.0",
4342
"unist-builder": "^3.0.0",

test/core.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,25 +109,29 @@ test('toHast()', (t) => {
109109
)
110110

111111
t.deepEqual(
112+
// @ts-expect-error: custom node.
112113
toHast(u('foo', 'tango')),
113114
u('text', 'tango'),
114115
'should transform unknown texts to `text`'
115116
)
116117

117118
t.deepEqual(
119+
// @ts-expect-error: custom node.
118120
toHast(u('bar', [u('text', 'tango')])),
119121
u('element', {tagName: 'div', properties: {}}, [u('text', 'tango')]),
120122
'should transform unknown parents to `div`'
121123
)
122124

123125
t.deepEqual(
126+
// @ts-expect-error: custom node.
124127
toHast(u('bar')),
125128
u('element', {tagName: 'div', properties: {}}, []),
126129
'should transform unknown nodes to `div`'
127130
)
128131

129132
t.deepEqual(
130133
toHast(
134+
// @ts-expect-error: custom node.
131135
u(
132136
'foo',
133137
{
@@ -147,12 +151,14 @@ test('toHast()', (t) => {
147151
)
148152

149153
t.deepEqual(
154+
// @ts-expect-error: custom node.
150155
toHast(u('foo', {data: {hChildren: [u('text', 'tango')]}}, 'tango')),
151156
u('element', {tagName: 'div', properties: {}}, [u('text', 'tango')]),
152157
'should transform unknown nodes with `data.hChildren` only to `div`'
153158
)
154159

155160
t.deepEqual(
161+
// @ts-expect-error: custom node.
156162
toHast(u('foo', {data: {hProperties: {className: 'charlie'}}}, 'tango')),
157163
u('element', {tagName: 'div', properties: {className: 'charlie'}}, []),
158164
'should transform unknown nodes with `data.hProperties` only to a `element` node'

test/definition.js

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
/**
2+
* @typedef {import('mdast').Paragraph} Paragraph
3+
*/
4+
15
import test from 'tape'
26
import {u} from 'unist-builder'
37
import {toHast} from '../index.js'
@@ -17,11 +21,15 @@ test('Definition', (t) => {
1721

1822
t.deepEqual(
1923
toHast(
20-
u('paragraph', [
21-
u('linkReference', {identifier: 'alpha'}, [u('text', 'bravo')]),
22-
u('definition', {identifier: 'alpha', url: 'https://charlie.com'}),
23-
u('definition', {identifier: 'alpha', url: 'https://delta.com'})
24-
])
24+
/** @type {Paragraph} */ (
25+
u('paragraph', [
26+
u('linkReference', {identifier: 'alpha', referenceType: 'shortcut'}, [
27+
u('text', 'bravo')
28+
]),
29+
u('definition', {identifier: 'alpha', url: 'https://charlie.com'}),
30+
u('definition', {identifier: 'alpha', url: 'https://delta.com'})
31+
])
32+
)
2533
),
2634
u('element', {tagName: 'p', properties: {}}, [
2735
u('element', {tagName: 'a', properties: {href: 'https://charlie.com'}}, [

test/footnote-reference.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ test('FootnoteReference', (t) => {
4040
)
4141

4242
t.deepEqual(
43+
// @ts-expect-error: supposed to be string.
4344
toHast(u('footnoteReference', {identifier: 1})),
4445
u(
4546
'element',

test/handlers-option.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/**
22
* @typedef {import('mdast').Paragraph} Paragraph
3+
* @typedef {import('mdast').Root|import('mdast').Content} Node
34
*/
45

56
import assert from 'node:assert'
@@ -23,11 +24,13 @@ test('handlers option', (t) => {
2324
'should override default handler'
2425
)
2526

26-
const customMdast = u('paragraph', [
27-
u('custom', 'with value'),
28-
u('custom', [u('image', {url: 'with-children.png'})]),
29-
u('text', 'bravo')
30-
])
27+
const customMdast = /** @type {Paragraph} */ (
28+
u('paragraph', [
29+
u('custom', 'with value'),
30+
u('custom', [u('image', {url: 'with-children.png'})]),
31+
u('text', 'bravo')
32+
])
33+
)
3134

3235
t.deepEqual(
3336
toHast(customMdast, {}),
@@ -51,7 +54,7 @@ test('handlers option', (t) => {
5154
t.deepEqual(
5255
toHast(customMdast, {
5356
// @ts-expect-error `hast` expected, but this returns unknown mdast nodes.
54-
unknownHandler(_, /** @type {object} */ node) {
57+
unknownHandler(_, /** @type {Node} */ node) {
5558
return node
5659
}
5760
}),

test/heading.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
1+
/**
2+
* @typedef {import('mdast').Heading} Heading
3+
*/
4+
15
import test from 'tape'
26
import {u} from 'unist-builder'
37
import {toHast} from '../index.js'
48

59
test('Heading', (t) => {
610
t.deepEqual(
7-
toHast(u('heading', {depth: 4}, [u('text', 'echo')])),
11+
toHast(
12+
/** @type {Heading} */ (u('heading', {depth: 4}, [u('text', 'echo')]))
13+
),
814
u('element', {tagName: 'h4', properties: {}}, [u('text', 'echo')]),
915
'should transform `heading` to a `h[1-6]` element'
1016
)

0 commit comments

Comments
 (0)