Skip to content

Commit

Permalink
Merge pull request #152 from rsksmart/feat/render-equations
Browse files Browse the repository at this point in the history
Render Equation support
  • Loading branch information
owans authored Nov 15, 2024
2 parents f189da4 + 14d7460 commit f39beac
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 0 deletions.
3 changes: 3 additions & 0 deletions docusaurus.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ const config = {
defaultLocale: 'en',
locales: ['en'],
},
clientModules:[
'/src/clientModules/renderEquations.js'
],
plugins: [
'docusaurus-plugin-sass',
[
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"clsx": "^2.0.0",
"docusaurus-plugin-sass": "^0.2.5",
"jssha": "^3.3.1",
"katex": "^0.16.11",
"prism-react-renderer": "^2.3.0",
"react": "^18.0.0",
"react-bootstrap": "^2.10.2",
Expand Down
34 changes: 34 additions & 0 deletions src/clientModules/renderEquations.js
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()
}
}
7 changes: 7 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5282,6 +5282,13 @@ jssha@^3.3.1:
resolved "https://registry.yarnpkg.com/jssha/-/jssha-3.3.1.tgz#c5b7fc7fb9aa745461923b87df0e247dd59c7ea8"
integrity sha512-VCMZj12FCFMQYcFLPRm/0lOBbLi8uM2BhXPTqw3U4YAfs4AZfiApOoBLoN8cQE60Z50m1MYMTQVCfgF/KaCVhQ==

katex@^0.16.11:
version "0.16.11"
resolved "https://registry.yarnpkg.com/katex/-/katex-0.16.11.tgz#4bc84d5584f996abece5f01c6ad11304276a33f5"
integrity sha512-RQrI8rlHY92OLf3rho/Ts8i/XvjgguEjOkO1BEXcU3N8BqPpSzBNwV/G0Ukr+P/l3ivvJUE/Fa/CwbS6HesGNQ==
dependencies:
commander "^8.3.0"

keyv@^4.5.3:
version "4.5.4"
resolved "https://registry.yarnpkg.com/keyv/-/keyv-4.5.4.tgz#a879a99e29452f942439f2a405e3af8b31d4de93"
Expand Down

0 comments on commit f39beac

Please sign in to comment.