@pretextbook/pretext-html 0.1.1 → 0.3.0

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.
Files changed (121) hide show
  1. package/README.md +82 -1
  2. package/assets/preview-html.xsl +0 -0
  3. package/assets/upstream.json +0 -0
  4. package/assets/xsl/README.md +0 -0
  5. package/assets/xsl/entities.ent +0 -0
  6. package/assets/xsl/extract-asymptote.xsl +0 -0
  7. package/assets/xsl/extract-biblio-csl.xsl +0 -0
  8. package/assets/xsl/extract-datafile.xsl +0 -0
  9. package/assets/xsl/extract-dynamic.xsl +0 -0
  10. package/assets/xsl/extract-identity.xsl +0 -0
  11. package/assets/xsl/extract-interactive.xsl +0 -0
  12. package/assets/xsl/extract-latex-image.xsl +0 -0
  13. package/assets/xsl/extract-mermaid.xsl +0 -0
  14. package/assets/xsl/extract-mom.xsl +0 -0
  15. package/assets/xsl/extract-pg.xsl +0 -0
  16. package/assets/xsl/extract-prefigure.xsl +0 -0
  17. package/assets/xsl/extract-qrcode.xsl +0 -0
  18. package/assets/xsl/extract-runestone-page-template.xsl +0 -0
  19. package/assets/xsl/extract-sageplot.xsl +0 -0
  20. package/assets/xsl/extract-stack.xsl +0 -0
  21. package/assets/xsl/extract-trace.xsl +0 -0
  22. package/assets/xsl/extract-youtube.xsl +0 -0
  23. package/assets/xsl/html-symbols.xsl +0 -0
  24. package/assets/xsl/latex/pretext-latex-AIM.xsl +0 -0
  25. package/assets/xsl/latex/pretext-latex-CLP.xsl +0 -0
  26. package/assets/xsl/latex/pretext-latex-chaos.xsl +0 -0
  27. package/assets/xsl/latex/pretext-latex-dyslexic-font.xsl +0 -0
  28. package/assets/xsl/latex/pretext-latex-guide.xsl +0 -0
  29. package/assets/xsl/latex/pretext-latex-texstyle.xsl +0 -0
  30. package/assets/xsl/localizations/README.md +0 -0
  31. package/assets/xsl/localizations/af-ZA.xml +0 -0
  32. package/assets/xsl/localizations/bg-BG.xml +0 -0
  33. package/assets/xsl/localizations/ca-ES.xml +0 -0
  34. package/assets/xsl/localizations/cs-CZ.xml +0 -0
  35. package/assets/xsl/localizations/de-DE.xml +0 -0
  36. package/assets/xsl/localizations/en-US.xml +0 -0
  37. package/assets/xsl/localizations/es-ES.xml +0 -0
  38. package/assets/xsl/localizations/fi-FI.xml +0 -0
  39. package/assets/xsl/localizations/fr-CA.xml +0 -0
  40. package/assets/xsl/localizations/fr-FR.xml +0 -0
  41. package/assets/xsl/localizations/hu-HU.xml +0 -0
  42. package/assets/xsl/localizations/it-IT.xml +0 -0
  43. package/assets/xsl/localizations/ku-CKB.xml +0 -0
  44. package/assets/xsl/localizations/localizations.xml +0 -0
  45. package/assets/xsl/localizations/nl-NL.xml +0 -0
  46. package/assets/xsl/localizations/pt-BR.xml +0 -0
  47. package/assets/xsl/localizations/pt-PT.xml +0 -0
  48. package/assets/xsl/localizations/zh-HANS.xml +0 -0
  49. package/assets/xsl/pretext-assembly.xsl +0 -0
  50. package/assets/xsl/pretext-basic-html.xsl +0 -0
  51. package/assets/xsl/pretext-beamer.xsl +0 -0
  52. package/assets/xsl/pretext-braille-preprint.xsl +0 -0
  53. package/assets/xsl/pretext-common.xsl +0 -0
  54. package/assets/xsl/pretext-dynamic.xsl +0 -0
  55. package/assets/xsl/pretext-epub.xsl +0 -0
  56. package/assets/xsl/pretext-fo.xsl +0 -0
  57. package/assets/xsl/pretext-html.xsl +0 -0
  58. package/assets/xsl/pretext-json-manifest.xsl +0 -0
  59. package/assets/xsl/pretext-jupyter.xsl +0 -0
  60. package/assets/xsl/pretext-latex-classic.xsl +0 -0
  61. package/assets/xsl/pretext-latex-common.xsl +0 -0
  62. package/assets/xsl/pretext-latex.xsl +0 -0
  63. package/assets/xsl/pretext-litprog.xsl +0 -0
  64. package/assets/xsl/pretext-merge.xsl +0 -0
  65. package/assets/xsl/pretext-numbers.xsl +0 -0
  66. package/assets/xsl/pretext-revealjs.xsl +0 -0
  67. package/assets/xsl/pretext-runestone-fitb.xsl +0 -0
  68. package/assets/xsl/pretext-runestone-static.xsl +0 -0
  69. package/assets/xsl/pretext-runestone.xsl +0 -0
  70. package/assets/xsl/pretext-sage-doctest.xsl +0 -0
  71. package/assets/xsl/pretext-smc.xsl +0 -0
  72. package/assets/xsl/pretext-solution-manual-latex.xsl +0 -0
  73. package/assets/xsl/pretext-text-utilities.xsl +0 -0
  74. package/assets/xsl/pretext-text.xsl +0 -0
  75. package/assets/xsl/pretext-units.xsl +0 -0
  76. package/assets/xsl/pretext-view-source.xsl +0 -0
  77. package/assets/xsl/pretext-ww-problem-sets.xsl +0 -0
  78. package/assets/xsl/publisher-variables.xsl +0 -0
  79. package/assets/xsl/support/README.md +0 -0
  80. package/assets/xsl/support/extract-math.xsl +0 -0
  81. package/assets/xsl/support/package-math.xsl +0 -0
  82. package/assets/xsl/support/play-button/README.md +0 -0
  83. package/assets/xsl/support/play-button/play-button.png +0 -0
  84. package/assets/xsl/support/play-button/play-button.svg +0 -0
  85. package/assets/xsl/support/pretext-pg-macros.xsl +0 -0
  86. package/assets/xsl/support/webpack_static_imports.xml +0 -0
  87. package/assets/xsl/tests/README.md +0 -0
  88. package/assets/xsl/tests/null.xml +0 -0
  89. package/assets/xsl/tests/pretext-text-utilities-test.xsl +0 -0
  90. package/assets/xsl/utilities/README.md +0 -0
  91. package/assets/xsl/utilities/author-report.xsl +0 -0
  92. package/assets/xsl/utilities/deprecate-autoname.sed +0 -0
  93. package/assets/xsl/utilities/deprecate-index.sed +0 -0
  94. package/assets/xsl/utilities/fix-deprecations.xsl +0 -0
  95. package/assets/xsl/utilities/pretext-enhanced-source.xsl +0 -0
  96. package/assets/xsl/utilities/report-publisher-variables.xsl +0 -0
  97. package/assets/xsl/xml-to-json.xsl +0 -0
  98. package/assets/xsl/xml-to-string.xsl +0 -0
  99. package/assets/xsl-bundle.json +1 -0
  100. package/dist/assets.d.ts +43 -0
  101. package/dist/assets.js +76 -0
  102. package/dist/cli-CzoinNle.js +639 -0
  103. package/dist/cli.d.ts +0 -0
  104. package/dist/cli.js +1 -1
  105. package/dist/host.browser.d.ts +29 -0
  106. package/dist/host.d.ts +41 -0
  107. package/dist/index.browser.d.ts +29 -0
  108. package/dist/index.browser.js +701 -0
  109. package/dist/index.d.ts +8 -0
  110. package/dist/index.js +20 -13
  111. package/dist/internal/posix-path.d.ts +59 -0
  112. package/dist/missing-asset.d.ts +31 -0
  113. package/dist/mounts.d.ts +18 -0
  114. package/dist/publication.d.ts +27 -0
  115. package/dist/renderer.d.ts +33 -1
  116. package/dist/sourcemap.d.ts +0 -0
  117. package/dist/theme.d.ts +0 -0
  118. package/dist/theme.js +0 -0
  119. package/dist/xinclude.d.ts +0 -0
  120. package/package.json +17 -3
  121. package/dist/cli-BQizBK0N.js +0 -555
