Skip to content

Commit ff843b4

Browse files
committed
Add: anchors links for headings
1 parent 4ac12aa commit ff843b4

File tree

3 files changed

+22
-3
lines changed

3 files changed

+22
-3
lines changed

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "vellum-doc",
3-
"version": "0.5.2",
3+
"version": "0.6.0",
44
"description": "A simple document web component for web publishing",
55
"main": "vellum-doc.js",
66
"module": "vellum-doc.js",

src/vellum-doc.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,21 @@ export class VellumDocument extends LitElement {
9797
})
9898
}
9999

100+
anchorHeadings() {
101+
this.headings.forEach(heading => {
102+
const spacing = document.createTextNode(' ')
103+
heading.append(spacing)
104+
105+
const anchor = document.createElement('a')
106+
anchor.href = `#${heading.id}`
107+
anchor.innerHTML = '#'
108+
anchor.className = 'anchor'
109+
anchor.title = heading.textContent ? heading.textContent : ''
110+
111+
heading.append(anchor)
112+
})
113+
}
114+
100115
override render() {
101116
return html`
102117
<div id="sidebar">
@@ -126,6 +141,10 @@ export class VellumDocument extends LitElement {
126141
html`<a href="#${id}">${heading}</a>`
127142
)
128143
}
144+
145+
override updated() {
146+
this.anchorHeadings()
147+
}
129148
}
130149

131150
declare global {

0 commit comments

Comments
 (0)