@pretextbook/pretext-html 0.16.1 → 0.18.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.
@@ -0,0 +1,79 @@
1
+ import { Element, Root } from 'xast';
2
+ export interface SubtreeOptions {
3
+ /**
4
+ * The `xml:id` of the element to preview. An element that is not itself a
5
+ * division selects the division enclosing it. Takes precedence over
6
+ * `file`/`line`.
7
+ */
8
+ id?: string;
9
+ /**
10
+ * The 1-based source line the author is at, selecting the innermost
11
+ * division containing it. A line outside every division (in the prolog or
12
+ * `<docinfo>`, say) selects the nearest division after it, else before it.
13
+ */
14
+ line?: number;
15
+ /**
16
+ * Which file `line` is in — an absolute path, as given to `sourcePath` and
17
+ * resolved the same way. Defaults to the file being rendered, which is the
18
+ * right answer for a single-file document; a line in an xi:included file
19
+ * needs its own path, since each file's lines start at 1.
20
+ */
21
+ file?: string;
22
+ /**
23
+ * Chunk level, with PreTeXt's meaning: the depth at which divisions become
24
+ * pages of their own (1 for chapters of a book or sections of an article,
25
+ * 2 one deeper, and so on). The preview is the page that would hold the
26
+ * selection in a build chunked at that level:
27
+ *
28
+ * - a selection deeper than `level` previews its ancestor at `level`,
29
+ * whole, since that whole ancestor is one page there;
30
+ * - a selection at `level` previews that division, whole;
31
+ * - a selection shallower than `level` — a chapter's introduction, say —
32
+ * previews that division as a summary page: its own content, then
33
+ * links to the divisions nested in it, since those are pages of their
34
+ * own and only summarized on the shallower page.
35
+ *
36
+ * Omitted, every division is a page of its own: the innermost division
37
+ * around the selection is previewed, as a summary page whenever it
38
+ * contains divisions. Only leaf divisions then render in full, and a
39
+ * cursor at the top of a book shows the book's front matter and a table
40
+ * of its chapters, not the book.
41
+ */
42
+ level?: number;
43
+ /**
44
+ * Render the whole document instead when it has no more than this many
45
+ * elements. Small documents preview whole as fast as any division of them
46
+ * would, and showing all of one is the better preview; this is the knob
47
+ * for "never show the entire document unless it is small". Counted on the
48
+ * xi:include-merged document, which {@link measureDocument} reports.
49
+ */
50
+ wholeDocumentUpTo?: number;
51
+ }
52
+ /** The division {@link selectDivision} chose. */
53
+ export interface SelectedDivision {
54
+ element: Element;
55
+ /** Its `xml:id` — authored, or injected (see {@link ensureXmlId}). */
56
+ id: string;
57
+ /** Whether its child divisions should be collapsed (see `level`). */
58
+ collapse: boolean;
59
+ }
60
+ /**
61
+ * Pick the division to preview (see the module comment). Returns undefined
62
+ * when the document has no division to pick — an `id` that names nothing,
63
+ * a file with no divisions — which the caller should treat as "render the
64
+ * whole document".
65
+ */
66
+ export declare function selectDivision(tree: Root, options: SubtreeOptions,
67
+ /** The file the tree's root was parsed from; what `file` defaults to. */
68
+ defaultFile: string): SelectedDivision | undefined;
69
+ /**
70
+ * The element's `xml:id`, injecting one when it has none — the very id
71
+ * pretext-assembly.xsl would stamp on it anyway (`parent-id + "-" +
72
+ * position`), so the rendered page's ids, and with them the source map and
73
+ * in-place patching, are the same as without the injection. Needed because
74
+ * both upstream's `subtree` parameter and the skeleton address a division
75
+ * by `xml:id`, and most divisions in a single-file document carry none.
76
+ */
77
+ export declare function ensureXmlId(tree: Root, element: Element): string;
78
+ /** Number of elements in the tree — what the assembly passes' cost tracks. */
79
+ export declare function countElements(tree: Root | Element): number;
package/dist/target.d.ts CHANGED
@@ -2,7 +2,7 @@
2
2
  * Which PreTeXt conversion a render drives.
3
3
  *
4
4
  * The two share almost everything — the same assembly passes, the same
5
- * publication file, the same portable/CDN resource handling — and differ in
5
+ * publication file, the same CDN resource handling — and differ in
6
6
  * the stylesheet they enter through:
7
7
  *
8
8
  * "html" - assets/preview-html.xsl, wrapping pretext-html.xsl.
@@ -1,10 +1,34 @@
1
1
  import { Element, Root } from 'xast';
