Skip to content

Commit 14b393e

Browse files
committed
Re-enable anchors links feature as optional
This reverts commit 9579118.
1 parent 9579118 commit 14b393e

File tree

5 files changed

+40
-5
lines changed

5 files changed

+40
-5
lines changed

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,19 @@ Example HTML:
3939
</vellum-doc>
4040
```
4141

42+
Anchor style links can be dynamically added to document headers using the
43+
`anchors` attribute:
44+
45+
```html
46+
<script type="module">
47+
import 'vellum-doc/vellum-doc.js'
48+
</script>
49+
50+
<vellum-doc anchors>
51+
<h1>Commodi</h1>
52+
</vellum-doc>
53+
```
54+
4255
### Styling
4356

4457
`<vellum-doc>` exposes the `index` **part**, which can be used to style the

index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
</head>
2727

2828
<body>
29-
<vellum-doc>
29+
<vellum-doc anchors>
3030
<section id="document">
3131
<h1>Commodi</h1>
3232
<p>

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.7.0",
3+
"version": "0.8.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: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { LitElement, html, css } from 'lit'
2-
import { customElement } from 'lit/decorators.js'
2+
import { customElement, property } from 'lit/decorators.js'
33
import { slugifyWithCounter } from '@sindresorhus/slugify'
44

55
@customElement('vellum-doc')
@@ -74,6 +74,9 @@ export class VellumDocument extends LitElement {
7474
}
7575
`
7676

77+
@property({ type: Boolean })
78+
anchors?: boolean
79+
7780
private slugify = slugifyWithCounter()
7881

7982
get headings(): HTMLElement[] {
@@ -97,6 +100,21 @@ export class VellumDocument extends LitElement {
97100
})
98101
}
99102

103+
anchorHeadings() {
104+
this.headings.forEach(heading => {
105+
const spacing = document.createTextNode(' ')
106+
heading.append(spacing)
107+
108+
const anchor = document.createElement('a')
109+
anchor.href = `#${heading.id}`
110+
anchor.innerHTML = '#'
111+
anchor.className = 'anchor'
112+
anchor.title = heading.textContent ? heading.textContent : ''
113+
114+
heading.append(anchor)
115+
})
116+
}
117+
100118
override render() {
101119
return html`
102120
<div id="sidebar">
@@ -126,6 +144,10 @@ export class VellumDocument extends LitElement {
126144
html`<a href="#${id}">${heading}</a>`
127145
)
128146
}
147+
148+
override updated() {
149+
if (this.anchors) this.anchorHeadings()
150+
}
129151
}
130152

131153
declare global {

0 commit comments

Comments
 (0)