katex-patch
code: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.inputt.loc.end-1 === " " ? " " : "") ).join("");
}
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)) {
}
const url = ../${name}/macros.js
import(url).then(({macros}) => {
set_macros(macros)
forceUpdate()
})
return "{\\rm loading}"
}
}
const pageid_macros = {}
function get_macros() {
const pid = scrapbox.Page.id
if (!pageid_macros.hasOwnProperty(pid)) {
pageid_macrospid = {...default_macros} cachedid.push(pid)
const remove_id = cachedid.shift()
}
}
function set_macros(macros) {
const pid = scrapbox.Page.id
if (!pageid_macros.hasOwnProperty(pid)) {
pageid_macrospid = {...macros, ...default_macros} cachedid.push(pid)
const remove_id = cachedid.shift()
return
}
pageid_macrospid = {...macros, ...pageid_macrospid} }
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) {
}
}
}
}
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 && args2.type === "verb"){ o_consolelog("patch");
const macro_info = args2; 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
}
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.children10.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(){
app.onStoreChange();
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