2
+ import { SourceCache } from './source-cache.js';
3
+ /**
4
+ * How a file the document refers to is read: by absolute, normalized path
5
+ * (as `path.resolve` spells it on the platform — posix in the browser build),
6
+ * returning its text, or `undefined` for a file that does not exist.
7
+ *
8
+ * Lets a host that holds its project in memory — a browser editor, an
9
+ * extension with unsaved buffers — supply files without a filesystem.
10
+ * Returning `undefined` falls through to the host platform's own read (the
11
+ * disk under Node, an absolute URL in the browser), so a reader can also just
12
+ * overlay a few files on top of what is there.
13
+ */
14
+ export type SourceReader = (file: string) => Promise<string | undefined> | string | undefined;
15
+ /** Options shared by everything that resolves includes. */
16
+ export interface ResolveOptions {
17
+ /** See {@link SourceReader}. */
18
+ readSource?: SourceReader;
19
+ /** Reuse parses of unchanged files across calls (see source-cache.ts). */
20
+ cache?: SourceCache;
21
+ }
22
+ /**
23
+ * Read a file through the caller's reader first, then the platform's.
24
+ */
25
+ export declare function readWith(options: ResolveOptions | undefined, file: string): Promise<string | undefined>;
2
26
  /**
3
27
  * Return `sourceContent` with every xi:include replaced by the contents of
4
28
  * the referenced files. `sourcePath` anchors relative hrefs; `projectDir`
5
29
  * bounds which files may be included.
6
30
  */
7
- export declare function resolveXIncludes(sourceContent: string, sourcePath: string, projectDir: string): Promise<string>;
31
+ export declare function resolveXIncludes(sourceContent: string, sourcePath: string, projectDir: string, options?: ResolveOptions): Promise<string>;
8
32
  /** Result of resolveXIncludesToTree. */
9
33
  export interface ResolvedTree {
10
34
  /**
@@ -16,18 +40,31 @@ export interface ResolvedTree {
16
40
  tree: Root;
17
41
  /** The merged document serialized, as resolveXIncludes would return it. */
18
42
  content: string;
43
+ /**
44
+ * Every file the document spans — the source itself first, then each
45
+ * xi:included file in the order first reached. What a host should watch
46
+ * for changes that affect this document.
47
+ */
48
+ files: string[];
49
+ /**
50
+ * Total length of those files' contents: the size of the merged document,
51
+ * near enough, without serializing it.
52
+ */
53
+ bytes: number;
19
54
  }
20
55
  /**
21
56
  * Like resolveXIncludes, but also return the merged tree with per-file
22
57
  * position attribution, for source-map computation.
23
58
  */
24
- export declare function resolveXIncludesToTree(sourceContent: string, sourcePath: string, projectDir: string): Promise<ResolvedTree>;
59
+ export declare function resolveXIncludesToTree(sourceContent: string, sourcePath: string, projectDir: string, options?: ResolveOptions): Promise<ResolvedTree>;
25
60
  /**
26
61
  * File a spliced-in element was parsed from, tagged by expandInclude.
27
62
  * Undefined for nodes of the file being resolved (callers track that file
28
63
  * themselves). Stored on the unist `data` field, which serializers ignore.
29
64
  */
30
65
  export declare function includedFileOf(element: Element): string | undefined;
66
+ /** Record that `element` was parsed from `file` (see includedFileOf). */
67
+ export declare function tagIncludedFile(element: Element, file: string): void;
31
68
  /**
32
69
  * Extract the `<docinfo>` element from a complete PreTeXt source file as an XML
33
70
  * string, resolving xi:includes along the way. Handles the two shapes authors
@@ -42,4 +79,4 @@ export declare function includedFileOf(element: Element): string | undefined;
42
79
  * macros beats a failed render). `mainPath` anchors relative hrefs;
43
80
  * `projectDir` bounds which files may be read.
44
81
  */
45
- export declare function extractDocinfo(mainContent: string, mainPath: string, projectDir: string): Promise<string | undefined>;
82
+ export declare function extractDocinfo(mainContent: string, mainPath: string, projectDir: string, options?: ResolveOptions): Promise<string | undefined>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pretextbook/pretext-html",
3
- "version": "0.16.1",
3
+ "version": "0.18.0",
4
4
  "description": "Convert PreTeXt documents to HTML in pure JavaScript using the official PreTeXt XSLT stylesheets via libxslt-wasm. No Python or PreTeXt installation required.",
5
5
  "author": "Oscar Levin",
6
6
  "license": "MIT",