Support for server-side math rendering with MathJax/KaTeX #644
Replies: 5 comments 1 reply
-
I am not familiar with JS or nodejs. But I believe that you can do something like this to call any elisp function (that would be calling your script) once the exporting is done.
It's out of scope of ox-hugo because it uses the ox.el machinery to export different elements like headings, bold, italics, code blocks, etc. You can uses advices or wrap the ox-hugo export function in another function where it also runs the nodejs scripts. |
Beta Was this translation helpful? Give feedback.
-
Thanks for the reply! Calling a script after exporting the HTML is an interesting idea, but I don't know how reliable it will be to parse the HTML file to identify the math elements... As ox-hugo is able to identify the math elements (whatever is inside |
Beta Was this translation helpful? Give feedback.
-
@shreevatsa Yes, you can tweak just the selected functions. See this. Relevant part:
You might want to add filter functions to
The |
Beta Was this translation helpful? Give feedback.
-
Closing this issue as I am assuming this to be resolved. Feel free to comment further in this thread. |
Beta Was this translation helpful? Give feedback.
-
Finally gave this a try, over a year later. Thanks for your advice and suggestions! Leaving a note here of what I did, just in case it's useful to anyone else finding this issue, or someone can make it less hacky. I put something like the following in (defun my-latex-filter-common (display-mode text backend info)
"text will be something like '\\(1 + \\frac12 + \\frac13 + \\dots\\)' and backend will be 'hugo'."
(when (string= backend "hugo")
(let* ((tmpfile (make-temp-file "use-katex" nil "katex-input" text))
(result (shell-command-to-string (format "NODE_PATH=/usr/local/lib/node_modules /usr/local/bin/node /Users/shreevatsa/Downloads/use-katex.js %s < %s" (if display-mode "display" "inline") tmpfile))))
(message "Using tmpfile %s" tmpfile)
;; (string-trim result)
result
)))
(defun my-latex-filter-inline (text backend info) (my-latex-filter-common nil text backend info))
(defun my-latex-filter-display (text backend info) (my-latex-filter-common t text backend info))
(add-to-list 'org-export-filter-latex-fragment-functions 'my-latex-filter-inline)
(add-to-list 'org-export-filter-latex-environment-functions 'my-latex-filter-display) Here, const katex = require('katex');
const fs = require("fs");
let input = fs.readFileSync(0).toString(); // Read from stdin
let displayMode = process.argv[2] == 'display' ? true : false;
if (input.startsWith(String.raw`\\(`)) displayMode = false;
else if (input.startsWith(String.raw`\\[`)) displayMode = true;
else console.log('to display or not to display?');
// The start and end of the string
input = input.replace(String.raw`\\(`, '').replace(String.raw`\\)`, '');
input = input.replace(String.raw`\\[`, '').replace(String.raw`\\]`, '');
// undscores are escaped -- unescape them
input = input.replaceAll(String.raw`\_`, '_');
// \, gets escaped into \\, -- revert this
input = input.replaceAll(String.raw`\\,`, String.raw`\,`);
var html = katex.renderToString(input, {
throwOnError: false,
displayMode: displayMode,
});
console.log(html); Results and problems:
|
Beta Was this translation helpful? Give feedback.
-
This is a feature request, and apologies that it is a half-baked one: I'm throwing up an idea before investigating it carefully.
Background:
To get properly typeset mathematics on a web page, the usual options are MathJax/KaTeX.
In their more commonly used client-side rendering mode, they are used as a piece of JavaScript on the page that scans the DOM for math fragments and replaces them with typeset HTML+CSS (or SVG, or…).
Hugo can take care of including this JS on the page, but the main problem using MathJax/KaTeX with Hugo is making sure that one's math markup in the input source file (
.md
) makes it through to the final generated HTML unscathed (so that MathJax/KaTeX have the correct syntax to work on).ox-hugo helps here, as it allows the user to write natural LaTeX syntax, and recognizes math blocks on the page and carefully escapes them before exporting to MarkDown, so that after Hugo works on it, the correct syntax will be left on the generated HTML page.
MathJax and KaTeX also have (less commonly used) support for server-side rendering, where the JavaScript code can be run (using Node.js I think) by the page author, so that the HTML+CSS (or SVG…) can be generated and put into the rendered HTML directly. That way, the user's browser doesn't need to run JavaScript (and wait for it to finish) to display the typeset mathematics.
Now my question is about whether ox-hugo, currently the best (and only?) way to easily insert mathematics into Hugo-generated sites, could be made to support this server-side rendering. The advantage for the reader would be as mentioned above: the page would load faster (no waiting for the mathematics to get typeset), and even without JavaScript being enabled.
This would probably require invoking js from ox-hugo during export (if node is installed on the user's computer). So I don't know how feasible / out-of-scope it is, sorry for not checking. But it might be something worth looking into. (Unrelated but Pandoc can do this, via a filter that handles the math elements…)
Beta Was this translation helpful? Give feedback.
All reactions