support for h1 hyperlink

This commit is contained in:
cupcakearmy 2021-04-18 17:43:39 +02:00
parent f2c6561efa
commit cc5c6cd595
No known key found for this signature in database
GPG Key ID: D28129AE5654D9D9

View File

@ -25,12 +25,28 @@
export let content
function encodeTextToUrl(text) {
return text
.replace(/[^A-Za-z ]/, '')
.replace('/ +/', ' ')
.replace(' ', '-')
}
onMount(() => {
hljs.highlightAll()
const selector = [1, 2, 3, 4, 5, 6]
.map((i) => `div.adapter h${i}`)
.join(', ')
const elements = window.document.querySelectorAll(selector)
for (const el of elements) {
const hash = encodeTextToUrl(el.textContent)
el.innerHTML = `<a class="target-link" name="${hash}" href="${window.location.pathname}#${hash}">${el.innerHTML}</a>`
}
})
</script>
<div>
<div class="adapter">
{@html content}
</div>
@ -102,6 +118,10 @@
margin-left: -0.7rem;
}
div :global(.target-link) {
border-bottom: none;
}
div :global(p.has-background) {
padding: 0.5em;
}