diff options
author | Marvin Borner | 2024-01-22 10:18:35 +0100 |
---|---|---|
committer | Marvin Borner | 2024-01-22 10:21:18 +0100 |
commit | c352987645294838b7ea1eaa3cbcaf0994bf9dd0 (patch) | |
tree | 016bcbc085a355e7a5c5d1535260f0f372597d8d /server-katex | |
parent | b7147115286644a87184e1607628a32a1fe832bc (diff) |
Sync from main
Diffstat (limited to 'server-katex')
-rw-r--r-- | server-katex/filter.lua | 23 | ||||
-rw-r--r-- | server-katex/math.ts | 18 |
2 files changed, 41 insertions, 0 deletions
diff --git a/server-katex/filter.lua b/server-katex/filter.lua new file mode 100644 index 0000000..1010217 --- /dev/null +++ b/server-katex/filter.lua @@ -0,0 +1,23 @@ +function Pandoc(doc) + local tmp_name = os.tmpname() + local math = assert(io.popen("deno run server-katex/math.ts > " .. tmp_name, "w")) + doc:walk({ + Math = function(el) + if el.mathtype == 'DisplayMath' then + assert(math:write("d" .. el.text:gsub("\n", " ") .. "\n")) + else + assert(math:write("i" .. el.text:gsub("\n", " ") .. "\n")) + end + end + }) + math:close() + local tmp = assert(io.open(tmp_name, "r")) + doc = doc:walk({ + Math = function(el) + return pandoc.RawInline(FORMAT, tmp:read()) + end + }) + tmp:close() + os.remove(tmp_name) + return doc +end diff --git a/server-katex/math.ts b/server-katex/math.ts new file mode 100644 index 0000000..7a5b891 --- /dev/null +++ b/server-katex/math.ts @@ -0,0 +1,18 @@ +import { readLines } from "https://deno.land/std@0.134.0/io/mod.ts"; +import katex from "https://cdn.jsdelivr.net/npm/katex@0.16.9/dist/katex.mjs"; + +const macros = []; +for await (const line of readLines(Deno.stdin)) { + try { + const output = katex.renderToString(line.slice(1), { + displayMode: line[0] == 'd', + throwOnError: false, + macros: macros, + strict: false, + fleqn: false + }).replace(/(\r\n|\n|\r)/gm, ""); + console.log(output); + } catch (error) { + throw new Error(`Input: ${line}\n\nError: ${error}`); + } +} |