Skip to content

Commit d0d2278

Browse files
Refactor to improve perf w/ pre-declared positions
Closes GH-9. Reviewed-by: Titus Wormer <tituswormer@gmail.com>
1 parent abf8c36 commit d0d2278

1 file changed

Lines changed: 9 additions & 6 deletions

File tree

lib/index.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,17 @@ export function gfmTableFromMarkdown() {
6565
function enterTable(token) {
6666
const align = token._align
6767
assert(align, 'expected `_align` on table')
68+
// Trailing `position: undefined` keeps the table hidden class stable;
69+
// mdast-util-from-markdown's enter() patches the field to a real value
70+
// but the property already exists, so no shape transition fires.
6871
this.enter(
6972
{
7073
type: 'table',
7174
align: align.map(function (d) {
7275
return d === 'none' ? null : d
7376
}),
74-
children: []
77+
children: [],
78+
position: undefined
7579
},
7680
token
7781
)
@@ -92,7 +96,8 @@ function exitTable(token) {
9296
* @type {FromMarkdownHandle}
9397
*/
9498
function enterRow(token) {
95-
this.enter({type: 'tableRow', children: []}, token)
99+
// See enterTable above for the rationale on the trailing position field.
100+
this.enter({type: 'tableRow', children: [], position: undefined}, token)
96101
}
97102

98103
/**
@@ -108,7 +113,8 @@ function exit(token) {
108113
* @type {FromMarkdownHandle}
109114
*/
110115
function enterCell(token) {
111-
this.enter({type: 'tableCell', children: []}, token)
116+
// See enterTable above for the rationale on the trailing position field.
117+
this.enter({type: 'tableCell', children: [], position: undefined}, token)
112118
}
113119

114120
// Overwrite the default code text data handler to unescape escaped pipes when
@@ -230,11 +236,8 @@ export function gfmTableToMarkdown(options) {
230236
function serializeData(matrix, align) {
231237
return markdownTable(matrix, {
232238
align,
233-
// @ts-expect-error: `markdown-table` types should support `null`.
234239
alignDelimiters,
235-
// @ts-expect-error: `markdown-table` types should support `null`.
236240
padding,
237-
// @ts-expect-error: `markdown-table` types should support `null`.
238241
stringLength
239242
})
240243
}

0 commit comments

Comments
 (0)