Files
paper-doc/frontend/src/components/papers/PaperParagraph.vue
T
govin 960450f744 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.
2026-09-23 11:30:29 +08:00

325 lines
10 KiB
Vue
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<script setup lang="ts">
/**
* One paragraph of a paper, as the reader sees it.
*
* Three things about this component carry the feature's contracts:
*
* 1. **The structure is never skipped.** A paragraph with no sentences still
* renders its heading, because the paper's shape comes from its template and
* an unwritten paragraph is still a paragraph — and then prints nothing
* under it. A blank paragraph is blank: some positions in an outline are
* meant to stay empty, and placeholder text would end up in the paper.
* 2. **A paragraph is reassembled from its sentences.** They are printed in
* `sort` order, and the only thing between them is the server's
* `separator_before` — a space for English, nothing for Chinese. This
* component adds no spacing of its own, because a client that guessed would
* space one paragraph differently from the next reader.
* 3. **Nothing but the writing is printed.** No position numbers, no ids, no
* counts — this is the paper, not the editor. `sort` is a writing detail and
* the places that show it are the paragraph dialog (its title and every
* sentence row) and the list, which is where it is acted on.
*
* A sentence that quotes something gets a superscript marker numbered by the
* paper's citation list, so the marker and the 参考文献 entry cannot disagree.
*/
import { computed } from 'vue'
import { Edit } from '@element-plus/icons-vue'
import type { PaperParagraph } from '@/api/papers'
import { PARAGRAPH_UNSET_LABEL, levelIndent } from '@/utils/format'
import { escapeHtml, hasDisplayMath, renderRich } from '@/utils/math'
const props = defineProps<{
paragraph: PaperParagraph
/** Citation id -> the number shown in the text, assigned by reading order. */
citationIndex: Record<number, number>
/** Reference id -> its formatted entry, so a marker can say what it points at. */
referenceText: Record<number, string>
}>()
defineEmits<{ edit: [paragraph: PaperParagraph] }>()
/** The heading's own typography, read from the template's field. */
const headingStyle = computed(() => {
const style: Record<string, string> = {}
if (props.paragraph.font_size != null) style.fontSize = `${props.paragraph.font_size}pt`
if (props.paragraph.font_color) style.color = props.paragraph.font_color
return style
})
/** Indentation follows the field's level, exactly as the template outline does. */
const headingIndent = computed(() => levelIndent(props.paragraph.level))
/**
* Every sentence, prepared for printing.
*
* Typesetting happens here rather than in the template so it runs once per
* sentence instead of on every re-render, and so the line-break rule — a break
* replaces the separator, it does not add to it — is stated in one place.
*/
interface RenderedSentence {
sentence: PaperParagraph['sentences'][number]
/** The sentence as HTML: its separator escaped, its formulas typeset. */
html: string
/** A `$$ … $$` formula, which takes a line of its own by definition. */
display: boolean
/** The writer asked for a line break before this sentence. */
lineBreak: boolean
}
const rendered = computed<RenderedSentence[]>(() =>
props.paragraph.sentences.map((sentence) => ({
sentence,
html:
escapeHtml(sentence.line_break_before ? '' : sentence.separator_before) +
renderRich(sentence.content),
display: hasDisplayMath(sentence.content),
lineBreak: sentence.line_break_before,
})),
)
/** Whether anything has actually been written here. */
const hasContent = computed(() =>
rendered.value.some(
(item) =>
item.sentence.content.trim().length > 0 || item.sentence.citations.length > 0,
),
)
/** Anchor id, so a citation can point back at the paragraph it came from. */
const anchor = computed(() => `paragraph-${props.paragraph.paper_template_filed_sort}`)
</script>
<template>
<section :id="anchor" class="paragraph" :class="{ 'is-unset': !paragraph.matched }">
<header class="paragraph-head">
<!-- The heading is the template's own text and nothing else. The
paragraph's position is a writing detail — the editor shows it, on
the dialog and beside every sentence — and printing it next to every
heading puts an implementation number in the middle of the paper. -->
<h3 class="paragraph-title" :style="{ paddingLeft: headingIndent }">
<span v-if="paragraph.name" class="paragraph-name" :style="headingStyle">
{{ paragraph.name }}
</span>
<span v-else class="paragraph-name paragraph-name--unset" :style="headingStyle">
{{ PARAGRAPH_UNSET_LABEL }}
</span>
</h3>
<div class="paragraph-actions">
<el-tag v-if="!paragraph.matched" size="small" type="warning" effect="plain">
模板无此段
</el-tag>
<span v-else class="paragraph-count">{{ paragraph.sentences.length }} 句</span>
<!-- The edit button sits beside every paragraph, written or not. -->
<el-button
size="small"
type="primary"
plain
:icon="Edit"
@click="$emit('edit', paragraph)"
>
编辑
</el-button>
</div>
</header>
<div v-if="hasContent" class="paragraph-body">
<template v-for="item in rendered" :key="item.sentence.id">
<!-- A display formula is a block by definition, so it takes its own
line whether or not the writer asked for a break. -->
<span
v-if="item.display"
class="sentence sentence--display"
v-html="item.html"
></span>
<template v-else>
<br v-if="item.lineBreak" />
<span class="sentence" v-html="item.html"></span>
</template>
<el-tooltip
v-for="citation in item.sentence.citations"
:key="citation.id"
placement="top"
:show-after="120"
>
<template #content>
<div class="cite-tip">
<div class="cite-tip-head">[{{ citationIndex[citation.id] ?? '?' }}]</div>
<!-- What the number means: the entry as the bibliography prints
it, not its database id. -->
<div
v-if="citation.reference_id != null && referenceText[citation.reference_id]"
class="cite-tip-entry"
>
{{ referenceText[citation.reference_id] }}
</div>
<div v-else class="cite-tip-unlinked">
这处引用还没有关联文献库条目
</div>
<div class="cite-tip-quote">「{{ citation.quote }}」</div>
</div>
</template>
<sup class="cite-mark">{{ citationIndex[citation.id] ?? '?' }}</sup>
</el-tooltip>
</template>
</div>
<!-- Nothing below the heading when there is nothing to print. An empty
paragraph is a position in an outline, and in an academic paper some
of those are meant to be empty — a separator, a placeholder the
template reserves, a section the writer has not reached yet. Printing
「(本段暂无内容)」 into a paper puts words there that the paper did
not ask for, and on export they would go with it. The blank body is
the honest rendering of a blank paragraph. -->
</section>
</template>
<style scoped>
.paragraph {
padding: 14px 16px;
border-radius: 8px;
border: 1px solid transparent;
transition: background-color 0.15s ease, border-color 0.15s ease;
}
.paragraph + .paragraph {
border-top: 1px solid var(--el-border-color-lighter);
border-top-left-radius: 0;
border-top-right-radius: 0;
}
.paragraph:hover {
background-color: var(--el-fill-color-lighter);
}
.paragraph.is-unset {
background-color: var(--el-color-warning-light-9);
}
.paragraph.is-unset:hover {
background-color: var(--el-color-warning-light-8);
}
.paragraph-head {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
}
.paragraph-title {
display: flex;
align-items: baseline;
gap: 8px;
margin: 0;
min-width: 0;
font-weight: 600;
line-height: 1.6;
}
.paragraph-name {
min-width: 0;
overflow-wrap: anywhere;
}
.paragraph-name--unset {
color: var(--el-color-warning);
font-style: italic;
}
.paragraph-actions {
display: flex;
align-items: center;
gap: 8px;
flex: 0 0 auto;
}
.paragraph-count {
font-size: 12px;
color: var(--el-text-color-placeholder);
}
.paragraph-body {
margin: 8px 0 0;
/* Body text is set at a readable size rather than at the heading's: a
template's font size describes its headings, not its prose. */
font-size: 15px;
line-height: 2;
color: var(--el-text-color-primary);
text-align: justify;
/* The two-character indent a Chinese manuscript expects. */
text-indent: 2em;
overflow-wrap: anywhere;
}
.sentence {
white-space: pre-wrap;
}
/* A display formula centres itself; the paragraph's two-character indent and
its justification must not fight it. */
.sentence--display {
display: block;
text-indent: 0;
text-align: center;
margin: 6px 0;
white-space: normal;
}
/* The fallback when KaTeX cannot read a formula: the source, in red, so it can
be found and fixed instead of silently vanishing. */
.paragraph-body :deep(.math-error) {
color: var(--el-color-danger);
font-size: 12px;
}
.paragraph-body :deep(.katex) {
/* Formulas inherit the paragraph's size and colour, and are never justified
apart — a stretched equation is unreadable. */
font-size: 1.05em;
text-align: left;
}
.cite-mark {
margin: 0 1px;
padding: 0 2px;
font-size: 11px;
color: var(--el-color-primary);
cursor: help;
vertical-align: super;
line-height: 0;
}
.cite-tip {
max-width: 380px;
}
.cite-tip-head {
display: flex;
align-items: center;
gap: 8px;
font-weight: 600;
}
/* The bibliography entry: the whole point of the tooltip. It is set as a
paragraph of running text because that is how it prints in the list. */
.cite-tip-entry {
margin-top: 4px;
line-height: 1.7;
}
.cite-tip-unlinked {
margin-top: 4px;
font-weight: 400;
opacity: 0.7;
}
.cite-tip-quote {
margin-top: 4px;
line-height: 1.6;
}
</style>