Skip to content

Add support for inferring phrasing of embedded hast #74

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Aug 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions lib/util/wrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,23 @@
*/

import extend from 'extend'
import {phrasing} from 'mdast-util-phrasing'
import {phrasing as hastPhrasing} from 'hast-util-phrasing'
import {phrasing as mdastPhrasing} from 'mdast-util-phrasing'

/**
* @param {MdastNode} node
* @returns {node is MdastPhrasingContent}
*/
function phrasing(node) {
return node.data && node.data.hName
? hastPhrasing({
type: 'element',
tagName: node.data.hName,
properties: {},
children: []
})
: mdastPhrasing(node)
}

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

if (phrasing(node)) {
if (!queue) queue = []
queue.push(node)
queue.push(/** @type {MdastPhrasingContent} */ (node))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe this should not be needed anymore. I’ll check myself.

} else {
if (queue) {
result = result.concat(onphrasing(queue))
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
"hast-util-is-element": "^2.0.0",
"hast-util-to-text": "^3.0.0",
"mdast-util-phrasing": "^3.0.0",
"hast-util-phrasing": "^2.0.1",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In unified, we prefer to keep loose ranges, if possible, so 2.0.0. It helps resolving packages by making the ranges loose. And it keeps package lock diffs cleaner.
I’ll fix this myself here, but just an FYI for the future!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, thanks. I'm learning as I go along with npm, package.json as well as git.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no problem, I’ll try to keep that mind, and do let me know when there’s something you’re wondering about in all this!

"mdast-util-to-string": "^3.0.0",
"rehype-minify-whitespace": "^5.0.0",
"trim-trailing-lines": "^2.0.0",
Expand Down
27 changes: 27 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,36 @@ import remarkStringify from 'remark-stringify'
import {assert} from 'mdast-util-assert'
import {removePosition} from 'unist-util-remove-position'
import {one, all, defaultHandlers, toMdast} from '../index.js'
import {wrapNeeded} from '../lib/util/wrap.js'

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

test('custom nodes', (t) => {
t.deepEqual(
wrapNeeded([
{type: 'text', value: 'some '},
{
// @ts-expect-error - custom node type
type: 'superscript',
data: {hName: 'sup'},
children: [{type: 'text', value: 'test'}]
},
{type: 'text', value: ' text'}
]),
wrapNeeded([
{type: 'text', value: 'some '},
{
type: 'emphasis',
children: [{type: 'text', value: 'test'}]
},
{type: 'text', value: ' text'}
]),
'should support `node.data.hName` to infer phrasing'
)

t.end()
})

test('exports', (t) => {
t.assert(one, 'should export `one`')

Expand Down