brane 0.1.1 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/AGENTS.md +362 -0
- package/CHANGELOG.md +33 -0
- package/CONTRIBUTING.md +91 -0
- package/DESIGN.md +402 -0
- package/Dockerfile +61 -0
- package/LICENSE +656 -17
- package/README.md +174 -2
- package/SECURITY.md +205 -0
- package/dist/absorb/brane-id.js +11 -0
- package/dist/absorb/chatgpt.js +161 -0
- package/dist/absorb/claude-code.js +504 -0
- package/dist/absorb/index.js +543 -0
- package/dist/absorb/organelle.js +345 -0
- package/dist/absorb/run.js +520 -0
- package/dist/absorb/sources.js +168 -0
- package/dist/absorb/types.js +3 -0
- package/dist/agent-env.js +45 -0
- package/dist/agents.js +1168 -0
- package/dist/claude-live-session.js +124 -0
- package/dist/cli.js +50 -0
- package/dist/codex-tool-log.js +144 -0
- package/dist/commands/absorb.js +291 -0
- package/dist/commands/attach.js +31 -0
- package/dist/commands/certify.js +62 -0
- package/dist/commands/config.js +116 -0
- package/dist/commands/cost.js +126 -0
- package/dist/commands/daemon.js +350 -0
- package/dist/commands/doctor.js +262 -0
- package/dist/commands/fold.js +95 -0
- package/dist/commands/list.js +89 -0
- package/dist/commands/mcp.js +84 -0
- package/dist/commands/meld.js +332 -0
- package/dist/commands/memory.js +215 -0
- package/dist/commands/revive.js +74 -0
- package/dist/commands/run.js +85 -0
- package/dist/commands/spawn.js +1094 -0
- package/dist/commands/token.js +153 -0
- package/dist/copilot-tool-log.js +145 -0
- package/dist/cost/pricing.js +116 -0
- package/dist/cost/reprice.js +136 -0
- package/dist/crush-tool-log.js +293 -0
- package/dist/daemon/agent-scope.js +116 -0
- package/dist/daemon/awaiting.js +54 -0
- package/dist/daemon/cert-manager.js +349 -0
- package/dist/daemon/client.js +262 -0
- package/dist/daemon/gated-routes.js +112 -0
- package/dist/daemon/keep-awake.js +103 -0
- package/dist/daemon/local-socket.js +145 -0
- package/dist/daemon/presence-gate.js +206 -0
- package/dist/daemon/server.js +6349 -0
- package/dist/daemon/signal-source.js +35 -0
- package/dist/daemon/tailscale-rebind.js +58 -0
- package/dist/daemon/token-ring.js +263 -0
- package/dist/daemon/token-routes.js +190 -0
- package/dist/db/events.js +152 -0
- package/dist/db/mentions.js +179 -0
- package/dist/db/schema.js +633 -0
- package/dist/db/tokens.js +583 -0
- package/dist/db/tool-uses.js +647 -0
- package/dist/diagram/index.js +85 -0
- package/dist/diagram/label.js +455 -0
- package/dist/diagram/layout.js +476 -0
- package/dist/diagram/links.js +50 -0
- package/dist/diagram/metrics.js +152 -0
- package/dist/diagram/parse.js +487 -0
- package/dist/diagram/svg.js +389 -0
- package/dist/diagram/types.js +9 -0
- package/dist/export.js +629 -0
- package/dist/files/archive-types.js +2 -0
- package/dist/files/archive.js +182 -0
- package/dist/files/browse.js +327 -0
- package/dist/files/bytes.js +162 -0
- package/dist/files/codecs.js +33 -0
- package/dist/files/find.js +212 -0
- package/dist/files/tar.js +223 -0
- package/dist/files/zip.js +209 -0
- package/dist/folds.js +72 -0
- package/dist/grammars/ebnf.js +92 -0
- package/dist/highlight.js +250 -0
- package/dist/ids.js +24 -0
- package/dist/markdown-diagnostics.js +233 -0
- package/dist/math.js +168 -0
- package/dist/mcp/server.js +886 -0
- package/dist/memory/embedder.js +145 -0
- package/dist/memory/embedding-worker.js +71 -0
- package/dist/memory/embeddings.js +91 -0
- package/dist/memory/index.js +232 -0
- package/dist/memory/storage.js +587 -0
- package/dist/memory/wiki.js +688 -0
- package/dist/memory/wordpiece.js +247 -0
- package/dist/mentions-scan.js +40 -0
- package/dist/presence.js +259 -0
- package/dist/session-affinity.js +185 -0
- package/dist/shared/api-request.js +124 -0
- package/dist/shared/block-glyphs.js +114 -0
- package/dist/shared/diagram-css.js +17 -0
- package/dist/shared/export-selection.js +101 -0
- package/dist/shared/find-in-text.js +110 -0
- package/dist/shared/glow-hues.js +225 -0
- package/dist/shared/hex-dump.js +62 -0
- package/dist/shared/hex-pages.js +52 -0
- package/dist/shared/hit-drum.js +79 -0
- package/dist/shared/known-nids.js +58 -0
- package/dist/shared/list-scroll-keys.js +56 -0
- package/dist/shared/markdown-lists.js +72 -0
- package/dist/shared/markdown-math.js +485 -0
- package/dist/shared/markdown-mentions.js +79 -0
- package/dist/shared/markdown-nids.js +41 -0
- package/dist/shared/markdown-protocols.js +53 -0
- package/dist/shared/markdown-refs.js +123 -0
- package/dist/shared/membrane-invasion.js +342 -0
- package/dist/shared/membrane-motion.js +83 -0
- package/dist/shared/membrane-palette.js +253 -0
- package/dist/shared/membrane-terrain.js +212 -0
- package/dist/shared/message-selection.js +123 -0
- package/dist/shared/mo-glyph.js +57 -0
- package/dist/shared/narrative-format.js +237 -0
- package/dist/shared/narrative-menu.js +82 -0
- package/dist/shared/narrative-sort.js +47 -0
- package/dist/shared/narrative-status.js +52 -0
- package/dist/shared/no-pan.js +85 -0
- package/dist/shared/pane-layout.js +50 -0
- package/dist/shared/payload-inline.js +83 -0
- package/dist/shared/speaker-color.js +61 -0
- package/dist/shared/token-rotation.js +14 -0
- package/dist/shared/view-url.js +197 -0
- package/dist/shared/wiki-links.js +70 -0
- package/dist/shared/wiki-window.js +4 -0
- package/dist/tailscale.js +55 -0
- package/dist/test/absorb-claude.test.js +892 -0
- package/dist/test/agent-scope.test.js +193 -0
- package/dist/test/agents.test.js +55 -0
- package/dist/test/api-request.test.js +122 -0
- package/dist/test/awaiting-glow.test.js +54 -0
- package/dist/test/awaiting.test.js +122 -0
- package/dist/test/bind-token.test.js +88 -0
- package/dist/test/block-glyphs.test.js +68 -0
- package/dist/test/cert-manager.test.js +262 -0
- package/dist/test/claude-live-session.test.js +110 -0
- package/dist/test/codex-tool-log.test.js +568 -0
- package/dist/test/config.test.js +71 -0
- package/dist/test/copilot-tool-log.test.js +93 -0
- package/dist/test/cors-preflight.test.js +49 -0
- package/dist/test/cost-linger.test.js +31 -0
- package/dist/test/cost-pricing.test.js +72 -0
- package/dist/test/cost-reprice.test.js +139 -0
- package/dist/test/cost-sessions.test.js +301 -0
- package/dist/test/crush-tool-log.test.js +289 -0
- package/dist/test/daemon-client.test.js +109 -0
- package/dist/test/daemon-socket.test.js +293 -0
- package/dist/test/diagram-arrows.test.js +205 -0
- package/dist/test/diagram-export.test.js +210 -0
- package/dist/test/diagram-labels.test.js +313 -0
- package/dist/test/diagram-parse.test.js +191 -0
- package/dist/test/diagram-render.test.js +175 -0
- package/dist/test/diagram-review.test.js +288 -0
- package/dist/test/diagram-self-loop-labels.test.js +303 -0
- package/dist/test/embedder.test.js +84 -0
- package/dist/test/events.test.js +123 -0
- package/dist/test/export-selection.test.js +102 -0
- package/dist/test/export.test.js +505 -0
- package/dist/test/files-browse.test.js +393 -0
- package/dist/test/files-find.test.js +272 -0
- package/dist/test/find-in-text.test.js +86 -0
- package/dist/test/first-paint-viewport.test.js +59 -0
- package/dist/test/fixtures/embedding-golden.js +23 -0
- package/dist/test/fixtures/wordpiece-golden.js +167 -0
- package/dist/test/glow-hues.test.js +202 -0
- package/dist/test/harness-cost.test.js +423 -0
- package/dist/test/helpers.js +21 -0
- package/dist/test/hex-pages.test.js +68 -0
- package/dist/test/highlight.test.js +69 -0
- package/dist/test/hit-drum.test.js +71 -0
- package/dist/test/hook-config.test.js +262 -0
- package/dist/test/json-gzip.test.js +70 -0
- package/dist/test/key-routing.test.js +58 -0
- package/dist/test/known-nids.test.js +106 -0
- package/dist/test/link-preview.test.js +67 -0
- package/dist/test/list-scroll-keys.test.js +66 -0
- package/dist/test/listen-own-words.test.js +147 -0
- package/dist/test/listen-timeout.test.js +33 -0
- package/dist/test/markdown-diagnostics.test.js +121 -0
- package/dist/test/markdown-lists.test.js +182 -0
- package/dist/test/markdown-math.test.js +256 -0
- package/dist/test/markdown-nids.test.js +146 -0
- package/dist/test/markdown-protocols.test.js +139 -0
- package/dist/test/mcp-bridge-cancel.test.js +98 -0
- package/dist/test/mcp-bridge-reconnect.test.js +121 -0
- package/dist/test/mcp-narrative.test.js +193 -0
- package/dist/test/mcp.test.js +548 -0
- package/dist/test/meld.test.js +85 -0
- package/dist/test/membrane-invasion.test.js +482 -0
- package/dist/test/membrane-motion.test.js +101 -0
- package/dist/test/membrane-palette.test.js +166 -0
- package/dist/test/membrane-terrain.test.js +170 -0
- package/dist/test/memory-log.test.js +202 -0
- package/dist/test/mentions-e2e.test.js +261 -0
- package/dist/test/mentions-ledger.test.js +265 -0
- package/dist/test/mentions-listen.test.js +265 -0
- package/dist/test/mentions.test.js +238 -0
- package/dist/test/message-selection.test.js +114 -0
- package/dist/test/mo-glyph.test.js +22 -0
- package/dist/test/model-resolution.test.js +166 -0
- package/dist/test/narrate-validate.test.js +77 -0
- package/dist/test/narrate-wait.test.js +160 -0
- package/dist/test/narrative-format.test.js +219 -0
- package/dist/test/narrative-menu.test.js +137 -0
- package/dist/test/narrative-sort.test.js +75 -0
- package/dist/test/no-pan.test.js +60 -0
- package/dist/test/pane-layout.test.js +153 -0
- package/dist/test/paths.test.js +37 -0
- package/dist/test/payload-inline.test.js +91 -0
- package/dist/test/precache-fonts.test.js +72 -0
- package/dist/test/presence-gate.test.js +440 -0
- package/dist/test/presence.test.js +160 -0
- package/dist/test/read-events.test.js +220 -0
- package/dist/test/remote-meld.test.js +115 -0
- package/dist/test/revive-cli.test.js +93 -0
- package/dist/test/search-floor.test.js +206 -0
- package/dist/test/search-fusion.test.js +168 -0
- package/dist/test/search.test.js +205 -0
- package/dist/test/security.test.js +48 -0
- package/dist/test/selection-wiring.test.js +61 -0
- package/dist/test/session-affinity.test.js +295 -0
- package/dist/test/signal-source.test.js +32 -0
- package/dist/test/socket-account.test.js +268 -0
- package/dist/test/spawn-toml.test.js +72 -0
- package/dist/test/speaker-color.test.js +90 -0
- package/dist/test/tailscale-rebind.test.js +79 -0
- package/dist/test/terminal-cursor.test.js +61 -0
- package/dist/test/terminal-resync.test.js +23 -0
- package/dist/test/thin-cli.test.js +382 -0
- package/dist/test/token-bootstrap.test.js +272 -0
- package/dist/test/token-devices.test.js +587 -0
- package/dist/test/token-lapse.test.js +94 -0
- package/dist/test/token-ring.test.js +188 -0
- package/dist/test/token-rotate-cli.test.js +58 -0
- package/dist/test/token-routes.test.js +95 -0
- package/dist/test/tool-uses.test.js +1006 -0
- package/dist/test/usage-ledger.test.js +126 -0
- package/dist/test/view-url.test.js +581 -0
- package/dist/test/wiki-links.test.js +57 -0
- package/dist/test/wiki-sources.test.js +85 -0
- package/dist/test/wiki-window.test.js +31 -0
- package/dist/test/wiki.test.js +399 -0
- package/dist/test/wordpiece.test.js +68 -0
- package/dist/tls.js +148 -0
- package/dist/tmux.js +202 -0
- package/dist/user-agent.js +46 -0
- package/docker-entrypoint.sh +141 -0
- package/docs/README.md +3 -0
- package/docs/absorb.md +231 -0
- package/docs/cost-ledger.md +125 -0
- package/docs/daemon-api.md +189 -0
- package/docs/diagrams.md +163 -0
- package/docs/doctor.md +36 -0
- package/docs/export-scope.md +69 -0
- package/docs/files-view.md +133 -0
- package/docs/link-previews.md +38 -0
- package/docs/math.md +32 -0
- package/docs/mcp.md +69 -0
- package/docs/memory-search.md +75 -0
- package/docs/mentions.md +101 -0
- package/docs/multi-brane.md +14 -0
- package/docs/passkeys.md +165 -0
- package/docs/presence.md +202 -0
- package/docs/releasing.md +53 -0
- package/docs/rendering-warnings.md +22 -0
- package/docs/request-layer.md +72 -0
- package/docs/selection-store.md +50 -0
- package/docs/session-affinity.md +61 -0
- package/docs/terminal-mirroring.md +211 -0
- package/docs/token-rotation.md +413 -0
- package/docs/view-urls.md +114 -0
- package/docs/web-app.md +23 -0
- package/docs/wiki.md +43 -0
- package/expo/dist/_expo/static/js/web/entry-e56efc1291dca7258ea00b8108b033a4.js +1097 -0
- package/expo/dist/apple-touch-icon.png +0 -0
- package/expo/dist/favicon.svg +3 -0
- package/expo/dist/index.html +177 -0
- package/expo/dist/katex/fonts/KaTeX_AMS-Regular.woff2 +0 -0
- package/expo/dist/katex/fonts/KaTeX_Caligraphic-Bold.woff2 +0 -0
- package/expo/dist/katex/fonts/KaTeX_Caligraphic-Regular.woff2 +0 -0
- package/expo/dist/katex/fonts/KaTeX_Fraktur-Bold.woff2 +0 -0
- package/expo/dist/katex/fonts/KaTeX_Fraktur-Regular.woff2 +0 -0
- package/expo/dist/katex/fonts/KaTeX_Main-Bold.woff2 +0 -0
- package/expo/dist/katex/fonts/KaTeX_Main-BoldItalic.woff2 +0 -0
- package/expo/dist/katex/fonts/KaTeX_Main-Italic.woff2 +0 -0
- package/expo/dist/katex/fonts/KaTeX_Main-Regular.woff2 +0 -0
- package/expo/dist/katex/fonts/KaTeX_Math-BoldItalic.woff2 +0 -0
- package/expo/dist/katex/fonts/KaTeX_Math-Italic.woff2 +0 -0
- package/expo/dist/katex/fonts/KaTeX_SansSerif-Bold.woff2 +0 -0
- package/expo/dist/katex/fonts/KaTeX_SansSerif-Italic.woff2 +0 -0
- package/expo/dist/katex/fonts/KaTeX_SansSerif-Regular.woff2 +0 -0
- package/expo/dist/katex/fonts/KaTeX_Script-Regular.woff2 +0 -0
- package/expo/dist/katex/fonts/KaTeX_Size1-Regular.woff2 +0 -0
- package/expo/dist/katex/fonts/KaTeX_Size2-Regular.woff2 +0 -0
- package/expo/dist/katex/fonts/KaTeX_Size3-Regular.woff2 +0 -0
- package/expo/dist/katex/fonts/KaTeX_Size4-Regular.woff2 +0 -0
- package/expo/dist/katex/fonts/KaTeX_Typewriter-Regular.woff2 +0 -0
- package/expo/dist/katex/katex.min.css +1 -0
- package/expo/dist/manifest.json +16 -0
- package/expo/dist/metadata.json +1 -0
- package/expo/dist/og/membrane.v1.png +0 -0
- package/expo/dist/pdf.min.mjs +28 -0
- package/expo/dist/pdf.worker.min.mjs +28 -0
- package/expo/dist/sw.js +154 -0
- package/package.json +79 -27
- package/protocols/LIVE_loop.md +407 -0
- package/protocols/bebrief.md +41 -0
- package/protocols/digest.md +231 -0
- package/protocols/fly.md +266 -0
- package/protocols/idle.md +81 -0
- package/protocols/keeper.md +829 -0
- package/protocols/markdown.md +80 -0
- package/protocols/memory.md +419 -0
- package/protocols/mentions.md +93 -0
- package/protocols/payloads.md +53 -0
- package/protocols/prelude.md +91 -0
- package/protocols/ramify.md +105 -0
- package/protocols/review.md +356 -0
- package/protocols/rrr.md +232 -0
- package/protocols/scribe.md +429 -0
- package/protocols/summarize.md +203 -0
- package/protocols/twin.md +174 -0
- package/protocols/wiki.md +438 -0
- package/protocols/worker.md +38 -0
- package/wiki/shared/append-only-event-log.md +148 -0
- package/wiki/shared/index.md +48 -0
- package/wiki/shared/owning-your-narratives.md +161 -0
- package/wiki/shared/premature-ontologies.md +435 -0
- package/wiki/shared/sedimentary-superintelligence.md +128 -0
- package/wiki/shared/sequestering-the-narrative.md +143 -0
- package/wiki/shared/why-wikis-work.md +216 -0
- package/.travis.yml +0 -7
- package/lib/brane.cjs.js +0 -12
- package/lib/brane.cjs.js.map +0 -1
- package/lib/brane.d.ts +0 -11
- package/lib/brane.js +0 -6
- package/lib/brane.js.map +0 -1
- package/lib/tests.d.ts +0 -1
- package/lib/tests.js +0 -8
- package/lib/tests.js.map +0 -1
- package/rollup.config.js +0 -21
- package/scripts/test.sh +0 -8
- package/src/brane.ts +0 -21
- package/src/tests.ts +0 -8
- package/tsconfig.json +0 -16
package/dist/math.js
ADDED
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
/** Server-side math: the micromark hookup for the HTML export, and the
|
|
2
|
+
* self-contained stylesheet that makes the export stand on its own.
|
|
3
|
+
*
|
|
4
|
+
* The rendering itself lives in `shared/markdown-math.ts`, because the web app
|
|
5
|
+
* runs the identical call. What's here is the two things only the export
|
|
6
|
+
* needs: a micromark HTML extension, and KaTeX's fonts turned into bytes the
|
|
7
|
+
* file carries with it.
|
|
8
|
+
*/
|
|
9
|
+
import { createRequire } from "node:module";
|
|
10
|
+
import { readFileSync } from "node:fs";
|
|
11
|
+
import { dirname, join } from "node:path";
|
|
12
|
+
import { renderMathHtml } from "./shared/markdown-math.js";
|
|
13
|
+
const require = createRequire(import.meta.url);
|
|
14
|
+
const KATEX_DIST = join(dirname(require.resolve("katex/package.json")), "dist");
|
|
15
|
+
/** micromark HTML extension for math.
|
|
16
|
+
*
|
|
17
|
+
* Structurally this is `micromark-extension-math`'s own `mathHtml()`, with one
|
|
18
|
+
* substitution: unparseable LaTeX comes back as the literal source rather than
|
|
19
|
+
* as KaTeX's red error markup (see `renderMathHtml`). Running our own also
|
|
20
|
+
* keeps the export and the app on one renderer call with one set of options,
|
|
21
|
+
* which is the property the whole design rests on.
|
|
22
|
+
*
|
|
23
|
+
* `onError` is how `diagnoseMarkdown` collects those failures instead of
|
|
24
|
+
* letting them pass silently. It rides on the extension rather than on a
|
|
25
|
+
* parallel scan for the same reason the substitution above exists: a warning
|
|
26
|
+
* fires only for a run this exact parse decided was math, so it can never
|
|
27
|
+
* disagree with what the reader will see.
|
|
28
|
+
*/
|
|
29
|
+
export function mathHtmlExtension(onError) {
|
|
30
|
+
return {
|
|
31
|
+
enter: {
|
|
32
|
+
mathFlow() {
|
|
33
|
+
this.lineEndingIfNeeded();
|
|
34
|
+
this.tag('<div class="math math-display">');
|
|
35
|
+
},
|
|
36
|
+
mathFlowFenceMeta() {
|
|
37
|
+
this.buffer();
|
|
38
|
+
},
|
|
39
|
+
mathText() {
|
|
40
|
+
this.tag('<span class="math math-inline">');
|
|
41
|
+
this.buffer();
|
|
42
|
+
},
|
|
43
|
+
},
|
|
44
|
+
exit: {
|
|
45
|
+
mathFlow() {
|
|
46
|
+
const value = this.resume();
|
|
47
|
+
this.tag(renderMathHtml(value.replace(/(?:\r?\n|\r)$/, ""), true, onError));
|
|
48
|
+
this.tag("</div>");
|
|
49
|
+
this.setData("mathFlowOpen");
|
|
50
|
+
this.setData("slurpOneLineEnding");
|
|
51
|
+
},
|
|
52
|
+
mathFlowFence() {
|
|
53
|
+
if (!this.getData("mathFlowOpen")) {
|
|
54
|
+
this.setData("mathFlowOpen", true);
|
|
55
|
+
this.setData("slurpOneLineEnding", true);
|
|
56
|
+
this.buffer();
|
|
57
|
+
}
|
|
58
|
+
},
|
|
59
|
+
mathFlowFenceMeta() {
|
|
60
|
+
this.resume();
|
|
61
|
+
},
|
|
62
|
+
mathFlowValue(token) {
|
|
63
|
+
this.raw(this.sliceSerialize(token));
|
|
64
|
+
},
|
|
65
|
+
mathText() {
|
|
66
|
+
const value = this.resume();
|
|
67
|
+
this.tag(renderMathHtml(value, false, onError));
|
|
68
|
+
this.tag("</span>");
|
|
69
|
+
},
|
|
70
|
+
mathTextData(token) {
|
|
71
|
+
this.raw(this.sliceSerialize(token));
|
|
72
|
+
},
|
|
73
|
+
},
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
let cachedCss = null;
|
|
77
|
+
function katexCss() {
|
|
78
|
+
if (cachedCss === null)
|
|
79
|
+
cachedCss = readFileSync(join(KATEX_DIST, "katex.min.css"), "utf8");
|
|
80
|
+
return cachedCss;
|
|
81
|
+
}
|
|
82
|
+
/** Every `@font-face` in KaTeX's stylesheet, with the woff2 it points at.
|
|
83
|
+
* KaTeX ships woff and ttf alongside for ancient browsers; a data URI only
|
|
84
|
+
* needs the one format, and woff2 is both the smallest and universally
|
|
85
|
+
* supported by anything that would open one of these files. */
|
|
86
|
+
function fontFaces(css) {
|
|
87
|
+
const faces = [];
|
|
88
|
+
for (const m of css.matchAll(/@font-face\{[^}]*\}/g)) {
|
|
89
|
+
const block = m[0];
|
|
90
|
+
const family = block.match(/font-family\s*:\s*['"]?(KaTeX_[A-Za-z0-9]+)/)?.[1];
|
|
91
|
+
const woff2 = block.match(/url\(([^)]*\.woff2)\)/)?.[1].replace(/["']/g, "");
|
|
92
|
+
if (family && woff2)
|
|
93
|
+
faces.push({ block, family, woff2 });
|
|
94
|
+
}
|
|
95
|
+
return faces;
|
|
96
|
+
}
|
|
97
|
+
/** Which KaTeX font families a rendered document actually reaches for.
|
|
98
|
+
*
|
|
99
|
+
* Derived from the stylesheet rather than hardcoded, so it can't drift when
|
|
100
|
+
* KaTeX is upgraded: collect every rule that sets a `KaTeX_*` family — the
|
|
101
|
+
* `font:` shorthand on `.katex` counts, which is how KaTeX_Main gets in — and
|
|
102
|
+
* keep the family if any of that rule's selectors has all its classes present
|
|
103
|
+
* in the document. */
|
|
104
|
+
function familiesUsed(css, html) {
|
|
105
|
+
const present = new Set();
|
|
106
|
+
for (const m of html.matchAll(/class="([^"]*)"/g)) {
|
|
107
|
+
for (const c of m[1].split(/\s+/))
|
|
108
|
+
if (c)
|
|
109
|
+
present.add(c);
|
|
110
|
+
}
|
|
111
|
+
const used = new Set();
|
|
112
|
+
for (const m of css.matchAll(/([^{}]+)\{([^{}]*)\}/g)) {
|
|
113
|
+
const family = m[2].match(/font(?:-family)?\s*:[^;]*?(KaTeX_[A-Za-z0-9]+)/)?.[1];
|
|
114
|
+
if (!family || used.has(family))
|
|
115
|
+
continue;
|
|
116
|
+
for (const selector of m[1].split(",")) {
|
|
117
|
+
const classes = [...selector.matchAll(/\.([A-Za-z0-9_-]+)/g)].map((x) => x[1]);
|
|
118
|
+
if (classes.length && classes.every((c) => present.has(c))) {
|
|
119
|
+
used.add(family);
|
|
120
|
+
break;
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
return used;
|
|
125
|
+
}
|
|
126
|
+
const fontDataUris = new Map();
|
|
127
|
+
function fontDataUri(relPath) {
|
|
128
|
+
let uri = fontDataUris.get(relPath);
|
|
129
|
+
if (uri === undefined) {
|
|
130
|
+
const buf = readFileSync(join(KATEX_DIST, relPath));
|
|
131
|
+
uri = `data:font/woff2;base64,${buf.toString("base64")}`;
|
|
132
|
+
fontDataUris.set(relPath, uri);
|
|
133
|
+
}
|
|
134
|
+
return uri;
|
|
135
|
+
}
|
|
136
|
+
/** A `<style>` block that renders `html`'s math with no network and no JS, or
|
|
137
|
+
* null when the document contains no math at all.
|
|
138
|
+
*
|
|
139
|
+
* KaTeX's span layout needs its stylesheet *and* its fonts, and a single-file
|
|
140
|
+
* export can only carry a font as base64 — so the whole point here is to carry
|
|
141
|
+
* as few as possible. Inlining all twenty faces costs ~338 KB; restricting to
|
|
142
|
+
* the families the document actually reaches for typically lands near 166 KB,
|
|
143
|
+
* and a narrative with no math pays nothing because we never get past the
|
|
144
|
+
* first line.
|
|
145
|
+
*
|
|
146
|
+
* The alternative — emitting MathML and letting the reader's browser find a
|
|
147
|
+
* math font — is smaller still, but it renders differently on every OS. These
|
|
148
|
+
* files get handed to people whose machines we don't control, so the fonts
|
|
149
|
+
* ride along.
|
|
150
|
+
*/
|
|
151
|
+
export function katexStyleFor(html) {
|
|
152
|
+
if (!html.includes("katex"))
|
|
153
|
+
return null;
|
|
154
|
+
const css = katexCss();
|
|
155
|
+
const used = familiesUsed(css, html);
|
|
156
|
+
if (used.size === 0)
|
|
157
|
+
return null;
|
|
158
|
+
let out = css;
|
|
159
|
+
for (const face of fontFaces(css)) {
|
|
160
|
+
if (!used.has(face.family)) {
|
|
161
|
+
out = out.replace(face.block, "");
|
|
162
|
+
continue;
|
|
163
|
+
}
|
|
164
|
+
out = out.replace(face.block, face.block.replace(/src\s*:[^;}]*/, `src:url(${fontDataUri(face.woff2)}) format("woff2")`));
|
|
165
|
+
}
|
|
166
|
+
return `<style>\n${out}\n</style>`;
|
|
167
|
+
}
|
|
168
|
+
//# sourceMappingURL=math.js.map
|