-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #152 from rsksmart/feat/render-equations
Render Equation support
- Loading branch information
Showing
4 changed files
with
45 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import katex from 'katex' | ||
import 'katex/dist/katex.min.css' | ||
|
||
function renderEquation (el) { | ||
if (!el) return | ||
|
||
let equation = el.getAttribute('title').slice('tex-render '.length) | ||
|
||
if (!equation) return | ||
|
||
let equationEl = document.createElement('span') | ||
|
||
katex.render(equation, equationEl, { | ||
throwOnError: false, | ||
}) | ||
equationEl.setAttribute('title', equation) | ||
equationEl.classList.add('tex-rendered') | ||
el.replaceWith(equationEl) | ||
} | ||
|
||
function initRenderEquation () { | ||
const elements = document.querySelectorAll('a[title^="tex-render "]') | ||
if (elements.length === 0) return | ||
|
||
elements.forEach(renderEquation) | ||
} | ||
|
||
export function onRouteDidUpdate ({ location, previousLocation }) { | ||
// Don't execute if we are still on the same page; the lifecycle may be fired | ||
// because the hash changes (e.g. when navigating between headings) | ||
if (location.pathname !== previousLocation?.pathname) { | ||
initRenderEquation() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters