Embed syntax (#47)

Co-authored-by: Subash <subash@vuln.in>
This commit is contained in:
Subash SN
2024-02-14 18:13:31 +01:00
committed by GitHub
parent 837056a50e
commit a4d631e047
10 changed files with 20 additions and 10 deletions

View File

@@ -68,10 +68,20 @@ customElements.define('docs-viewer', class extends HTMLElement {
observer.observe(document.documentElement, { attributes: true, attributeFilter: ['class'] })
}
patchSyntax(input) {
// remove backticks inside link, example: [`hypercore`] -> [hypercore]
let output = input.replace(/\[`([^`]+)`\]/g, "[$1]")
// remove gitbook embed syntax
output = output.replace(/{% embed url="([^"]+)" %}\s*(.*?)\s*{% embeded %}/g, '> [$2]($1)')
return output
}
async load (page = '/') {
if (page === '/') page = this.entry
const html = await fetch(page)
const text = await marked.parse((await html.text()).replace(/\[`([^`]+)`\]/g, "[$1]")) // remove backticks inside link, example: [`hypercore`] -> [hypercore]
const text = await marked.parse(this.patchSyntax(await html.text()))
this.panel.querySelector('slot').innerHTML = text
this.panel.style.display = ''
}