Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TeX SSR #150

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
clarify how we test for static contents; it's a bit of a hack, maybe…
  • Loading branch information
Fil committed Nov 12, 2023
commit 9369125c5552e26bdb78d481f058fc83ff13292a
2 changes: 1 addition & 1 deletion public/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ async function mermaid() {

export function define(cell) {
const {id, inline, inputs = [], outputs = [], files = [], databases = [], body} = cell;
if (body === undefined) return;
if (body == null) return;
const variables = [];
cellsById.get(id)?.variables.forEach((v) => v.delete());
cellsById.set(id, {cell, variables});
Expand Down
14 changes: 9 additions & 5 deletions src/markdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,13 @@ function getLiveSource(content, language, option): {source?: string; html?: stri
}

function maybeStaticTeX(content, displayMode) {
// We try SSR first. katex.renderToString errors when the expression contains
// some ${interpolation}, so this guarantees that interpolations will be
// handled in the browser. By way of consequence, TeX errors stemming from
// static text (e.g., ParseError on tex`\left{x}`) are handled in the browser,
// and don't stop the build process.
try {
// TODO smarter detection of ${} contents
// TODO smarter insertion of the TeX stylesheet
// TODO: unique insertion of the TeX stylesheet?
return {
html:
katex.renderToString(content, {displayMode}) +
Expand Down Expand Up @@ -130,7 +134,7 @@ function makeFenceRenderer(root: string, baseRenderer: RenderRule, sourcePath: s
result += `<div id="cell-${id}" class="observablehq observablehq--block"></div>\n`;
count++;
}
if (html !== undefined) result += html;
if (html != null) result += html;
if (source == null || option === "show") {
result += baseRenderer(tokens, idx, options, context, self);
count++;
Expand Down Expand Up @@ -276,9 +280,9 @@ function makePlaceholderRenderer(root: string, sourcePath: string): RenderRule {
const token = tokens[idx];

// inline TeX?
if (token.content.match(/^tex[`]/)) {
if (token.content.startsWith("tex`") && token.content.endsWith("`")) {
const {html} = maybeStaticTeX(token.content.slice(4, -1), false);
if (html !== undefined) return `<span id="cell-${id}">${html}</span>`;
if (html != null) return `<span id="cell-${id}">${html}</span>`;
}

const transpile = transpileJavaScript(token.content, {
Expand Down
Loading