From b21b27d1d3d68bfb4d285f39122a154fceefb9ab Mon Sep 17 00:00:00 2001 From: Jacky Zhao Date: Thu, 5 May 2022 20:30:55 -0400 Subject: [PATCH] fix: clean wikilinks and render latex in popover --- assets/js/popover.js | 41 +++++++++++++++++++-------------- assets/js/search.js | 1 + layouts/partials/head.html | 6 ++++- layouts/partials/page-list.html | 2 +- 4 files changed, 31 insertions(+), 19 deletions(-) diff --git a/assets/js/popover.js b/assets/js/popover.js index 9270b8a..5fb2244 100644 --- a/assets/js/popover.js +++ b/assets/js/popover.js @@ -5,13 +5,14 @@ function htmlToElement(html) { return template.content.firstChild } -function initPopover(baseURL, useContextualBacklinks) { +function initPopover(baseURL, useContextualBacklinks, renderLatex) { const basePath = baseURL.replace(window.location.origin, "") fetchData.then(({ content }) => { const links = [...document.getElementsByClassName("internal-link")] links .filter(li => li.dataset.src || (li.dataset.idx && useContextualBacklinks)) .forEach(li => { + var el if (li.dataset.ctx) { const linkDest = content[li.dataset.src] const popoverElement = `
@@ -19,32 +20,38 @@ function initPopover(baseURL, useContextualBacklinks) {

${highlight(removeMarkdown(linkDest.content), li.dataset.ctx)}...

${new Date(linkDest.lastmodified).toLocaleDateString()}

` - const el = htmlToElement(popoverElement) - li.appendChild(el) - li.addEventListener("mouseover", () => { - el.classList.add("visible") - }) - li.addEventListener("mouseout", () => { - el.classList.remove("visible") - }) + el = htmlToElement(popoverElement) } else { const linkDest = content[li.dataset.src.replace(/\/$/g, "").replace(basePath, "")] + console.log(linkDest.content) if (linkDest) { const popoverElement = `

${linkDest.title}

${removeMarkdown(linkDest.content).split(" ", 20).join(" ")}...

${new Date(linkDest.lastmodified).toLocaleDateString()}

` - const el = htmlToElement(popoverElement) - li.appendChild(el) - li.addEventListener("mouseover", () => { - el.classList.add("visible") - }) - li.addEventListener("mouseout", () => { - el.classList.remove("visible") - }) + el = htmlToElement(popoverElement) } } + li.appendChild(el) + if (renderLatex) { + renderMathInElement(el, { + delimiters: [ + { left: '$$', right: '$$', display: false }, + { left: '$', right: '$', display: false }, + { left: '\\(', right: '\\)', display: false }, + { left: '\\[', right: '\\]', display: false } + ], + throwOnError: false + }) + } + li.addEventListener("mouseover", () => { + el.classList.add("visible") + }) + li.addEventListener("mouseout", () => { + el.classList.remove("visible") + }) + }) }) } diff --git a/assets/js/search.js b/assets/js/search.js index 975eaa6..195a40b 100644 --- a/assets/js/search.js +++ b/assets/js/search.js @@ -39,6 +39,7 @@ const removeMarkdown = ( .replace(/\s{0,2}\[.*?\]: .*?$/g, '') .replace(/\!\[(.*?)\][\[\(].*?[\]\)]/g, options.useImgAltText ? '$1' : '') .replace(/\[(.*?)\][\[\(].*?[\]\)]/g, '$1') + .replace(/!?\[\[\S[^\[\]\|]*(?:\|([^\[\]]*))?\S\]\]/g, '$1') .replace(/^\s{0,3}>\s?/g, '') .replace(/(^|\n)\s{0,3}>\s?/g, '\n\n') .replace(/^\s{1,2}\[(.*?)\]: (\S+)( ".*?")?\s*$/g, '') diff --git a/layouts/partials/head.html b/layouts/partials/head.html index a8d947e..a5f23da 100644 --- a/layouts/partials/head.html +++ b/layouts/partials/head.html @@ -85,7 +85,11 @@ ); {{if $.Site.Data.config.enableLinkPreview}} - initPopover({{strings.TrimRight "/" .Site.BaseURL }}, {{$.Site.Data.config.enableContextualBacklinks}}) + initPopover( + {{strings.TrimRight "/" .Site.BaseURL }}, + {{$.Site.Data.config.enableContextualBacklinks}}, + {{$.Site.Data.config.enableLatex}} + ) {{end}} {{if $.Site.Data.config.enableLatex}} renderMathInElement(document.body, { diff --git a/layouts/partials/page-list.html b/layouts/partials/page-list.html index bdfcd59..6c2249b 100644 --- a/layouts/partials/page-list.html +++ b/layouts/partials/page-list.html @@ -12,4 +12,4 @@ {{- end -}} - \ No newline at end of file +