package/README.md CHANGED
@@ -74,6 +74,58 @@ const { html } = await renderHtml({
74
74
  - `theme: "dark" | "light" | "system"` makes the preview follow the embedding
75
75
  app's light/dark theme (see [Theme control](#theme-control)).
76
76
 
77
+ ### API (browser)
78
+
79
+ The same renderer runs in a browser — no server, no Python. Bundlers pick it up
80
+ through the `browser` export condition automatically; there is nothing to
81
+ configure.
82
+
83
+ ```js
84
+ import { renderHtml, isJspiAvailable } from "@pretextbook/pretext-html";
85
+
86
+ if (!isJspiAvailable()) {
87
+ // No WebAssembly JSPI in this engine — fall back to a server-side build.
88
+ }
89
+
90
+ const { html } = await renderHtml({
91
+ sourcePath: "/source/main.ptx", // a *virtual* path: no filesystem is read
92
+ sourceContent: editorText, // required in the browser
93
+ projectDir: "/source",
94
+ fragment: true, // rendering one division on its own
95
+ });
96
+ ```
97
+
98
+ Differences from the Node API, all of them consequences of there being no
99
+ filesystem:
100
+
101
+ - **`sourceContent` is required.** `sourcePath` still anchors relative
102
+ `xi:include`s and the output's URLs, but nothing is read from it.
103
+ - **`xi:include`s are not resolved from disk.** Pre-merge them, or pass the
104
+ already-merged document as `sourceContent`. An unresolvable include uses its
105
+ `<xi:fallback>` if it has one, and otherwise throws.
106
+ - **`publicationPath` / `docinfoSourcePath` need absolute URLs**, since those
107
+ are the only readable locations. Prefer `docinfo` (a string) over
108
+ `docinfoSourcePath`.
109
+ - **Stylesheets are fetched from jsDelivr**, version-pinned to the installed
110
+ package, so no build configuration is needed. To self-host them, copy this
111
+ package's `assets/` directory somewhere your app serves and call
112
+ `setAssetsBase("/pretext-assets")` before the first render. (Under Node the
113
+ equivalent is `PRETEXT_HTML_ASSETS`, or the same `setAssetsBase`.)
114
+ - **A cold render costs two requests**, not ~32: `assets/xsl-bundle.json` packs
115
+ the whole stylesheet closure (13 stylesheets plus the 17 locale files
116
+ `pretext-common.xsl` eagerly loads) into one file. It is purely an
117
+ optimisation — if it is missing, each file is fetched individually and the
118
+ render still succeeds. It is recorded from a real render by
119
+ `npm run build-xsl-bundle`, which runs as part of `npm run build`.
120
+
121
+ Typical timings, measured on a single-division fragment: **~400ms cold**
122
+ (stylesheet compile plus asset fetch, paid once per session and then cached)
123
+ and **~90ms warm**. That is what makes preview-on-save feel instant.
124
+
125
+ `setMountReader()` is the escape hatch if neither delivery mode fits — it lets
126
+ you serve the stylesheets from an in-memory map, a zip, or a webview resource
127
+ URI.
128
+
77
129
  ### Theme control
78
130
 
79
131
  A rendered page can already switch between light and dark — pretext-core.js
@@ -149,20 +201,49 @@ the message helper.
149
201
  (libxml2's own XInclude pass cannot suspend in the current WASM build).
150
202
  Supports nested includes, `parse="text"`, and `xi:fallback`; `xpointer` is
151
203
  not supported.
204
+ 5. **Platform seam** — `src/host.ts` holds everything that touches the outside
205
+ world: read a file under a mount, read a caller-named file, locate
206
+ `assets/`. Under Node those are filesystem calls; in the browser build
207
+ (`src/host.browser.ts`, swapped in by `vite.config.mts`) they are `fetch`es.
208
+ The browser build also substitutes a posix-only `node:path`
209
+ (`src/internal/posix-path.ts`), which is sound because no path this package
210
+ computes ever reaches a real filesystem — with `FILESYSTEM=0` they are all
211
+ virtual, ending up as mount keys or `.ptx.invalid` URL segments. The result
212
+ has no `node:` imports at all; rollup fails the build if one survives.
152
213
 
153
214
  ## Limitations (current WASM build)
154
215
 
155
216
  | Limitation | Cause | Fix |
156
217
  | ---------------------------------------------- | ----------------------------------------------- | --------------------------------------------------------------------------- |
157
218
  | No multi-file output (`exsl:document`) | Compiled with `FILESYSTEM=0` | Rebuild with `FILESYSTEM=1` (files land in MEMFS) |
158
- | Node only, JSPI flag required | Loader fetches resources mid-transform via JSPI | Preload stylesheets into MEMFS at init, or wait for JSPI to ship unflagged |
219
+ | JSPI required (flagged on Node) | Loader fetches resources mid-transform via JSPI | Feature-detect with `isJspiAvailable()`; engines without it need a fallback |
159
220
  | No generated images (latex-image, sageplot, …) | Produced by the Python toolchain, not XSLT | Out of scope; run `pretext generate` and the preview will pick the files up |
160
221
  | `xi:include` with `xpointer` unsupported | JS resolver stands in for libxml2's | Rebuild adding `xmlXIncludeProcessFlags` to the JSPI export list |
222
+ | Renders run one at a time, never in parallel | Shared libxslt state; the transform suspends mid-run | Inherent; `renderHtml` queues concurrent calls for you |
161
223
 
162
224
  The whole-book stack overflow ("memory access out of bounds") that the stock
163
225
  `libxslt-wasm` build hit on large documents is **fixed** in the
164
226
  `@pretextbook/libxslt-wasm` fork, which links with a larger WASM stack.
165
227
 
228
+ ### Concurrency
229
+
230
+ A render is not reentrant. It drives a single cached compiled stylesheet
231
+ through a patched `globalThis.fetch` and shared mount tables, and it *suspends*
232
+ mid-transform to fetch stylesheets — which is precisely the window in which
233
+ another render would get to run. Overlapping renders interleave inside libxslt
234
+ and corrupt it.
235
+
236
+ `renderHtml` therefore **queues**: concurrent calls are safe, and each resolves
237
+ with its own result, but they execute one at a time rather than in parallel. A
238
+ render that rejects does not stall the queue.
239
+
240
+ This matters mainly if you drive the internals yourself. If you do, serialize
241
+ your own calls — the corruption is not self-describing: it surfaces as an
242
+ out-of-bounds memory fault, and afterwards the WASM instance aborts on *every*
243
+ later call for the lifetime of the process. Once that has happened, only a
244
+ restart (or a page reload) recovers; `renderHtml` detects the state and says so
245
+ rather than repeating the underlying assertion.
246
+
166
247
  Missing generated assets degrade gracefully: the transform serves a stub for
167
248
  missing files, PreTeXt emits its usual `PTX:ERROR` advice, and the preview
168
249
  renders without the image.
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes