From 3d50c332d2dd1ff80a0b79e8914d17020a4a5b4b Mon Sep 17 00:00:00 2001
From: Tova <291672564+Tova22@users.noreply.github.com>
Date: Mon, 9 Nov 2026 09:05:52 +0200
Subject: [PATCH 01/42] Add font family and size to the Markdown and LaTeX
Visual editors
- Shared Text > Font / Size menu: three generic families, ten sizes.
- Markdown: recognised marks on a
selection; front matter keys and a Document settings card for the
whole document.
- LaTeX Visual: menu wired to the existing size and family marks; a
Font field in Document settings (\familydefault).
- PDF export keeps the recognised span styles before sanitising.
Co-Authored-By: Claude Sonnet 5.5
---
.../documentPage/ScientDocumentPage.tsx | 12 ++
.../scient/documentPage/documentFont.test.ts | 73 ++++++++++
.../src/scient/documentPage/documentFont.ts | 67 +++++++++
.../documentPage/scient-document-page.css | 37 +++++
.../scient/latex/LatexDocumentSettings.tsx | 125 +++++++++--------
.../src/scient/latex/LatexVisualEditor.tsx | 11 ++
.../latex/latexDocumentLayoutSettings.ts | 19 +++
.../latex/latexDocumentSettings.test.ts | 23 ++++
.../src/scient/latex/latexEditingTarget.ts | 9 ++
.../web/src/scient/latex/latexFontCommands.ts | 46 +++++++
.../markdownEditor/documentFont.test.ts | 39 ++++++
.../src/scient/markdownEditor/documentFont.ts | 64 +++++++++
.../markdownEditor/prosemirror/commands.ts | 86 ++++++++++++
.../prosemirror/documentFontPresentation.ts | 20 +++
.../prosemirror/fontStyles.test.ts | 128 ++++++++++++++++++
.../markdownEditor/prosemirror/plugins.ts | 2 +
.../markdownEditor/prosemirror/schema.ts | 103 +++++++++++++-
.../scient/markdownEditor/prosemirror/view.ts | 35 +++++
.../markdownEditor/scient-markdown-editor.css | 35 +++++
.../ui/MarkdownDocumentSettings.tsx | 111 +++++++++++++++
.../ui/ScientMarkdownControls.tsx | 42 ++++++
.../scient/writing/DocumentSettingsField.tsx | 14 ++
apps/web/src/scient/writing/FontMenuItems.tsx | 67 +++++++++
apps/web/src/scient/writing/TextMenu.tsx | 2 +
apps/web/src/scient/writing/fontChoices.ts | 45 ++++++
packages/scient-markdown/src/fontSpans.ts | 59 ++++++++
packages/scient-markdown/src/index.ts | 12 ++
.../scient-markdown/src/sourceLedger.test.ts | 12 ++
packages/scient-markdown/src/sourceLedger.ts | 39 +++++-
29 files changed, 1273 insertions(+), 64 deletions(-)
create mode 100644 apps/web/src/scient/documentPage/documentFont.test.ts
create mode 100644 apps/web/src/scient/documentPage/documentFont.ts
create mode 100644 apps/web/src/scient/latex/latexFontCommands.ts
create mode 100644 apps/web/src/scient/markdownEditor/documentFont.test.ts
create mode 100644 apps/web/src/scient/markdownEditor/documentFont.ts
create mode 100644 apps/web/src/scient/markdownEditor/prosemirror/documentFontPresentation.ts
create mode 100644 apps/web/src/scient/markdownEditor/prosemirror/fontStyles.test.ts
create mode 100644 apps/web/src/scient/markdownEditor/ui/MarkdownDocumentSettings.tsx
create mode 100644 apps/web/src/scient/writing/DocumentSettingsField.tsx
create mode 100644 apps/web/src/scient/writing/FontMenuItems.tsx
create mode 100644 apps/web/src/scient/writing/fontChoices.ts
create mode 100644 packages/scient-markdown/src/fontSpans.ts
diff --git a/apps/web/src/scient/documentPage/ScientDocumentPage.tsx b/apps/web/src/scient/documentPage/ScientDocumentPage.tsx
index 13944ada91..5d87691e3f 100644
--- a/apps/web/src/scient/documentPage/ScientDocumentPage.tsx
+++ b/apps/web/src/scient/documentPage/ScientDocumentPage.tsx
@@ -31,6 +31,12 @@ import { scientMathMarkdownText, scientMathRemarkPluginsForMessage } from "../ma
import { resolveScientRichFenceKind } from "../presentation/scientRichFenceKind";
import { scientMarkdownRemarkPlugins } from "../markdown/scientMarkdownProfiles";
import type { DocumentPageTracker } from "./documentPageReadiness";
+import {
+ DOCUMENT_FONT_SANITIZE_ATTRIBUTES,
+ documentFontOfMarkdown,
+ rehypeApplyFontSpans,
+ rehypeMarkFontSpans,
+} from "./documentFont";
import { remarkDocumentFrontMatter } from "./remarkDocumentFrontMatter";
import "../bidi/scient-bidi.css";
@@ -75,6 +81,7 @@ const DOCUMENT_SANITIZE_SCHEMA = {
attributes: {
...defaultSchema.attributes,
blockquote: [...(defaultSchema.attributes?.blockquote ?? []), "dataAlert"],
+ span: [...(defaultSchema.attributes?.span ?? []), ...DOCUMENT_FONT_SANITIZE_ATTRIBUTES.span],
},
protocols: {
...defaultSchema.protocols,
@@ -500,6 +507,7 @@ export function ScientDocumentPage(props: ScientDocumentPageProps) {
input.markdown,
);
const direction = resolveMarkdownDirection(input.markdown, input.direction);
+ const documentFont = documentFontOfMarkdown(input.markdown);
const context = {
assets: new Map(input.assets.map((asset) => [asset.id, asset])),
assetBaseUrl: props.inputUrl,
@@ -509,7 +517,9 @@ export function ScientDocumentPage(props: ScientDocumentPageProps) {
const rehypePlugins: NonNullable = [
rehypeRaw,
[rehypeReportUnsupportedHtml, { tracker }],
+ rehypeMarkFontSpans,
[rehypeSanitize, DOCUMENT_SANITIZE_SCHEMA],
+ rehypeApplyFontSpans,
[rehypeEnsureTitle, { title: input.title }],
[rehypeScientBidi, { direction, requestedDirection: input.direction }],
];
@@ -526,6 +536,8 @@ export function ScientDocumentPage(props: ScientDocumentPageProps) {
dir={direction}
lang={input.language ?? undefined}
data-scient-document={input.documentKind}
+ data-scient-font-size={documentFont.fontSize ?? undefined}
+ data-scient-font-family={documentFont.fontFamily ?? undefined}
>
{
+ const processor = unified()
+ .use(remarkParse)
+ .use(remarkRehype, { allowDangerousHtml: true })
+ .use(rehypeRaw)
+ .use(rehypeMarkFontSpans)
+ .use(rehypeSanitize, schema)
+ .use(rehypeApplyFontSpans);
+ const tree = await processor.run(processor.parse(markdown));
+ return toHtml(tree);
+}
+
+describe("export of local font styles", () => {
+ it("keeps the exact size and family styles through the sanitizer", async () => {
+ expect(await exported('a big b')).toContain(
+ 'big',
+ );
+ expect(await exported('x')).toContain(
+ 'style="font-family:serif,',
+ );
+ });
+
+ it.each([
+ 'x',
+ 'x',
+ 'x',
+ 'x',
+ ])("removes any other style: %s", async (markdown) => {
+ const html = await exported(markdown);
+ expect(html).not.toContain("color");
+ expect(html).not.toContain("Arial");
+ expect(html).not.toContain("3em");
+ expect(html).toContain(">x");
+ });
+
+ it("ignores a font data attribute written directly in the document", async () => {
+ const html = await exported('x');
+ expect(html).not.toContain("style=");
+ expect(html).not.toContain("data-scient-font-size");
+ });
+
+ it("reads the whole-document keys of a leading front matter only", () => {
+ expect(
+ documentFontOfMarkdown("---\ntitle: T\nfont-size: 12pt\nfont-family: monospace\n---\n\ntext"),
+ ).toEqual({ fontFamily: "monospace", fontSize: "12pt" });
+ expect(documentFontOfMarkdown("text\n\n---\nfont-size: 12pt\n---\n")).toEqual({
+ fontFamily: null,
+ fontSize: null,
+ });
+ });
+});
diff --git a/apps/web/src/scient/documentPage/documentFont.ts b/apps/web/src/scient/documentPage/documentFont.ts
new file mode 100644
index 0000000000..e5cbf7c8ce
--- /dev/null
+++ b/apps/web/src/scient/documentPage/documentFont.ts
@@ -0,0 +1,67 @@
+import {
+ MARKDOWN_FONT_SIZES,
+ markdownFontSpanStyle,
+ parseMarkdownFontSpanStyle,
+} from "@scientfactory/scient-markdown";
+import { readMarkdownDocumentFont } from "../markdownEditor/documentFont";
+import { isFontFamilyName } from "../writing/fontChoices";
+
+/**
+ * Font styles of an exported Markdown document: the local `` forms
+ * Scient writes, and the whole-document front matter keys. Only the exact
+ * listed values get through the sanitizer; any other CSS is still removed.
+ */
+
+interface HastNode {
+ type?: string;
+ tagName?: string;
+ properties?: Record;
+ children?: HastNode[];
+}
+
+export const DOCUMENT_FONT_SANITIZE_ATTRIBUTES = {
+ span: ["dataScientFontSize", "dataScientFontFamily"],
+} as const;
+
+function visitSpans(node: HastNode, visit: (properties: Record) => void) {
+ if (node.type === "element" && node.tagName === "span") visit((node.properties ??= {}));
+ node.children?.forEach((child) => visitSpans(child, visit));
+}
+
+/** Before the sanitizer: move a recognised style into data attributes it allows. */
+export function rehypeMarkFontSpans() {
+ return (tree: HastNode) =>
+ visitSpans(tree, (properties) => {
+ // Only the style may ask for a font; the document's own attributes are not trusted.
+ delete properties.dataScientFontSize;
+ delete properties.dataScientFontFamily;
+ const style = typeof properties.style === "string" ? properties.style.trim() : "";
+ const span = parseMarkdownFontSpanStyle(style);
+ if (span === null) return;
+ if ("size" in span) properties.dataScientFontSize = span.size;
+ else properties.dataScientFontFamily = span.family;
+ });
+}
+
+/** After the sanitizer: turn the surviving data attributes back into the style they stand for. */
+export function rehypeApplyFontSpans() {
+ return (tree: HastNode) =>
+ visitSpans(tree, (properties) => {
+ const size = properties.dataScientFontSize;
+ const family = properties.dataScientFontFamily;
+ delete properties.dataScientFontSize;
+ delete properties.dataScientFontFamily;
+ const sizeName = MARKDOWN_FONT_SIZES.find(({ name }) => name === size)?.name;
+ if (sizeName !== undefined) {
+ properties.style = markdownFontSpanStyle({ size: sizeName });
+ } else if (typeof family === "string" && isFontFamilyName(family)) {
+ properties.style = `font-family:${family}, "Noto Sans Hebrew", "Arial Hebrew", "Noto Sans Arabic"`;
+ }
+ });
+}
+
+/** The leading `---` block of the document, if it starts with one. */
+export function documentFontOfMarkdown(markdown: string) {
+ const front = /^---[ \t]*\r?\n[\s\S]*?\r?\n---[ \t]*(?:\r?\n|$)/u.exec(markdown);
+ return readMarkdownDocumentFont(front?.[0] ?? null);
+}
diff --git a/apps/web/src/scient/documentPage/scient-document-page.css b/apps/web/src/scient/documentPage/scient-document-page.css
index f857ef20e4..3943ed9b12 100644
--- a/apps/web/src/scient/documentPage/scient-document-page.css
+++ b/apps/web/src/scient/documentPage/scient-document-page.css
@@ -92,6 +92,43 @@ body {
font-size: 11pt;
}
+/* Whole-document font from the front matter (see documentFont.ts). */
+.scient-document[data-scient-font-size="10pt"] {
+ --scient-document-size-scale: calc(10 / 10.5);
+}
+.scient-document[data-scient-font-size="11pt"] {
+ --scient-document-size-scale: calc(11 / 10.5);
+}
+.scient-document[data-scient-font-size="12pt"] {
+ --scient-document-size-scale: calc(12 / 10.5);
+}
+.scient-document[data-scient-font-size] {
+ font-size: calc(10.5pt * var(--scient-document-size-scale));
+}
+.scient-document[data-scient-font-size] h1 {
+ font-size: calc(20pt * var(--scient-document-size-scale));
+}
+.scient-document[data-scient-font-size] h2 {
+ font-size: calc(15pt * var(--scient-document-size-scale));
+}
+.scient-document[data-scient-font-size] h3 {
+ font-size: calc(12.5pt * var(--scient-document-size-scale));
+}
+.scient-document[data-scient-font-size] :where(h4, h5, h6) {
+ font-size: calc(11pt * var(--scient-document-size-scale));
+}
+.scient-document[data-scient-font-family="serif"] {
+ font-family: serif, "Noto Sans Hebrew", "Arial Hebrew", "Noto Sans Arabic";
+}
+.scient-document[data-scient-font-family="sans-serif"] {
+ font-family: sans-serif, "Noto Sans Hebrew", "Arial Hebrew", "Noto Sans Arabic";
+}
+.scient-document[data-scient-font-family="monospace"] {
+ font-family:
+ ui-monospace, "SF Mono", Menlo, Consolas, "Liberation Mono", monospace, "Noto Sans Hebrew",
+ "Arial Hebrew";
+}
+
.scient-document :where(p, ul, ol, blockquote, .scient-document-table, figure) {
margin: 0 0 0.8em;
}
diff --git a/apps/web/src/scient/latex/LatexDocumentSettings.tsx b/apps/web/src/scient/latex/LatexDocumentSettings.tsx
index 822e23387f..e19e991a3f 100644
--- a/apps/web/src/scient/latex/LatexDocumentSettings.tsx
+++ b/apps/web/src/scient/latex/LatexDocumentSettings.tsx
@@ -1,7 +1,9 @@
-import { useState, type ReactNode } from "react";
+import { useState } from "react";
import { XIcon } from "lucide-react";
import { Button } from "~/components/ui/button";
import { Input } from "~/components/ui/input";
+import { DocumentSettingsField } from "../writing/DocumentSettingsField";
+import { FONT_FAMILIES } from "../writing/fontChoices";
import { LatexSelect } from "./LatexSelect";
import { latexVisualLayoutProfile, type LatexVisualLayoutUpdate } from "./latexVisualDocument";
import { LATEX_PAPER_SIZES } from "./latexVisualLayout";
@@ -53,16 +55,6 @@ function currentSettings(source: string) {
};
}
-/** A small label above its control. */
-function Field(props: { readonly label: string; readonly children: ReactNode }) {
- return (
-
- {props.label}
- {props.children}
-
- );
-}
-
/**
* Two or three choices side by side, drawn like the header's view switch:
* white, with a grey pill under the chosen one that slides when it changes.
@@ -232,7 +224,7 @@ export function LatexDocumentSettings(props: {
>
Fonts and compiler