Skip to content

Commit 8dfb4ac

Browse files
committed
Change types to use mdast types
1 parent fee25c2 commit 8dfb4ac

File tree

3 files changed

+32
-36
lines changed

3 files changed

+32
-36
lines changed

index.js

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,22 @@
11
/**
2-
* @typedef {import('unist').Node} Node
3-
* @typedef {import('unist-util-remove').TestFunctionAnything} TestFunction
2+
* @typedef {import('mdast').Root|import('mdast').Content} Node
43
*/
54

65
import {remove} from 'unist-util-remove'
76

87
/**
98
* @template {Node} T
109
* @param {T} tree
11-
* @returns {T}
10+
* @returns {T|null}
1211
*/
1312
export function squeezeParagraphs(tree) {
14-
return remove(tree, {cascade: false}, isEmptyParagraph)
15-
}
16-
17-
/**
18-
* Whether paragraph is empty or composed only of whitespace.
19-
*
20-
* @type {TestFunction}
21-
*/
22-
function isEmptyParagraph(node) {
23-
return (
24-
node.type === 'paragraph' &&
25-
// @ts-expect-error paragraphs are parents.
26-
node.children.every(
27-
(/** @type {Node} */ node) =>
28-
// @ts-expect-error texts are literals.
29-
node.type === 'text' && /^\s*$/.test(node.value)
13+
return remove(tree, {cascade: false}, (node) =>
14+
Boolean(
15+
node.type === 'paragraph' &&
16+
node.children.every(
17+
(/** @type {Node} */ node) =>
18+
node.type === 'text' && /^\s*$/.test(node.value)
19+
)
3020
)
3121
)
3222
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
"index.js"
4040
],
4141
"dependencies": {
42-
"@types/unist": "^2.0.0",
42+
"@types/mdast": "^3.0.0",
4343
"unist-util-remove": "^3.0.0"
4444
},
4545
"devDependencies": {

test.js

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,34 @@
1+
/**
2+
* @typedef {import('mdast').Root} Root
3+
*/
4+
15
import test from 'tape'
26
import {u} from 'unist-builder'
37
import {squeezeParagraphs} from './index.js'
48

59
test((t) => {
610
t.deepEqual(
711
squeezeParagraphs(
8-
u('root', [
9-
u('paragraph', []),
10-
u('paragraph', [u('text', 'first')]),
11-
u('paragraph', []),
12-
u('paragraph', [u('text', ''), u('text', ' \n')]),
13-
u('paragraph', [
14-
u('text', 'second'),
15-
u('text', ' '),
16-
u('text', 'value')
17-
]),
18-
u('paragraph', []),
19-
u('list', {ordered: false, start: null, loose: false}, [
20-
u('listItem', {loose: false}, [u('paragraph', [])]),
21-
u('listItem', {loose: false}, [
22-
u('paragraph', [u('text', ' '), u('text', ' ')])
12+
/** @type {Root} */ (
13+
u('root', [
14+
u('paragraph', []),
15+
u('paragraph', [u('text', 'first')]),
16+
u('paragraph', []),
17+
u('paragraph', [u('text', ''), u('text', ' \n')]),
18+
u('paragraph', [
19+
u('text', 'second'),
20+
u('text', ' '),
21+
u('text', 'value')
22+
]),
23+
u('paragraph', []),
24+
u('list', {ordered: false, start: null, loose: false}, [
25+
u('listItem', {loose: false}, [u('paragraph', [])]),
26+
u('listItem', {loose: false}, [
27+
u('paragraph', [u('text', ' '), u('text', ' ')])
28+
])
2329
])
2430
])
25-
])
31+
)
2632
),
2733
u('root', [
2834
u('paragraph', [u('text', 'first')]),

0 commit comments

Comments
 (0)