Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
21 changes: 21 additions & 0 deletions integrations/code/media/katex.LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2013-2020 Khan Academy and other contributors

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
1 change: 1 addition & 0 deletions integrations/code/media/katex.min.css

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions integrations/code/media/katex.min.js

Large diffs are not rendered by default.

38 changes: 38 additions & 0 deletions integrations/code/media/preview.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
<base id="preview-base">
<link rel="stylesheet" href="__STYLESHEET__">
<link rel="stylesheet" href="__PALETTE__">
<link rel="stylesheet" href="__KATEX_STYLESHEET__">
<style>
img {
max-width: 100%;
Expand Down Expand Up @@ -42,6 +43,7 @@
}

</style>
<script nonce="__NONCE__" src="__KATEX__"></script>
</head>
<body
dir="ltr"
Expand Down Expand Up @@ -237,6 +239,41 @@
});
}

function typesetMath() {
const mathNodes = [...preview.querySelectorAll(".arithmatex")];
if (!window.katex) return;

try {
const delimiters = [
{ left: "$$", right: "$$", displayMode: true },
{ left: "$", right: "$", displayMode: false },
{ left: "\\[", right: "\\]", displayMode: true },
{ left: "\\(", right: "\\)", displayMode: false },
];
for (const node of mathNodes) {
const source = node.textContent.trim();
const delimiter = delimiters.find(
({ left, right }) =>
source.startsWith(left) && source.endsWith(right),
);
const expression = delimiter
? source.slice(
delimiter.left.length,
source.length - delimiter.right.length,
)
: source;
if (!expression) continue;
node.replaceChildren();
window.katex.render(expression, node, {
displayMode: delimiter?.displayMode ?? node.tagName === "DIV",
throwOnError: false,
});
}
} catch (error) {
// Ignore invalid expressions so one math block cannot break preview updates.
}
}

function utf8Index(text) {
const encoder = new TextEncoder();
const bytes = [];
Expand Down Expand Up @@ -468,6 +505,7 @@
preview.replaceChildren(
...sanitizeHtml(decorateHtml(update.html, update.mappings || [])),
);
typesetMath();
restoreUiState(renderedUri);
revealPendingFragment();
if (activeSourceOffset !== null) {
Expand Down
28 changes: 28 additions & 0 deletions integrations/code/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions integrations/code/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
"@types/semver": "^7.7.1",
"@types/vscode": "^1.100.0",
"esbuild": "^0.28.1",
"katex": "0.16.22",
"typescript": "^6.0.3"
},
"allowScripts": {
Expand Down
14 changes: 11 additions & 3 deletions integrations/code/src/preview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -532,12 +532,18 @@ function previewHtml(
const palette = webview.asWebviewUri(
vscode.Uri.joinPath(extensionUri, "media", "palette.css"),
);
const katex = webview.asWebviewUri(
vscode.Uri.joinPath(extensionUri, "media", "katex.min.js"),
);
const katexStylesheet = webview.asWebviewUri(
vscode.Uri.joinPath(extensionUri, "media", "katex.min.css"),
);
const csp = [
"default-src 'none'",
`img-src ${webview.cspSource} data: https:`,
`style-src ${webview.cspSource} 'unsafe-inline'`,
"font-src https://zensical.org data:",
`script-src 'nonce-${nonce}'`,
`font-src ${webview.cspSource} data:`,
`script-src 'nonce-${nonce}' ${webview.cspSource}`,
`base-uri ${webview.cspSource}`,
"form-action 'none'",
].join("; ");
Expand All @@ -549,7 +555,9 @@ function previewHtml(
.replaceAll("__CSP__", csp)
.replaceAll("__NONCE__", nonce)
.replaceAll("__STYLESHEET__", stylesheet.toString())
.replaceAll("__PALETTE__", palette.toString());
.replaceAll("__PALETTE__", palette.toString())
.replaceAll("__KATEX__", katex.toString())
.replaceAll("__KATEX_STYLESHEET__", katexStylesheet.toString());
}

/**
Expand Down