This commit is contained in:
Danila Fedorin 2020-12-15 03:18:19 +00:00
commit 2fa33d6d96
5 changed files with 31 additions and 10 deletions

View File

@ -15,4 +15,7 @@ mkdir $out
cp -r public/$publicPath/* $out/
# Render math in HTML and XML files.
node $server &
SERVER_ID=$!
find $out/ -regex "$out/.*\.html" | xargs ruby $converter
kill $SERVER_ID

View File

@ -1,23 +1,24 @@
require "open3"
require "nokogiri"
require "net/http"
require "json"
def render_cached(cache, command, string, render_comment = nil)
def render_cached(cache, display, string, render_comment = nil)
cache.fetch(string) do |new|
puts " Rendering #{render_comment || new}"
cache[string] = Open3.popen3(command) do |i, o, e, t|
i.write new
i.close
o.read.force_encoding(Encoding::UTF_8).strip
end
res = Net::HTTP.post URI("http://localhost:3000/render"),
{ :equations => [ { :str => string, :display => display } ] }.to_json,
"Content-Type" => "application/json"
cache[string] = JSON.parse(res.body)[0]
end
end
def perform_katex_sub(inline_cache, display_cache, content)
rendered = content.gsub /\\\(((?:[^\\]|\\[^\)])*)\\\)/ do |match|
render_cached(inline_cache, "katex", $~[1])
render_cached(inline_cache, false, $~[1])
end
rendered = rendered.gsub /\$\$((?:[^\$]|$[^\$])*)\$\$/ do |match|
render_cached(display_cache, "katex -d", $~[1], "display")
render_cached(display_cache, true, $~[1], "display")
end
return rendered
end

View File

@ -21,7 +21,12 @@ let
extraFlags = if settings.drafts then " -D " else "";
builder = ./builder.sh;
converter = ./convert.rb;
buildInputs = [ hugo requiredPackages.katex (ruby.withPackages (ps: [ ps.nokogiri ])) ];
server = ./katexserver.js;
buildInputs = [
hugo
requiredPackages.katex requiredPackages.express requiredPackages.bodyParser
(ruby.withPackages (ps: [ ps.nokogiri ]))
];
};
rev = "565d4a695541de1d2c760270adf082e36809c464";
sha256 = "0sv23falf3dciapclhl25yf22qi6c26in9zqcl3dvad2v2j7bzbc";

12
blog/katexserver.js Normal file
View File

@ -0,0 +1,12 @@
const katex = require('katex');
const express = require('express');
const bodyParser = require('body-parser');
app = express();
app.use(bodyParser.json());
app.post('/render', (req, res) => {
console.log(req.body)
res.send(req.body.equations.map(eq =>
katex.renderToString(eq.str, { throwOnError: false, displayMode: eq.display })));
});
app.listen(3000);

View File

@ -1,3 +1,3 @@
[
"katex"
"katex", "express", "body-parser"
]