Skip to content

Commit

Permalink
Update unist-util-visit
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Jul 30, 2021
1 parent f662588 commit 42fe278
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 24 deletions.
1 change: 0 additions & 1 deletion lib/handlers/media.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ export function media(h, node) {
/** @type {ElementChild} */
let child

// @ts-expect-error: assume valid content.
visit({type: 'root', children: nodes}, 'link', findLink)

// If the content links to something, or if it’s not phrasing…
Expand Down
14 changes: 5 additions & 9 deletions lib/handlers/table.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,10 @@ function inspect(node) {
/** @type {Array.<string|null>} */
const align = [null]

visit(node, 'element', visitor)

return {align, headless}

/** @type {import('unist-util-visit').Visitor<Element>} */
function visitor(child) {
visit(node, 'element', (child) => {
// If there is a `thead`, assume there is a header row.
if (cell(child)) {
if (cell(child) && child.properties) {
if (!align[cellIndex]) {
// @ts-expect-error: `props` are defined.
align[cellIndex] = String(child.properties.align || '') || null
}

Expand All @@ -66,7 +60,9 @@ function inspect(node) {
rowIndex++
cellIndex = 0
}
}
})

return {align, headless}
}

/**
Expand Down
23 changes: 10 additions & 13 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
* @typedef {import('./types.js').MdastRoot} MdastRoot
*/

import {hasProperty} from 'hast-util-has-property'
// @ts-expect-error: next.
import minifyWhitespace from 'rehype-minify-whitespace'
import {convert} from 'unist-util-is'
Expand Down Expand Up @@ -97,7 +96,16 @@ export function toMdast(tree, options = {}) {
}
)

visit(tree, 'element', onelement)
visit(tree, 'element', (node) => {
const id =
node.properties &&
'id' in node.properties &&
String(node.properties.id).toUpperCase()

if (id && !own.call(byId, id)) {
byId[id] = node
}
})

minifyWhitespace({newlines: options.newlines === true})(tree)

Expand All @@ -115,17 +123,6 @@ export function toMdast(tree, options = {}) {

return mdast

/** @type {import('unist-util-visit').Visitor<Element>} */
function onelement(node) {
const id =
// @ts-expect-error: `hasProperty` just checked it.
hasProperty(node, 'id') && String(node.properties.id).toUpperCase()

if (id && !own.call(byId, id)) {
byId[id] = node
}
}

/**
* Collapse text nodes, and fix whitespace.
* Most of this is taken care of by `rehype-minify-whitespace`, but
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
"repeat-string": "^1.0.0",
"trim-trailing-lines": "^2.0.0",
"unist-util-is": "^5.0.0",
"unist-util-visit": "^3.0.0"
"unist-util-visit": "^4.0.0"
},
"devDependencies": {
"@types/tape": "^4.0.0",
Expand Down

0 comments on commit 42fe278

Please sign in to comment.