Skip to content

Commit d9b2ddd

Browse files
triaeiouwooorm
andauthored
Add support for inferring phrasing of embedded hast
Related-to: syntax-tree/unist#75. Closes GH-74. Reviewed-by: Titus Wormer <[email protected]> Co-authored-by: Titus Wormer <[email protected]>
1 parent bd47018 commit d9b2ddd

File tree

3 files changed

+46
-2
lines changed

3 files changed

+46
-2
lines changed

lib/util/wrap.js

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,23 @@
55
*/
66

77
import extend from 'extend'
8-
import {phrasing} from 'mdast-util-phrasing'
8+
import {phrasing as hastPhrasing} from 'hast-util-phrasing'
9+
import {phrasing as mdastPhrasing} from 'mdast-util-phrasing'
10+
11+
/**
12+
* @param {MdastNode} node
13+
* @returns {node is MdastPhrasingContent}
14+
*/
15+
function phrasing(node) {
16+
return node.data && node.data.hName
17+
? hastPhrasing({
18+
type: 'element',
19+
tagName: node.data.hName,
20+
properties: {},
21+
children: []
22+
})
23+
: mdastPhrasing(node)
24+
}
925

1026
/**
1127
* @param {Array.<MdastNode>} nodes
@@ -80,7 +96,7 @@ function runs(nodes, onphrasing, onnonphrasing) {
8096

8197
if (phrasing(node)) {
8298
if (!queue) queue = []
83-
queue.push(node)
99+
queue.push(/** @type {MdastPhrasingContent} */ (node))
84100
} else {
85101
if (queue) {
86102
result = result.concat(onphrasing(queue))

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
"hast-util-is-element": "^2.0.0",
4646
"hast-util-to-text": "^3.0.0",
4747
"mdast-util-phrasing": "^3.0.0",
48+
"hast-util-phrasing": "^2.0.1",
4849
"mdast-util-to-string": "^3.0.0",
4950
"rehype-minify-whitespace": "^5.0.0",
5051
"trim-trailing-lines": "^2.0.0",

test/index.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,36 @@ import remarkStringify from 'remark-stringify'
1919
import {assert} from 'mdast-util-assert'
2020
import {removePosition} from 'unist-util-remove-position'
2121
import {one, all, defaultHandlers, toMdast} from '../index.js'
22+
import {wrapNeeded} from '../lib/util/wrap.js'
2223

2324
const fixtures = path.join('test', 'fixtures')
2425

26+
test('custom nodes', (t) => {
27+
t.deepEqual(
28+
wrapNeeded([
29+
{type: 'text', value: 'some '},
30+
{
31+
// @ts-expect-error - custom node type
32+
type: 'superscript',
33+
data: {hName: 'sup'},
34+
children: [{type: 'text', value: 'test'}]
35+
},
36+
{type: 'text', value: ' text'}
37+
]),
38+
wrapNeeded([
39+
{type: 'text', value: 'some '},
40+
{
41+
type: 'emphasis',
42+
children: [{type: 'text', value: 'test'}]
43+
},
44+
{type: 'text', value: ' text'}
45+
]),
46+
'should support `node.data.hName` to infer phrasing'
47+
)
48+
49+
t.end()
50+
})
51+
2552
test('exports', (t) => {
2653
t.assert(one, 'should export `one`')
2754

0 commit comments

Comments
 (0)