frontend: typeset the LaTeX, and let a sentence start a new line

Two things a paper needs that the paragraph model could not express.

**Formulas.** A sentence stores the LaTeX the writer typed — `$\ell$`,
`$$ … \tag{1} $$` — because that is the editable form, and it was being printed
as source. `src/utils/math.ts` now splits a sentence into text and formulas,
gives the formulas to KaTeX and escapes everything else; `math-error` in red
when KaTeX cannot read one, rather than an exception that blanks the paragraph
(the paper in the database has 106 sentences with formulas, 29 of them
display). Four delimiters are recognised — `$$…$$`, `\[…\]`, `$…$`, `\(…\)` —
and an unmatched `$` is left alone instead of swallowing the rest of the
paragraph. A display formula is a block, so it takes its own line and centres
itself without any flagging; text goes through `v-html` escaped, and KaTeX runs
with trust off.

**Line breaks.** A paragraph is its sentences run together, which is right for
prose and wrong for a display formula, a list of items or an author line.
`paper_sentence.line_break_before` is that request: a flag rather than a
newline inside `content`, because content folds its whitespace on purpose (a
sentence really is one line) and because a break hidden in a string is one
nobody can see or remove. It prints a `<br>`, it is a checkbox on the
sentence's row in the editor, and it *replaces* the separator rather than
adding to it — the new line already separates, and a space as well would indent
the line by a blank.

Verified against the real paper in the browser: 214 inline formulas and 29
display formulas typeset with zero failures and no `$$` left on the page; a
sentence flagged for a break lands on its own line while its neighbour stays
inline; the display formula centres on a line of its own and the sentence after
it starts below. 54 paper checks (2 new), 61 reference checks and 76 MCP checks
pass, with `vue-tsc` and the build clean.

Also: `.pnpm-store/` is ignored — pnpm put its content-addressed store in the
project root rather than the home directory.
This commit is contained in:
2026-09-23 11:30:29 +08:00
parent b97aab6f30
commit 960450f744
14 changed files with 406 additions and 22 deletions
+40
View File
@@ -306,6 +306,23 @@ stop (`。`) and nothing else — deliberately conservative: an English abstract
survives that migration as a single row rather than being cut at the first
`et al.`, and one long line is a line the writer can split by hand.
#### A line break is asked for, never guessed
A paragraph is its sentences run together — that is the model. Everything a
paper also puts *inside* a paragraph that is not prose (a display formula, a
list of items, an author line) therefore needs a way to say "start a new line
here", and that is `paper_sentence.line_break_before`.
It is a flag rather than a newline inside `content` for two reasons. Content
folds its whitespace, deliberately, so that a sentence really is one line and a
pasted newline cannot split one in half; and a break buried in a string is a
break nobody can see, count or remove. A flag prints a `<br>` and is visible in
the editor as a checkbox on the sentence's row.
A break also **replaces** the separator rather than adding to it: the new line
already separates the two sentences, and a space as well would indent the line
by one blank.
#### What goes at the seam
A paragraph is printed by concatenating its sentences in `sort` order, so the
@@ -662,6 +679,29 @@ are not lists (the paper document) keep scrolling normally.
| `/references` | reference table + CRUD (`?tag=<id>` filters, `?new=1` opens the create dialog) | `references` |
| `/references/tags` | tag library + CRUD (the UI label is 标签管理) | `references` |
### Sentences are stored as LaTeX and typeset on read
A formula is stored exactly as the writer typed it — `$\ell$`, `$$ … \tag{1} $$`
— because that is the form that can be edited; an equation image cannot be
changed by one character. Typesetting happens when the paper is read, in
`src/utils/math.ts`, which splits a sentence into text and formulas, hands the
formulas to KaTeX and escapes everything else.
Four delimiters are recognised, because a writer's habit decides which they use:
`$$…$$` and `\[…\]` for display, `$…$` and `\(…\)` for inline. An unmatched
`$` is left alone rather than swallowing the rest of the paragraph.
Two rules are worth knowing when reading the renderer:
- **A display formula is a block.** It takes its own line whether or not the
sentence asked for a break, because that is what "display" means — so the
formulas in a paper sit centred on their own lines with no extra flagging.
- **KaTeX never throws.** `throwOnError: false` renders a formula it cannot read
as its own source in red, so one typo shows itself instead of blanking the
paragraph the writer is looking at. Text segments are escaped before they go
through `v-html`, and KaTeX runs with `trust` off, so a sentence cannot inject
markup.
### The paper view
`PapersView` is the library; `papers/PaperDetailView` is the writing surface.