Skip to content

Commit

Permalink
fix: add missing id & role to heading elements
Browse files Browse the repository at this point in the history
  • Loading branch information
jrson83 committed Sep 27, 2023
1 parent 932a812 commit e2c3234
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 27 deletions.
9 changes: 8 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@

All notable changes to this project will be documented in this file.

## [0.1.0] - 2023-0927
## 0.2.0 - 2023-09-27

### Bug Fixes

- Add missing `id` & `role` to heading elements


## 0.1.0 - 2023-09-27

- initial release
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ console.log(value)

```html
<section id="heading-21-section">
<h2>
<h2 id="heading-21" role="presentation">
<a href="#heading-21" class="heading-anchor">
<button type="button" class="heading-anchor__btn">#</button>
</a>
Expand All @@ -112,15 +112,15 @@ console.log(value)
</section>

<section id="heading-22-section">
<h2>
<h2 id="heading-22" role="presentation">
<a href="#heading-22" class="heading-anchor">
<button type="button" class="heading-anchor__btn">#</button>
</a>
<span role="heading" aria-level="2">Heading 2.2</span>
</h2>
<p>Paragraph 2</p>
<section id="heading-31-section">
<h3>
<h3 id="heading-31" role="presentation">
<a href="#heading-31" class="heading-anchor">
<button type="button" class="heading-anchor__btn">#</button>
</a>
Expand All @@ -131,7 +131,7 @@ console.log(value)
</section>

<section id="heading-23-section">
<h2>
<h2 id="heading-23" role="presentation">
<a href="#heading-23" class="heading-anchor">
<button type="button" class="heading-anchor__btn">#</button>
</a>
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "rehype-slug-anchor-sectionize",
"version": "0.1.0",
"version": "0.2.0",
"description": "rehype plugin which wraps headings and their descendants in nested <section> elements and applies slugged anchor links to them.",
"keywords": [
"anchor",
Expand Down
32 changes: 15 additions & 17 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ const rehypeSlugAnchorSectionize: Plugin<
endIndex > 0 ? endIndex : undefined
)

const slugId = node.properties.id || slug(hastToString(node)) || null
const slugId = slug(hastToString(node)) || null

const linkProps = {
href: `#${slugId}`,
Expand All @@ -95,12 +95,12 @@ const rehypeSlugAnchorSectionize: Plugin<
}),
}

const section = {
type: 'element',
tagName: options.wrapperTagName,
properties: sectionProps,
children: between,
} as Element
node.properties = {
...(slugId && {
id: slugId,
}),
role: 'presentation',
}

node.children = [
{
Expand All @@ -120,19 +120,17 @@ const rehypeSlugAnchorSectionize: Plugin<
type: 'element',
tagName: 'span',
properties: { role: 'heading', ariaLevel: depthTag.slice(1) },
children: [
Object.assign({}, ...node.children, {
properties: {
...(slugId && {
id: slugId,
}),
role: 'presentation',
},
}),
],
children: node.children,
},
]

const section = {
type: 'element',
tagName: options.wrapperTagName,
properties: sectionProps,
children: between,
} as Element

parent.children.splice(startIndex, section.children.length, section)
}
)
Expand Down
8 changes: 4 additions & 4 deletions test/__snapshots__/plugin.test.ts.snap
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`should ... 1`] = `
"<section id=\\"heading-21-section\\"><h2><a href=\\"#heading-21\\" class=\\"heading-anchor\\"><button type=\\"button\\" class=\\"heading-anchor__btn\\">#</button></a><span role=\\"heading\\" aria-level=\\"2\\">Heading 2.1</span></h2>
"<section id=\\"heading-21-section\\"><h2 id=\\"heading-21\\" role=\\"presentation\\"><a href=\\"#heading-21\\" class=\\"heading-anchor\\"><button type=\\"button\\" class=\\"heading-anchor__btn\\">#</button></a><span role=\\"heading\\" aria-level=\\"2\\">Heading 2.1</span></h2>
<p>Paragraph 1</p>
</section><section id=\\"heading-22-section\\"><h2><a href=\\"#heading-22\\" class=\\"heading-anchor\\"><button type=\\"button\\" class=\\"heading-anchor__btn\\">#</button></a><span role=\\"heading\\" aria-level=\\"2\\">Heading 2.2</span></h2>
</section><section id=\\"heading-22-section\\"><h2 id=\\"heading-22\\" role=\\"presentation\\"><a href=\\"#heading-22\\" class=\\"heading-anchor\\"><button type=\\"button\\" class=\\"heading-anchor__btn\\">#</button></a><span role=\\"heading\\" aria-level=\\"2\\">Heading 2.2</span></h2>
<p>Paragraph 2</p>
<section id=\\"heading-31-section\\"><h3><a href=\\"#heading-31\\" class=\\"heading-anchor\\"><button type=\\"button\\" class=\\"heading-anchor__btn\\">#</button></a><span role=\\"heading\\" aria-level=\\"3\\">Heading 3.1</span></h3>
<section id=\\"heading-31-section\\"><h3 id=\\"heading-31\\" role=\\"presentation\\"><a href=\\"#heading-31\\" class=\\"heading-anchor\\"><button type=\\"button\\" class=\\"heading-anchor__btn\\">#</button></a><span role=\\"heading\\" aria-level=\\"3\\">Heading 3.1</span></h3>
<p>Paragraph 3</p>
</section></section><section id=\\"heading-23-section\\"><h2><a href=\\"#heading-23\\" class=\\"heading-anchor\\"><button type=\\"button\\" class=\\"heading-anchor__btn\\">#</button></a><span role=\\"heading\\" aria-level=\\"2\\">Heading 2.3</span></h2>
</section></section><section id=\\"heading-23-section\\"><h2 id=\\"heading-23\\" role=\\"presentation\\"><a href=\\"#heading-23\\" class=\\"heading-anchor\\"><button type=\\"button\\" class=\\"heading-anchor__btn\\">#</button></a><span role=\\"heading\\" aria-level=\\"2\\">Heading 2.3</span></h2>
<p>Paragraph 4</p></section>"
`;

0 comments on commit e2c3234

Please sign in to comment.