generated at
katex-patch


patch.js
import { macros as mphys } from "../KatexPhysicsMacros/macros.js"; // import {macros as mchem } from "../mhchemmacros/mhchem.js" import { setCodeFormula, setup } from "../MultiLineFormula/script.js" function popNextArg(ctx) { return ctx.consumeArgs(1)[0].reverse().map( t => t.text + (t.loc.lexer.input[t.loc.end-1] === " " ? " " : "") ).join(""); } const done_mark = "\\htmlStyle{opacity: 0.5}{\\includegraphics[height=0.8em, totalheight=0.9em, width=0.9em, alt=KA logo]{https://katex.org/img/khan-academy.png}} " const loaded = {"KatexPhysicsMacros": done_mark} const default_macros = { ...mphys, "\\usepackage"(ctx) { if (scrapbox.Layout !== "page") { return } let name = popNextArg(ctx); if (name === "") { name = scrapbox.Page.title; } if (loaded.hasOwnProperty(name)) { return loaded[name]; } loaded[name] = "loading"; const url = `../${name}/macros.js` import(url).then(({macros}) => { set_macros(macros) loaded[name] = done_mark forceUpdate() }) return "{\\rm loading}" } } const pageid_macros = {} const cachedid = [undefined, undefined, undefined] function get_macros() { const pid = scrapbox.Page.id if (!pageid_macros.hasOwnProperty(pid)) { pageid_macros[pid] = {...default_macros} cachedid.push(pid) const remove_id = cachedid.shift() delete pageid_macros[remove_id] } return pageid_macros[pid] } function set_macros(macros) { const pid = scrapbox.Page.id if (!pageid_macros.hasOwnProperty(pid)) { pageid_macros[pid] = {...macros, ...default_macros} cachedid.push(pid) const remove_id = cachedid.shift() delete pageid_macros[remove_id] return } pageid_macros[pid] = {...macros, ...pageid_macros[pid]} } function deco_parse(o_parse) { return function parse(){ const now_macros = get_macros() const hasdeftag = now_macros.hasOwnProperty("\\df@tag"); this.settings.macros = this.gullet.macros.current = now_macros; try { const res = o_parse.call(this); return res } catch (error) { console.error(error); } finally { if (hasdeftag) { delete now_macros["\\df@tag"] } } } } function deco_parseFunction(orig) { return function parseFunction(t, i, m) { const v = this.fetch(); const _ = v.text; if (_ != "\\coover") return orig.call(this, t, i, m); const M = { numArgs: 0, numOptionalArgs: 0, greediness: 1, } const greediness = 1; const allowedInText = false; const allowedInMath = true; if (this.consume(), null != m && greediness <= m) throw new j("Got function '" + _ + "' with no arguments" + (i ? " as " + i : ""),v); if ("text" === this.mode && !allowedInText) throw new j("Can't use function '" + _ + "' in text mode",v); if ("math" === this.mode && !1 === allowedInMath) throw new j("Can't use function '" + _ + "' in math mode",v); var W = this.parseArguments(_, M) , $ = W.args , Y = W.optArgs; return { "type": "infix", "mode": "math", "replaceWith": "\\cfrac", "token": v } } } let enabled = false const o_consolelog = console.log; console.log = function logpatch(...args){ if (args.length == 5 && args[2].type === "verb"){ o_consolelog("patch"); const macro_info = args[2]; let o_handler = macro_info.handler; macro_info.handler = function(t, i, s){ //o_consolelog(t, i, s); const Parser = Object.getPrototypeOf(t.parser); const o_parse = Parser.parse; Parser.parse = deco_parse(o_parse); const o_parseFunction = Parser.parseFunction; Parser.parseFunction = deco_parseFunction(o_parseFunction); Object.getPrototypeOf(t.parser.settings).isTrusted = _=>true; console.log = o_consolelog; macro_info.handler = o_handler; enabled = true } } else { o_consolelog(...args) } } function getReactFiber(obj) { if (obj === null) return undefined return obj[Object.keys(obj).find((key) => key.startsWith("__reactFiber"))] } const patch_code = "[$ \\show \\verb ][$ \\verb{} ]" function enablepatch() { switch (scrapbox.Layout) { case "page": console.log("enable patch")   const line = getReactFiber(document.querySelector("#editor > div > div.lines > div:nth-child(2)"))   ?.return.stateNode   if (line === undefined) return   line.props.children = patch_code   line.props.codeBlock = undefined   line.props.tableBlock = undefined   line.props.cli = undefined   line.props.helpfeel = undefined line.updater.enqueueForceUpdate(line) break case "list": getReactFiber(document.querySelector("li > a > div.content")) ?.child.sibling.type({page:{}}).type({page:{descriptions:[""]}}) .props.children[1][0].type({children:patch_code}) break case "stream": console.log("stream") getReactFiber(document.querySelector( "div.stream > div.lazy-render > div.time-range > .page > div.lines > div:last-child")) ?.child.type({children:""}).props.children.type({children: patch_code}) break default: break } } enablepatch() const app = document.querySelector("#app-container")._reactRootContainer._internalRoot.current.child.stateNode; const display_style = app._stores.find(obj=>obj.constructor.name=="DisplayStyle"); function forceUpdate(){ display_style.style['hide-dots'] = true; app.onStoreChange(); display_style.style['hide-dots'] = false; app.onStoreChange(); } function mullineformula() { if (!enabled) enablepatch() if (scrapbox.Layout === "page") { const now_macros = get_macros() setCodeFormula(now_macros).then(() => { forceUpdate(); setup(now_macros); }) } else { forceUpdate(); } } window.scrapbox.addListener("page:changed", mullineformula); mullineformula() window.macros = pageid_macros