Skip to content

Commit 5a22dd3

Browse files
committed
Fix to clone result
1 parent b319844 commit 5a22dd3

File tree

2 files changed

+6
-8
lines changed

2 files changed

+6
-8
lines changed

lib/index.js

+4-8
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
// To do next major: use `structuredClone` (or so?) to deep clone `properties`
2-
// and the like: the return value has to be a clone (not shallow copy) of the
3-
// passed tree.
4-
51
/**
62
* @typedef {import('hast').Nodes} Nodes
73
* @typedef {import('hast').RootContent} RootContent
@@ -36,6 +32,7 @@
3632
* Number of characters to truncate to (default: `140`).
3733
*/
3834

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

4138
/** @type {ReadonlyArray<RootContent>} */
@@ -109,7 +106,7 @@ export function truncate(tree, options) {
109106
}
110107
}
111108

112-
return result
109+
return structuredClone(result)
113110

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

143140
if (!ignore.includes(child)) {
144-
const result = preorder(child)
145-
// @ts-expect-error: assume content matches.
141+
const result = /** @type {RootContent} */ (preorder(child))
146142
if (result) children.push(result)
147143
}
148144

@@ -152,7 +148,7 @@ export function truncate(tree, options) {
152148
}
153149
}
154150

155-
// @ts-expect-error: assume content matches.
151+
// @ts-expect-error: content model matches.
156152
replacement.children = children
157153
}
158154

package.json

+2
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,12 @@
3535
],
3636
"dependencies": {
3737
"@types/hast": "^3.0.0",
38+
"@ungap/structured-clone": "^1.0.0",
3839
"micromark-util-character": "^2.0.0"
3940
},
4041
"devDependencies": {
4142
"@types/node": "^20.0.0",
43+
"@types/ungap__structured-clone": "^0.3.0",
4244
"c8": "^8.0.0",
4345
"hast-util-select": "^6.0.0",
4446
"hastscript": "^8.0.0",

0 commit comments

Comments
 (0)