1// Copyright 2020-2022 The Khronos Group Inc. 2// 3// SPDX-License-Identifier: Apache-2.0 4 5// Translates the latexmath in html on build time using KaTeX 6// Usage: nodejs translate_math.js katex/katex.min.js vkspec.html 7 8const katex = require(process.argv[2]); 9const fs = require("fs"); 10const escapeRegex = require("escape-string-regexp"); 11const he = require('he'); 12 13const filepath = process.argv[3]; 14 15var html = fs.readFileSync(filepath, "utf8"); 16 17const delimiters = [ 18 //{ left: "$$", right: "$$", display: true}, 19 { left: "\\[", right: "\\]", display: true}, 20 //{ left: "$", right: "$", display: false}, 21 { left: "\\(", right: "\\)", display: false} 22 ] 23 24for( var delim of delimiters ) { 25 const regex = new RegExp( escapeRegex(delim.left) + "([\\S\\s]*?)" + escapeRegex(delim.right), "g"); 26 html = html.replace( regex, 27 function(match, g1) { 28 return katex.renderToString( he.decode(g1, {'strict': true}), {displayMode: delim.display, output: 'html', strict: true} ); 29 } 30 ); 31} 32 33fs.writeFileSync(filepath, html, 'utf8'); 34