Skip to content

Commit 7a1c786

Browse files
committed
feat: pop the section stack based on heading difference
1 parent e333747 commit 7a1c786

File tree

4 files changed

+36
-6
lines changed

4 files changed

+36
-6
lines changed

dist/action.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19436,7 +19436,9 @@ function linkify_headings() {
1943619436
prev_section[prev_section.length - 1].sections || []
1943719437
);
1943819438
} else if (node.depth < data.prev_level) {
19439-
data.section_stack.pop();
19439+
for (let i = 0; i < data.prev_level - node.depth; i++) {
19440+
data.section_stack.pop();
19441+
}
1944019442
}
1944119443

1944219444
data.section_stack[data.section_stack.length - 1].push({

dist/cli.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18199,7 +18199,9 @@ function linkify_headings() {
1819918199
prev_section[prev_section.length - 1].sections || []
1820018200
);
1820118201
} else if (node.depth < data.prev_level) {
18202-
data.section_stack.pop();
18202+
for (let i = 0; i < data.prev_level - node.depth; i++) {
18203+
data.section_stack.pop();
18204+
}
1820318205
}
1820418206

1820518207
data.section_stack[data.section_stack.length - 1].push({

src/format/headings.test.ts

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,9 @@ _headings(
221221
222222
#### one
223223
224-
#### two
224+
##### two
225+
226+
### style
225227
`,
226228
data: {
227229
dir: "docs",
@@ -242,8 +244,31 @@ _headings(
242244
output.contents,
243245
`<h3><span id="component-format-script" class="offset-anchor"></span><a href="docs#component-format-script" class="anchor" aria-hidden></a>script</h3>
244246
<h4><span id="component-format-script-one" class="offset-anchor"></span><a href="docs#component-format-script-one" class="anchor" aria-hidden></a>one</h4>
245-
<h4><span id="component-format-script-two" class="offset-anchor"></span><a href="docs#component-format-script-two" class="anchor" aria-hidden></a>two</h4>`
247+
<h5><span id="component-format-script-one-two" class="offset-anchor" data-scrollignore></span><a href="docs#component-format-script-one-two" class="anchor" aria-hidden></a>two</h5>
248+
<h3><span id="component-format-style" class="offset-anchor"></span><a href="docs#component-format-style" class="anchor" aria-hidden></a>style</h3>`
246249
);
250+
251+
//@ts-ignore
252+
assert.equal(output.data.sections, [
253+
{
254+
slug: "component-format-script",
255+
title: "script",
256+
sections: [
257+
{
258+
slug: "component-format-script-one",
259+
title: "one",
260+
sections: [
261+
{
262+
slug: "component-format-script-one-two",
263+
title: "two",
264+
sections: [],
265+
},
266+
],
267+
},
268+
],
269+
},
270+
{ slug: "component-format-style", title: "style", sections: [] },
271+
]);
247272
}
248273
);
249274

src/format/headings.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,6 @@ export function linkify_headings(): Transformer {
9797
}
9898

9999
data.slugs.push(slug);
100-
console.log(data.slugs);
101100

102101
// We keep a 'section_stack' to keep track of the section structure
103102
if (node.depth > data.prev_level) {
@@ -108,7 +107,9 @@ export function linkify_headings(): Transformer {
108107
prev_section[prev_section.length - 1].sections || []
109108
);
110109
} else if (node.depth < data.prev_level) {
111-
data.section_stack.pop();
110+
for (let i = 0; i < data.prev_level - node.depth; i++) {
111+
data.section_stack.pop();
112+
}
112113
}
113114

114115
data.section_stack[data.section_stack.length - 1].push({

0 commit comments

Comments
 (0)