Skip to content

Commit 2a34159

Browse files
committed
fix(toc): set tocId to element.id
1 parent 3837d9b commit 2a34159

4 files changed

Lines changed: 41 additions & 29 deletions

File tree

.changeset/rotten-lemons-accept.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@svelte-put/toc': patch
3+
---
4+
5+
set extracted `tocId` to `element.id` (bugfix)

apps/docs/src/routes/(main)/docs/(pkg)/toc/_page/codes/quick-start-input.svelte

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
<main use:toc={{ store: tocStore, observe: true }}>
77
<h1>Page Heading</h1>
8+
89
<section>
910
<h2>Table of Contents</h2>
1011
{#if Object.values($tocStore.items).length}
@@ -17,11 +18,15 @@
1718
</ul>
1819
{/if}
1920
</section>
21+
22+
<section>
23+
<h2>Section Heading Level 2</h2>
24+
<p>...</p>
25+
</section>
26+
2027
<section>
21-
<h2>Section Heading</h2>
22-
<section>
23-
<h3>Nested Section Heading</h3>
24-
</section>
28+
<h3>Section Heading Level 3</h3>
29+
<p>...</p>
2530
</section>
2631
<!-- ... -->
2732
</main>
Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,36 @@
1-
<main>
2-
<h1 id="page-heading" data-toc="">
1+
<main data-toc-observe-for="page-heading">
2+
<h1 id="page-heading" data-toc="" class="">
33
<a aria-hidden="true" tabindex="-1" href="#page-heading" data-toc-anchor="">#</a>Page Heading
44
</h1>
5-
<section>
6-
<h2 id="table-of-contents" data-toc="">
5+
<section data-toc-observe-for="table-of-contents">
6+
<h2 id="table-of-contents" data-toc="" class="">
77
<a aria-hidden="true" tabindex="-1" href="#table-of-contents" data-toc-anchor="">#</a>Table of
88
Contents
99
</h2>
1010
<ul>
11-
<a href="#page-heading" style="display: contents;">
12-
<li>Page Heading</li>
13-
</a>
14-
<a href="#table-of-contents" style="display: contents;">
15-
<li>Table of Contents</li>
16-
</a>
17-
<a href="#section-heading" style="display: contents;">
18-
<li>Section Heading</li>
19-
</a>
20-
<a href="#nested-section-heading" style="display: contents;">
21-
<li>Nested Section Heading</li>
11+
<a href="#page-heading" style="display: contents;"><li class="">Page Heading</li> </a><a
12+
href="#table-of-contents"
13+
style="display: contents;"
14+
><li>Table of Contents</li>
15+
</a><a href="#section-heading-level-2" style="display: contents;"
16+
><li class="">Section Heading Level 2</li>
17+
</a><a href="#section-heading-level-3" class="active" style="display: contents;"
18+
><li>Section Heading Level 3</li>
2219
</a>
2320
</ul>
2421
</section>
25-
<section>
26-
<h2 id="section-heading" data-toc="">
27-
<a aria-hidden="true" tabindex="-1" href="#section-heading" data-toc-anchor="">#</a>Section
28-
Heading
22+
<section data-toc-observe-for="section-heading-level-2">
23+
<h2 id="section-heading-level-2" data-toc="" class="">
24+
<a aria-hidden="true" tabindex="-1" href="#section-heading-level-2" data-toc-anchor="">#</a
25+
>Section Heading Level 2
2926
</h2>
30-
<section>
31-
<h3 id="nested-section-heading" data-toc="">
32-
<a aria-hidden="true" tabindex="-1" href="#nested-section-heading" data-toc-anchor="">#</a
33-
>Nested Section Heading
34-
</h3>
35-
</section>
27+
<p>...</p>
28+
</section>
29+
<section data-toc-observe-for="section-heading-level-3">
30+
<h3 id="section-heading-level-3" data-toc="">
31+
<a aria-hidden="true" tabindex="-1" href="#section-heading-level-3" data-toc-anchor="">#</a
32+
>Section Heading Level 3
33+
</h3>
34+
<p>...</p>
3635
</section>
3736
</main>

packages/actions/toc/src/lib/toc.action.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,10 @@ export const toc: Action<HTMLElement, UserTocParameters, TocAttributes> = functi
127127
if (element.hasAttribute(ATTRIBUTES.ignore)) continue;
128128

129129
const text = extractElementText(element);
130+
130131
const tocId = extractTocId(element, text);
132+
element.id = tocId;
133+
131134
processScrollMarginTop(element, scrollMarginTop);
132135
const a = processAnchor(element, anchor, tocId);
133136

0 commit comments

Comments
 (0)