Skip to content

Commit

Permalink
Fix to clone result
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Aug 7, 2023
1 parent b319844 commit 5a22dd3
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 8 deletions.
12 changes: 4 additions & 8 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
// To do next major: use `structuredClone` (or so?) to deep clone `properties`
// and the like: the return value has to be a clone (not shallow copy) of the
// passed tree.

/**
* @typedef {import('hast').Nodes} Nodes
* @typedef {import('hast').RootContent} RootContent
Expand Down Expand Up @@ -36,6 +32,7 @@
* Number of characters to truncate to (default: `140`).
*/

import structuredClone from '@ungap/structured-clone'
import {unicodePunctuation, unicodeWhitespace} from 'micromark-util-character'

/** @type {ReadonlyArray<RootContent>} */
Expand Down Expand Up @@ -109,7 +106,7 @@ export function truncate(tree, options) {
}
}

return result
return structuredClone(result)

/**
* Transform in `preorder`.
Expand Down Expand Up @@ -141,8 +138,7 @@ export function truncate(tree, options) {
const child = node.children[index]

if (!ignore.includes(child)) {
const result = preorder(child)
// @ts-expect-error: assume content matches.
const result = /** @type {RootContent} */ (preorder(child))
if (result) children.push(result)
}

Expand All @@ -152,7 +148,7 @@ export function truncate(tree, options) {
}
}

// @ts-expect-error: assume content matches.
// @ts-expect-error: content model matches.
replacement.children = children
}

Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,12 @@
],
"dependencies": {
"@types/hast": "^3.0.0",
"@ungap/structured-clone": "^1.0.0",
"micromark-util-character": "^2.0.0"
},
"devDependencies": {
"@types/node": "^20.0.0",
"@types/ungap__structured-clone": "^0.3.0",
"c8": "^8.0.0",
"hast-util-select": "^6.0.0",
"hastscript": "^8.0.0",
Expand Down

0 comments on commit 5a22dd3

Please sign in to comment.