@uniweb/press 0.4.12 → 0.4.14
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/adapters/epub.js +19 -4
- package/src/adapters/math-css.js +43 -2
- package/src/adapters/pagedjs.js +14 -3
package/package.json
CHANGED
package/src/adapters/epub.js
CHANGED
|
@@ -31,7 +31,7 @@
|
|
|
31
31
|
import JSZip from 'jszip'
|
|
32
32
|
import { parseFragment } from 'parse5'
|
|
33
33
|
import { fetchAssets } from '../assets/fetch.js'
|
|
34
|
-
import { MATH_CSS } from './math-css.js'
|
|
34
|
+
import { MATH_CSS, numberEquations } from './math-css.js'
|
|
35
35
|
|
|
36
36
|
// ============================================================================
|
|
37
37
|
// Public API
|
|
@@ -63,14 +63,30 @@ export async function compileEpub(input, options = {}) {
|
|
|
63
63
|
const { meta = {}, stylesheet, identifier, cover, loadAsset } = options
|
|
64
64
|
|
|
65
65
|
const resolvedMeta = { ...(metadata || {}), ...meta }
|
|
66
|
-
|
|
66
|
+
// Math CSS is emitted ahead of the design stylesheet, not inside it.
|
|
67
|
+
// Temml's MathML needs these rules to lay out at all -- without them a
|
|
68
|
+
// pmatrix's rows touch and an aligned environment's = signs drift -- and a
|
|
69
|
+
// foundation that supplies its own `stylesheet` REPLACES the default, so
|
|
70
|
+
// folding them into DEFAULT_STYLESHEET meant customizing the design
|
|
71
|
+
// silently broke the maths. Emitting first also leaves the foundation's
|
|
72
|
+
// sheet able to override any of it.
|
|
73
|
+
const css = `${MATH_CSS}\n${stylesheet || DEFAULT_STYLESHEET}`
|
|
67
74
|
const id = identifier || resolvedMeta.identifier || generateUuid()
|
|
68
75
|
const coverUrl = cover || resolvedMeta.cover || resolvedMeta.coverImage
|
|
69
76
|
|
|
70
77
|
// Walk each HTML fragment, collect image URLs, capture a chapter title.
|
|
71
78
|
// The parsed tree is kept so we can re-serialize as XHTML after
|
|
72
79
|
// rewriting <img src> to manifest-relative paths.
|
|
73
|
-
|
|
80
|
+
// Numbered straight through the book rather than restarting per chapter,
|
|
81
|
+
// and as text rather than a CSS counter — reader support for counters is
|
|
82
|
+
// not something an EPUB can find out.
|
|
83
|
+
let eqnNo = 1
|
|
84
|
+
const numbered = sections.map((html) => {
|
|
85
|
+
const result = numberEquations(html, eqnNo)
|
|
86
|
+
eqnNo = result.next
|
|
87
|
+
return result.html
|
|
88
|
+
})
|
|
89
|
+
const chapters = numbered.map((html, i) => buildChapter(html, i))
|
|
74
90
|
|
|
75
91
|
// Deduplicate image URLs across all chapters — and the cover, if any —
|
|
76
92
|
// and fetch them in parallel. Failed fetches are logged and the original
|
|
@@ -626,5 +642,4 @@ pre {
|
|
|
626
642
|
overflow: auto;
|
|
627
643
|
white-space: pre-wrap;
|
|
628
644
|
}
|
|
629
|
-
${MATH_CSS}
|
|
630
645
|
`
|
package/src/adapters/math-css.js
CHANGED
|
@@ -43,12 +43,21 @@ export const MATH_CSS = `/* Temml's own class hooks — without these an aligned
|
|
|
43
43
|
MathML-Core default here; Temml's own CSS only adjusts it for jot. */
|
|
44
44
|
math mtd { padding-top: 0.5ex; padding-bottom: 0.5ex; }
|
|
45
45
|
math mtable.tml-jot mtd { padding-top: 0.7ex; padding-bottom: 0.7ex; }
|
|
46
|
-
/* AMS auto-numbering
|
|
46
|
+
/* AMS auto-numbering, for lanes that keep our CSS counter.
|
|
47
|
+
|
|
48
|
+
Scoped to :empty because the document lanes cannot rely on it -- Paged.js
|
|
49
|
+
rewrites counters for its own pagination and strips counter-increment, so
|
|
50
|
+
every equation rendered as "(0)" (measured 2026-07-31; the declaration
|
|
51
|
+
survives intact without the polyfill). press therefore writes the numbers
|
|
52
|
+
into the spans as text, and a span carrying a number is no longer :empty, so
|
|
53
|
+
the two can never both fire.
|
|
54
|
+
|
|
55
|
+
AMS auto-numbering. Which equations number is the AUTHOR's choice, made in
|
|
47
56
|
LaTeX: align and equation number, aligned and the starred forms do not.
|
|
48
57
|
Without these two rules that choice was discarded -- align and align-star
|
|
49
58
|
rendered identically, so an author who asked for numbers silently got none.
|
|
50
59
|
(No backticks in here: this string is a JS template literal.) */
|
|
51
|
-
.tml-eqn::before {
|
|
60
|
+
.tml-eqn:empty::before {
|
|
52
61
|
counter-increment: tmlEqnNo;
|
|
53
62
|
content: "(" counter(tmlEqnNo) ")";
|
|
54
63
|
}
|
|
@@ -74,3 +83,35 @@ math.tml-display:has(.tml-eqn) {
|
|
|
74
83
|
display: block !important;
|
|
75
84
|
width: 100%;
|
|
76
85
|
}`
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Write equation numbers into Temml's tag spans as text.
|
|
89
|
+
*
|
|
90
|
+
* Temml leaves `<span class="tml-eqn"></span>` empty and expects a CSS counter
|
|
91
|
+
* to draw the number. That works in a browser and not in a document: Paged.js
|
|
92
|
+
* rewrites counters for its own pagination and strips `counter-increment`, so
|
|
93
|
+
* every equation renders as "(0)" -- measured, and confirmed by the declaration
|
|
94
|
+
* surviving intact once the polyfill is removed. EPUB readers vary at least as
|
|
95
|
+
* widely, and neither lane offers a way to find out.
|
|
96
|
+
*
|
|
97
|
+
* So the document lanes stop asking CSS to count. A span that carries its
|
|
98
|
+
* number is no longer `:empty`, and the stylesheet's counter rule is scoped to
|
|
99
|
+
* `:empty`, so the two can never both fire and double up.
|
|
100
|
+
*
|
|
101
|
+
* A regex rather than a parse: Temml emits this span in exactly one shape, and
|
|
102
|
+
* the epub adapter would otherwise have to thread a counter through its parse5
|
|
103
|
+
* tree for no gain.
|
|
104
|
+
*
|
|
105
|
+
* @param {string} html
|
|
106
|
+
* @param {number} [start=1] - First number to use; lets a caller continue the
|
|
107
|
+
* sequence across sections so a book numbers straight through.
|
|
108
|
+
* @returns {{ html: string, next: number }}
|
|
109
|
+
*/
|
|
110
|
+
export function numberEquations(html, start = 1) {
|
|
111
|
+
let n = start
|
|
112
|
+
const out = String(html ?? '').replace(
|
|
113
|
+
/<span class="tml-eqn"><\/span>/g,
|
|
114
|
+
() => `<span class="tml-eqn">(${n++})</span>`,
|
|
115
|
+
)
|
|
116
|
+
return { html: out, next: n }
|
|
117
|
+
}
|
package/src/adapters/pagedjs.js
CHANGED
|
@@ -129,9 +129,21 @@ async function compileServerSide(htmlDoc, options = {}) {
|
|
|
129
129
|
* want to inspect the emitted document before handing it to a browser.
|
|
130
130
|
*/
|
|
131
131
|
export function emitDocument({ body = '', meta = {}, stylesheet, polyfillUrl } = {}) {
|
|
132
|
+
// Equation numbers are written in as text rather than left to the CSS
|
|
133
|
+
// counter: Paged.js strips counter-increment while rewriting counters for
|
|
134
|
+
// its own pagination, which rendered every equation as "(0)".
|
|
135
|
+
body = numberEquations(body).html
|
|
136
|
+
|
|
132
137
|
const lang = meta.language ?? 'en'
|
|
133
138
|
const title = meta.title ?? 'Book'
|
|
134
|
-
|
|
139
|
+
// Math CSS is emitted ahead of the design stylesheet, not inside it.
|
|
140
|
+
// Temml's MathML needs these rules to lay out at all -- without them a
|
|
141
|
+
// pmatrix's rows touch and an aligned environment's = signs drift -- and a
|
|
142
|
+
// foundation that supplies its own `stylesheet` REPLACES the default, so
|
|
143
|
+
// folding them into DEFAULT_STYLESHEET meant customizing the design
|
|
144
|
+
// silently broke the maths. Emitting first also leaves the foundation's
|
|
145
|
+
// sheet able to override any of it.
|
|
146
|
+
const css = `${MATH_CSS}\n${stylesheet || DEFAULT_STYLESHEET}`
|
|
135
147
|
const polyfill = polyfillUrl || DEFAULT_POLYFILL_URL
|
|
136
148
|
|
|
137
149
|
return `<!doctype html>
|
|
@@ -231,7 +243,7 @@ export const DEFAULT_POLYFILL_URL =
|
|
|
231
243
|
* book PDF without a custom foundation stylesheet. Foundations override by
|
|
232
244
|
* passing `options.stylesheet`.
|
|
233
245
|
*/
|
|
234
|
-
import { MATH_CSS } from './math-css.js'
|
|
246
|
+
import { MATH_CSS, numberEquations } from './math-css.js'
|
|
235
247
|
|
|
236
248
|
export const DEFAULT_STYLESHEET = `
|
|
237
249
|
/* Hidden metadata block — the DOM carries it for CSS string-set / string() */
|
|
@@ -294,5 +306,4 @@ blockquote {
|
|
|
294
306
|
}
|
|
295
307
|
ul, ol { margin: 0.5em 0 0.75em 1.5em; }
|
|
296
308
|
figure { break-inside: avoid; }
|
|
297
|
-
${MATH_CSS}
|
|
298
309
|
`
|