@typfolio/dev-tools 0.0.0-stage → 0.2.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.
- package/LICENSE +202 -0
- package/README.md +54 -2
- package/dist/index.d.ts +64 -0
- package/dist/index.js +160 -0
- package/dist/pdf.d.ts +56 -0
- package/dist/pdf.js +208 -0
- package/dist/sample-source.generated.d.ts +2 -0
- package/dist/sample-source.generated.js +6 -0
- package/package.json +38 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
package/README.md
CHANGED
|
@@ -1,3 +1,55 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @typfolio/dev-tools
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Dev-time tools for Typfolio templates. Build-time template checks and a PDF structure inspector.
|
|
4
|
+
|
|
5
|
+
Install it as a devDependency. Nothing here is needed at runtime: `@typfolio/core` never imports this package, so none of it reaches a production bundle.
|
|
6
|
+
|
|
7
|
+
```ts
|
|
8
|
+
import { createTemplateChecker } from '@typfolio/dev-tools';
|
|
9
|
+
|
|
10
|
+
const checker = await createTemplateChecker({ wasm, fonts }); // the options of createCompiler
|
|
11
|
+
try {
|
|
12
|
+
const report = await checker.check(template, { modes: ['minimal', 'full'], pdf: { standards: ['ua-1'] } });
|
|
13
|
+
// report.ok: true only if every mode compiled
|
|
14
|
+
// report.results: { mode, ok, diagnostics, error?, bytes? } per mode
|
|
15
|
+
} finally {
|
|
16
|
+
await checker.dispose();
|
|
17
|
+
}
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
`check` also takes `assets`, the files a template reads besides its input (shared settings, a logo), exactly as a request's `assets`: a template that reads `json("/export-settings.json")` can be checked with that file in place.
|
|
21
|
+
|
|
22
|
+
`checkTemplate(template, { wasm, fonts, modes?, pdf?, assets? })` is the one-shot form that creates and disposes a checker.
|
|
23
|
+
|
|
24
|
+
`check` also takes a template made with `defineTemplate`: it compiles only the files the template declares, with each input's JSON Schema at its path, so a file the template imports without declaring it fails, with what to do.
|
|
25
|
+
|
|
26
|
+
`inputSchemaFiles(definitions, { toJSONSchema? })` returns the JSON Schema file of every input of these templates, partials or `defineInput(...)` results, by path, as JSON text. It writes nothing: a script writes the files where the editor reads them, and a test compares them, so a schema that changed without its file fails CI. Schemas that implement Standard JSON Schema (`~standard.jsonSchema`, as Zod's do) need no option; for Zod Mini, pass `toJSONSchema: schema => z.toJSONSchema(schema, { io: 'input' })`.
|
|
27
|
+
|
|
28
|
+
```ts
|
|
29
|
+
import { inputSchemaFiles } from '@typfolio/dev-tools';
|
|
30
|
+
|
|
31
|
+
for (const [path, text] of Object.entries(inputSchemaFiles([headerInput, outcomesInput]))) {
|
|
32
|
+
assert.deepEqual(JSON.parse(await readFile(new URL(path, templatesRoot), 'utf8')), JSON.parse(text), `${path} is out of date`);
|
|
33
|
+
}
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
A check compiles the template once per sample mode (`minimal`, then `full`) the way an editor does, so `input()` builds a sample from the JSON Schema in `template.files`. A compile error is reported in that mode's result; a malformed template or option is thrown. Nothing is written to disk. See "Build-time checks" in [docs/template-authoring.md](../../docs/template-authoring.md) for the rules and a test recipe.
|
|
37
|
+
|
|
38
|
+
## `inspectPdf`
|
|
39
|
+
|
|
40
|
+
```ts
|
|
41
|
+
import { inspectPdf } from '@typfolio/dev-tools/pdf';
|
|
42
|
+
|
|
43
|
+
const report = await inspectPdf(bytes); // the bytes of a compiled PDF
|
|
44
|
+
// report.pages, tagged, structureTree, title, language, creationDate
|
|
45
|
+
// report.standard: { pdfUa?, pdfA?: { part, conformance? } } what the XMP metadata declares
|
|
46
|
+
// report.headings: [{ level, text }] in document order
|
|
47
|
+
// report.figures: [{ alt? }], report.tables, report.fonts: [{ name, embedded }]
|
|
48
|
+
// report.structureRoles: { H1: 1, Table: 1, ... } how often each structure role occurs
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
`inspectPdf` reports facts about the file so a test can assert on them. It never says a file conforms to anything: a tagged PDF with PDF/UA metadata, a title, a language and alt text can still fail PDF/UA. Use a validator such as veraPDF for conformance.
|
|
52
|
+
|
|
53
|
+
It reads the file with [`pdfjs-dist`](https://www.npmjs.com/package/pdfjs-dist), an **optional peer dependency** (`^6.3.0`) that you install yourself when you use this entry. It is loaded on the first call and only from `@typfolio/dev-tools/pdf`: the main entry never imports it. Without it, the call rejects with a `TypstPdfError` (`INITIALIZATION`) that says to install `pdfjs-dist`; bytes that are not a readable PDF reject with `INVALID_REQUEST`. Counts are per page (an element that continues over a page break is counted for each page), see "Inspecting the output" in [docs/template-authoring.md](../../docs/template-authoring.md) for this and the other limits.
|
|
54
|
+
|
|
55
|
+
The package builds on `@typfolio/core/internal`, which is unstable and meant only for Typfolio's own packages.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { TypstPdfError, type CompilerOptions, type Diagnostic, type PartialDefinition, type PdfOptions, type PdfTemplate, type StandardSchemaV1, type TemplateDefinition, type TemplateInput } from '@typfolio/core';
|
|
2
|
+
/** Which sample `@local/typfolio`'s `input()` builds: every property, or only what the schema requires. */
|
|
3
|
+
export type SampleMode = 'minimal' | 'full';
|
|
4
|
+
export interface JSONSchemaOptions {
|
|
5
|
+
/**
|
|
6
|
+
* Turns a schema into JSON Schema. Needed only for a schema that does not implement Standard JSON
|
|
7
|
+
* Schema (`~standard.jsonSchema`), such as Zod Mini's: `toJSONSchema: schema => z.toJSONSchema(schema, { io: 'input' })`.
|
|
8
|
+
*/
|
|
9
|
+
toJSONSchema?(schema: StandardSchemaV1): unknown;
|
|
10
|
+
}
|
|
11
|
+
type Definition = TemplateDefinition<unknown, unknown> | PartialDefinition | TemplateInput;
|
|
12
|
+
/**
|
|
13
|
+
* The JSON Schema file of each input of these templates, partials (nested ones included) or
|
|
14
|
+
* `defineInput(...)` results, by its path, such as `common/header.schema.json`, as JSON text. Write
|
|
15
|
+
* each next to the Typst files, where its `input(json.with("./header.schema.json"))` reads it in an
|
|
16
|
+
* editor for a sample, and in a test compare the parsed files with these to catch a schema that
|
|
17
|
+
* changed without its file. An input without a schema is left out. Nothing is written here.
|
|
18
|
+
*/
|
|
19
|
+
export declare function inputSchemaFiles(definitions: Definition | readonly Definition[], options?: JSONSchemaOptions): Record<string, string>;
|
|
20
|
+
export interface TemplateCheckOptions extends JSONSchemaOptions {
|
|
21
|
+
/** Defaults to `['minimal', 'full']`. Each mode is a separate compile. */
|
|
22
|
+
modes?: readonly SampleMode[];
|
|
23
|
+
/** Same options as a request's `pdf`. */
|
|
24
|
+
pdf?: PdfOptions;
|
|
25
|
+
/**
|
|
26
|
+
* Files the template reads besides its input, such as shared settings or a logo: same as a
|
|
27
|
+
* request's `assets`. For a template made with `defineTemplate`, a schema file given here is used
|
|
28
|
+
* as it is instead of one made from the input's schema.
|
|
29
|
+
*/
|
|
30
|
+
assets?: Readonly<Record<string, Uint8Array>>;
|
|
31
|
+
}
|
|
32
|
+
export interface TemplateCheckResult {
|
|
33
|
+
mode: SampleMode;
|
|
34
|
+
ok: boolean;
|
|
35
|
+
/** Warnings when `ok`; the compile errors otherwise. */
|
|
36
|
+
diagnostics: readonly Diagnostic[];
|
|
37
|
+
/** The `COMPILATION` error of a failed mode. */
|
|
38
|
+
error?: TypstPdfError;
|
|
39
|
+
/** The PDF of a successful mode. Held in memory only; nothing is written anywhere. */
|
|
40
|
+
bytes?: Uint8Array;
|
|
41
|
+
}
|
|
42
|
+
export interface TemplateCheckReport {
|
|
43
|
+
/** True only if every requested mode compiled. */
|
|
44
|
+
ok: boolean;
|
|
45
|
+
results: TemplateCheckResult[];
|
|
46
|
+
}
|
|
47
|
+
export interface TemplateChecker {
|
|
48
|
+
/**
|
|
49
|
+
* Compiles `template` once per mode in editor mode: no runtime marker, so `input()` reads the schema
|
|
50
|
+
* from `template.files` (at the path its `json.with(...)` names, relative to the file that writes it)
|
|
51
|
+
* and builds the sample. A template made with `defineTemplate` is compiled from the files it
|
|
52
|
+
* declares only, with each input's JSON Schema at its path (see `inputSchemaFiles`): a file it
|
|
53
|
+
* imports without declaring it fails, with what to do. A failed compile is reported in its result;
|
|
54
|
+
* a malformed template or option rejects with `INVALID_REQUEST` or `UNSUPPORTED_OPTION`.
|
|
55
|
+
*/
|
|
56
|
+
check(template: PdfTemplate | TemplateDefinition<unknown, unknown>, options?: TemplateCheckOptions): Promise<TemplateCheckReport>;
|
|
57
|
+
/** Stops accepting checks; waits for an accepted one and frees the compiler. */
|
|
58
|
+
dispose(): Promise<void>;
|
|
59
|
+
}
|
|
60
|
+
/** Same options as `createCompiler`. One active check per checker. */
|
|
61
|
+
export declare function createTemplateChecker(options: CompilerOptions): Promise<TemplateChecker>;
|
|
62
|
+
/** One-shot form of `createTemplateChecker`: creates a checker, runs one check, disposes it. */
|
|
63
|
+
export declare function checkTemplate(template: PdfTemplate | TemplateDefinition<unknown, unknown>, options: CompilerOptions & TemplateCheckOptions): Promise<TemplateCheckReport>;
|
|
64
|
+
export {};
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
// Dev-time tools. Nothing here is needed at runtime: applications install this package as a devDependency
|
|
2
|
+
// and core never imports it, so none of it (nor the generated sample source it embeds) reaches a production bundle.
|
|
3
|
+
import { TypstPdfError, } from '@typfolio/core';
|
|
4
|
+
import { bundledPackageDirectory, createEngine, explainCompileError, initialize, isTemplateDefinition, packageFileMounts, prepareRequest, resolveFonts, } from '@typfolio/core/internal';
|
|
5
|
+
import { sampleFile, sampleSource } from './sample-source.generated.js';
|
|
6
|
+
const sampleModes = ['minimal', 'full'];
|
|
7
|
+
// Each input of these templates, partials and inputs, nested partials included, once per path.
|
|
8
|
+
function inputsOf(definitions) {
|
|
9
|
+
const found = new Map();
|
|
10
|
+
const add = (path, schema) => {
|
|
11
|
+
if (found.has(path) && found.get(path) !== schema) {
|
|
12
|
+
throw new TypstPdfError('INVALID_REQUEST', `Two different schemas read their input from ${path}`);
|
|
13
|
+
}
|
|
14
|
+
found.set(path, schema);
|
|
15
|
+
};
|
|
16
|
+
const visit = (definition) => {
|
|
17
|
+
if (isTemplateDefinition(definition)) {
|
|
18
|
+
for (const slot of definition.inputs)
|
|
19
|
+
add(slot.path, slot.schema);
|
|
20
|
+
}
|
|
21
|
+
else if (definition?.kind === 'typfolio-partial') {
|
|
22
|
+
const partial = definition;
|
|
23
|
+
visit(partial.input);
|
|
24
|
+
for (const nested of Object.values(partial.partials))
|
|
25
|
+
visit(nested);
|
|
26
|
+
}
|
|
27
|
+
else if (typeof definition?.path === 'string') {
|
|
28
|
+
const input = definition;
|
|
29
|
+
add(input.path.replace(/^\/+/, ''), input.schema);
|
|
30
|
+
}
|
|
31
|
+
else {
|
|
32
|
+
throw new TypstPdfError('INVALID_REQUEST', 'Expected a defineTemplate(...), a definePartial(...) or a defineInput(...)');
|
|
33
|
+
}
|
|
34
|
+
};
|
|
35
|
+
for (const definition of definitions)
|
|
36
|
+
visit(definition);
|
|
37
|
+
return found;
|
|
38
|
+
}
|
|
39
|
+
function jsonSchemaOf(schema, path, options) {
|
|
40
|
+
if (options.toJSONSchema)
|
|
41
|
+
return options.toJSONSchema(schema);
|
|
42
|
+
const converter = schema['~standard'].jsonSchema;
|
|
43
|
+
if (typeof converter?.input === 'function')
|
|
44
|
+
return converter.input({ target: 'draft-2020-12' });
|
|
45
|
+
throw new TypstPdfError('INVALID_REQUEST', `The schema of ${path} does not implement Standard JSON Schema (~standard.jsonSchema): pass toJSONSchema, for Zod Mini \`schema => z.toJSONSchema(schema, { io: 'input' })\``);
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* The JSON Schema file of each input of these templates, partials (nested ones included) or
|
|
49
|
+
* `defineInput(...)` results, by its path, such as `common/header.schema.json`, as JSON text. Write
|
|
50
|
+
* each next to the Typst files, where its `input(json.with("./header.schema.json"))` reads it in an
|
|
51
|
+
* editor for a sample, and in a test compare the parsed files with these to catch a schema that
|
|
52
|
+
* changed without its file. An input without a schema is left out. Nothing is written here.
|
|
53
|
+
*/
|
|
54
|
+
export function inputSchemaFiles(definitions, options = {}) {
|
|
55
|
+
const files = {};
|
|
56
|
+
for (const [path, schema] of inputsOf(Array.isArray(definitions) ? definitions : [definitions])) {
|
|
57
|
+
if (schema !== undefined)
|
|
58
|
+
files[path] = `${JSON.stringify(jsonSchemaOf(schema, path, options), null, 2)}\n`;
|
|
59
|
+
}
|
|
60
|
+
return files;
|
|
61
|
+
}
|
|
62
|
+
function parseOptions(options) {
|
|
63
|
+
if (options === undefined)
|
|
64
|
+
return { modes: sampleModes };
|
|
65
|
+
if (!options || typeof options !== 'object' || Array.isArray(options))
|
|
66
|
+
throw new TypstPdfError('INVALID_REQUEST', 'check options must be a plain object');
|
|
67
|
+
const { modes = sampleModes, pdf, assets, toJSONSchema, ...rest } = options;
|
|
68
|
+
for (const key of Object.keys(rest))
|
|
69
|
+
throw new TypstPdfError('UNSUPPORTED_OPTION', `Unsupported check option ${key}`);
|
|
70
|
+
if (!Array.isArray(modes) || !modes.length)
|
|
71
|
+
throw new TypstPdfError('INVALID_REQUEST', 'modes must be a non-empty array');
|
|
72
|
+
for (const mode of modes)
|
|
73
|
+
if (!sampleModes.includes(mode))
|
|
74
|
+
throw new TypstPdfError('UNSUPPORTED_OPTION', `Unsupported sample mode ${String(mode)}`);
|
|
75
|
+
if (new Set(modes).size !== modes.length)
|
|
76
|
+
throw new TypstPdfError('INVALID_REQUEST', 'modes must not repeat');
|
|
77
|
+
if (toJSONSchema !== undefined && typeof toJSONSchema !== 'function')
|
|
78
|
+
throw new TypstPdfError('INVALID_REQUEST', 'toJSONSchema must be a function');
|
|
79
|
+
return { modes, pdf, assets, ...(toJSONSchema === undefined ? {} : { toJSONSchema }) };
|
|
80
|
+
}
|
|
81
|
+
// A defined template as the editor sees it: its declared files, and each input's JSON Schema at its path.
|
|
82
|
+
function editorRequest(template, options) {
|
|
83
|
+
const { pdf, assets } = options;
|
|
84
|
+
if (!isTemplateDefinition(template))
|
|
85
|
+
return { template, input: null, pdf, ...(assets === undefined ? {} : { assets }) };
|
|
86
|
+
const encoder = new TextEncoder();
|
|
87
|
+
const schemas = {};
|
|
88
|
+
for (const [path, schema] of inputsOf([template])) {
|
|
89
|
+
if (assets?.[path] !== undefined)
|
|
90
|
+
continue;
|
|
91
|
+
if (schema === undefined) {
|
|
92
|
+
throw new TypstPdfError('INVALID_REQUEST', `The input at ${path} has no schema: give its JSON Schema file in assets`);
|
|
93
|
+
}
|
|
94
|
+
schemas[path] = encoder.encode(JSON.stringify(jsonSchemaOf(schema, path, options)));
|
|
95
|
+
}
|
|
96
|
+
return { template: { entrypoint: template.entrypoint, files: template.files }, input: null, pdf, assets: { ...assets, ...schemas } };
|
|
97
|
+
}
|
|
98
|
+
/** Same options as `createCompiler`. One active check per checker. */
|
|
99
|
+
export async function createTemplateChecker(options) {
|
|
100
|
+
const fonts = await resolveFonts(options?.fonts);
|
|
101
|
+
await initialize(options.wasm);
|
|
102
|
+
const engine = await createEngine(fonts);
|
|
103
|
+
const encoder = new TextEncoder();
|
|
104
|
+
// The package as the editor sees it: typst.toml, lib.typ and sample.typ, but no input at the package root.
|
|
105
|
+
const mounts = [...packageFileMounts(), [`${bundledPackageDirectory}/${sampleFile}`, encoder.encode(sampleSource)]];
|
|
106
|
+
let closed = false;
|
|
107
|
+
let busy = false;
|
|
108
|
+
let finished = Promise.resolve();
|
|
109
|
+
let disposal;
|
|
110
|
+
return {
|
|
111
|
+
async check(template, checkOptions) {
|
|
112
|
+
if (closed)
|
|
113
|
+
throw new TypstPdfError('DISPOSED', 'The template checker is disposed');
|
|
114
|
+
if (busy)
|
|
115
|
+
throw new TypstPdfError('BUSY', 'The template checker already has an active check');
|
|
116
|
+
const { modes, ...rest } = parseOptions(checkOptions);
|
|
117
|
+
// `input: null` only fills the reserved /input.json; the editor branch never reads it.
|
|
118
|
+
const prepared = prepareRequest(editorRequest(template, rest));
|
|
119
|
+
busy = true;
|
|
120
|
+
let release;
|
|
121
|
+
finished = new Promise(resolve => { release = resolve; });
|
|
122
|
+
try {
|
|
123
|
+
const results = [];
|
|
124
|
+
for (const mode of modes) {
|
|
125
|
+
await Promise.resolve();
|
|
126
|
+
try {
|
|
127
|
+
const compiled = engine.compile(prepared, { mounts, inputs: [['typfolio-sample', mode]] });
|
|
128
|
+
results.push({ mode, ok: true, diagnostics: compiled.diagnostics, bytes: compiled.bytes });
|
|
129
|
+
}
|
|
130
|
+
catch (error) {
|
|
131
|
+
if (!(error instanceof TypstPdfError) || error.code !== 'COMPILATION')
|
|
132
|
+
throw error;
|
|
133
|
+
const explained = explainCompileError(error, prepared.files);
|
|
134
|
+
results.push({ mode, ok: false, diagnostics: explained.diagnostics, error: explained });
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
return { ok: results.every(result => result.ok), results };
|
|
138
|
+
}
|
|
139
|
+
finally {
|
|
140
|
+
busy = false;
|
|
141
|
+
release();
|
|
142
|
+
}
|
|
143
|
+
},
|
|
144
|
+
dispose() {
|
|
145
|
+
closed = true;
|
|
146
|
+
return disposal ??= finished.then(() => engine.dispose());
|
|
147
|
+
},
|
|
148
|
+
};
|
|
149
|
+
}
|
|
150
|
+
/** One-shot form of `createTemplateChecker`: creates a checker, runs one check, disposes it. */
|
|
151
|
+
export async function checkTemplate(template, options) {
|
|
152
|
+
const { wasm, fonts, ...checkOptions } = options ?? {};
|
|
153
|
+
const checker = await createTemplateChecker({ wasm, fonts });
|
|
154
|
+
try {
|
|
155
|
+
return await checker.check(template, checkOptions);
|
|
156
|
+
}
|
|
157
|
+
finally {
|
|
158
|
+
await checker.dispose();
|
|
159
|
+
}
|
|
160
|
+
}
|
package/dist/pdf.d.ts
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
export interface PdfHeading {
|
|
2
|
+
/** 1 to 6 for `H1`..`H6`; 0 for a plain `H`. */
|
|
3
|
+
level: number;
|
|
4
|
+
/** Empty when the text could not be recovered from the page content. */
|
|
5
|
+
text: string;
|
|
6
|
+
}
|
|
7
|
+
export interface PdfFigure {
|
|
8
|
+
/** The figure element's `/Alt`; undefined when the element has none. */
|
|
9
|
+
alt?: string;
|
|
10
|
+
}
|
|
11
|
+
export interface PdfFont {
|
|
12
|
+
/** The font's `/BaseFont` name, including the subset prefix of an embedded subset. */
|
|
13
|
+
name: string;
|
|
14
|
+
/** Whether the file carries the font program, as pdf.js sees it. */
|
|
15
|
+
embedded: boolean;
|
|
16
|
+
}
|
|
17
|
+
export interface PdfInspection {
|
|
18
|
+
pages: number;
|
|
19
|
+
/** The catalog's `/MarkInfo /Marked` is true. */
|
|
20
|
+
tagged: boolean;
|
|
21
|
+
/** The catalog has a `/StructTreeRoot`. */
|
|
22
|
+
structureTree: boolean;
|
|
23
|
+
/** The document information title, else the XMP `dc:title`. */
|
|
24
|
+
title?: string;
|
|
25
|
+
/** The catalog's `/Lang`. */
|
|
26
|
+
language?: string;
|
|
27
|
+
creationDate?: Date;
|
|
28
|
+
/** What the XMP metadata declares (`pdfuaid:part`, `pdfaid:part`, `pdfaid:conformance`). A declaration, not a verdict. */
|
|
29
|
+
standard: {
|
|
30
|
+
pdfUa?: number;
|
|
31
|
+
/** `conformance` is absent when the XMP has no `pdfaid:conformance`. */
|
|
32
|
+
pdfA?: {
|
|
33
|
+
part: number;
|
|
34
|
+
conformance?: string;
|
|
35
|
+
};
|
|
36
|
+
};
|
|
37
|
+
/** In document order, from the structure tree. */
|
|
38
|
+
headings: PdfHeading[];
|
|
39
|
+
figures: PdfFigure[];
|
|
40
|
+
/** Number of `Table` structure elements. */
|
|
41
|
+
tables: number;
|
|
42
|
+
/** Fonts that page content selects, in order of first use. */
|
|
43
|
+
fonts: PdfFont[];
|
|
44
|
+
/** How often each structure role occurs, whatever it is. */
|
|
45
|
+
structureRoles: Record<string, number>;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Reads a compiled PDF and reports facts that matter for accessibility and output policy: tagging, title,
|
|
49
|
+
* language, the standards its XMP metadata declares, headings, figure alternative text, tables and fonts.
|
|
50
|
+
* It reports facts only. A tagged PDF with PDF/UA metadata and all the right-looking structure is not thereby
|
|
51
|
+
* conformant; use a validator such as veraPDF for that.
|
|
52
|
+
*
|
|
53
|
+
* Needs `pdfjs-dist` (an optional peer dependency); rejects with a `TypstPdfError` that says so if it is missing.
|
|
54
|
+
* Input that is not a readable PDF rejects with `INVALID_REQUEST`.
|
|
55
|
+
*/
|
|
56
|
+
export declare function inspectPdf(bytes: Uint8Array): Promise<PdfInspection>;
|
package/dist/pdf.js
ADDED
|
@@ -0,0 +1,208 @@
|
|
|
1
|
+
// PDF structure inspector. It reports facts about a compiled PDF so a test can assert on them; it never says
|
|
2
|
+
// a file conforms to anything. `pdfjs-dist` is an optional peer dependency, loaded on the first call and only
|
|
3
|
+
// from this entry, so the main `@typfolio/dev-tools` entry (and every bundle that uses only that) never sees it.
|
|
4
|
+
import { TypstPdfError } from '@typfolio/core';
|
|
5
|
+
let loader;
|
|
6
|
+
/** Shared by concurrent callers, so it carries no call identity. */
|
|
7
|
+
class PdfJsImportError extends Error {
|
|
8
|
+
}
|
|
9
|
+
async function loadPdfJs() {
|
|
10
|
+
let imported;
|
|
11
|
+
try {
|
|
12
|
+
// The Node-capable build: in Node pdf.js runs its worker code in this thread and needs no `workerSrc`.
|
|
13
|
+
imported = await import('pdfjs-dist/legacy/build/pdf.mjs');
|
|
14
|
+
}
|
|
15
|
+
catch (cause) {
|
|
16
|
+
throw new PdfJsImportError('pdfjs-dist could not be imported', { cause });
|
|
17
|
+
}
|
|
18
|
+
const lib = imported;
|
|
19
|
+
if (typeof lib?.getDocument !== 'function' || typeof lib.OPS?.setFont !== 'number' || typeof lib.PDFDateString?.toDateObject !== 'function') {
|
|
20
|
+
throw new TypeError('pdfjs-dist did not provide the API this version of @typfolio/dev-tools reads (getDocument, OPS, PDFDateString)');
|
|
21
|
+
}
|
|
22
|
+
return lib;
|
|
23
|
+
}
|
|
24
|
+
function getPdfJs() {
|
|
25
|
+
loader ??= loadPdfJs().catch(cause => {
|
|
26
|
+
loader = undefined;
|
|
27
|
+
throw cause;
|
|
28
|
+
});
|
|
29
|
+
return loader;
|
|
30
|
+
}
|
|
31
|
+
async function requirePdfJs() {
|
|
32
|
+
try {
|
|
33
|
+
return await getPdfJs();
|
|
34
|
+
}
|
|
35
|
+
catch (error) {
|
|
36
|
+
if (!(error instanceof PdfJsImportError))
|
|
37
|
+
throw error;
|
|
38
|
+
throw new TypstPdfError('INITIALIZATION', 'inspectPdf could not load pdfjs-dist. Install pdfjs-dist (an optional peer dependency of @typfolio/dev-tools) to use @typfolio/dev-tools/pdf.', [], { cause: error.cause });
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
const text = (value) => typeof value === 'string' && value ? value : undefined;
|
|
42
|
+
function integer(value) {
|
|
43
|
+
const parsed = typeof value === 'string' && /^\s*\d+\s*$/.test(value) ? Number(value) : undefined;
|
|
44
|
+
return parsed !== undefined && Number.isSafeInteger(parsed) ? parsed : undefined;
|
|
45
|
+
}
|
|
46
|
+
function metadataValue(metadata, name) {
|
|
47
|
+
return metadata?.get(name) ?? undefined;
|
|
48
|
+
}
|
|
49
|
+
function creationDate(pdfjs, info, metadata) {
|
|
50
|
+
const fromInfo = text(info?.CreationDate);
|
|
51
|
+
const parsed = fromInfo === undefined ? null : pdfjs.PDFDateString.toDateObject(fromInfo);
|
|
52
|
+
if (parsed && !Number.isNaN(parsed.getTime()))
|
|
53
|
+
return parsed;
|
|
54
|
+
const fromXmp = text(metadataValue(metadata, 'xmp:createdate'));
|
|
55
|
+
const date = fromXmp === undefined ? undefined : new Date(fromXmp);
|
|
56
|
+
return date && !Number.isNaN(date.getTime()) ? date : undefined;
|
|
57
|
+
}
|
|
58
|
+
const headingLevel = (role) => {
|
|
59
|
+
const match = /^H([1-6])?$/.exec(role);
|
|
60
|
+
return match ? Number(match[1] ?? 0) : undefined;
|
|
61
|
+
};
|
|
62
|
+
function contentIds(node, into = []) {
|
|
63
|
+
for (const child of node.children ?? []) {
|
|
64
|
+
if (child.type === 'content' && typeof child.id === 'string')
|
|
65
|
+
into.push(child.id);
|
|
66
|
+
else
|
|
67
|
+
contentIds(child, into);
|
|
68
|
+
}
|
|
69
|
+
return into;
|
|
70
|
+
}
|
|
71
|
+
function walk(node, roles, facts) {
|
|
72
|
+
const role = text(node.role);
|
|
73
|
+
if (role !== undefined) {
|
|
74
|
+
roles.set(role, (roles.get(role) ?? 0) + 1);
|
|
75
|
+
const level = headingLevel(role);
|
|
76
|
+
if (level !== undefined)
|
|
77
|
+
facts.headings.push({ level, contentIds: contentIds(node) });
|
|
78
|
+
else if (role === 'Figure')
|
|
79
|
+
facts.figures.push(typeof node.alt === 'string' ? { alt: node.alt } : {});
|
|
80
|
+
else if (role === 'Table')
|
|
81
|
+
facts.tables += 1;
|
|
82
|
+
}
|
|
83
|
+
for (const child of node.children ?? [])
|
|
84
|
+
if (child.role !== undefined)
|
|
85
|
+
walk(child, roles, facts);
|
|
86
|
+
}
|
|
87
|
+
/** The text of each marked-content section that has an id, by id. Text goes to its innermost section with an id. */
|
|
88
|
+
async function textByContentId(page) {
|
|
89
|
+
const { items } = await page.getTextContent({ includeMarkedContent: true });
|
|
90
|
+
const byId = new Map();
|
|
91
|
+
const open = [];
|
|
92
|
+
for (const item of items) {
|
|
93
|
+
if (item.type === 'beginMarkedContentProps')
|
|
94
|
+
open.push(typeof item.id === 'string' ? item.id : null);
|
|
95
|
+
else if (item.type === 'beginMarkedContent')
|
|
96
|
+
open.push(null);
|
|
97
|
+
else if (item.type === 'endMarkedContent')
|
|
98
|
+
open.pop();
|
|
99
|
+
else if (typeof item.str === 'string') {
|
|
100
|
+
let id = null;
|
|
101
|
+
for (let depth = open.length - 1; depth >= 0 && id === null; depth -= 1)
|
|
102
|
+
id = open[depth] ?? null;
|
|
103
|
+
if (id)
|
|
104
|
+
byId.set(id, (byId.get(id) ?? '') + item.str + (item.hasEOL ? ' ' : ''));
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
return byId;
|
|
108
|
+
}
|
|
109
|
+
async function pageFonts(pdfjs, page, fonts) {
|
|
110
|
+
const { fnArray, argsArray } = await page.getOperatorList();
|
|
111
|
+
fnArray.forEach((fn, index) => {
|
|
112
|
+
const id = fn === pdfjs.OPS.setFont ? argsArray[index]?.[0] : undefined;
|
|
113
|
+
if (typeof id !== 'string' || !page.commonObjs.has(id))
|
|
114
|
+
return;
|
|
115
|
+
const font = page.commonObjs.get(id);
|
|
116
|
+
const name = text(font?.name) ?? text(font?.loadedName);
|
|
117
|
+
if (name !== undefined && !fonts.has(name))
|
|
118
|
+
fonts.set(name, { name, embedded: font?.missingFile !== true });
|
|
119
|
+
});
|
|
120
|
+
}
|
|
121
|
+
function isPdf(bytes) {
|
|
122
|
+
// The header may follow up to 1024 bytes of leading data.
|
|
123
|
+
const head = new TextDecoder('latin1').decode(bytes.subarray(0, 1024));
|
|
124
|
+
return head.includes('%PDF-');
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* Reads a compiled PDF and reports facts that matter for accessibility and output policy: tagging, title,
|
|
128
|
+
* language, the standards its XMP metadata declares, headings, figure alternative text, tables and fonts.
|
|
129
|
+
* It reports facts only. A tagged PDF with PDF/UA metadata and all the right-looking structure is not thereby
|
|
130
|
+
* conformant; use a validator such as veraPDF for that.
|
|
131
|
+
*
|
|
132
|
+
* Needs `pdfjs-dist` (an optional peer dependency); rejects with a `TypstPdfError` that says so if it is missing.
|
|
133
|
+
* Input that is not a readable PDF rejects with `INVALID_REQUEST`.
|
|
134
|
+
*/
|
|
135
|
+
export async function inspectPdf(bytes) {
|
|
136
|
+
if (!(bytes instanceof Uint8Array))
|
|
137
|
+
throw new TypstPdfError('INVALID_REQUEST', 'inspectPdf needs the PDF bytes as a Uint8Array');
|
|
138
|
+
if (!isPdf(bytes))
|
|
139
|
+
throw new TypstPdfError('INVALID_REQUEST', 'inspectPdf input is not a PDF: no %PDF- header in the first 1024 bytes');
|
|
140
|
+
const pdfjs = await requirePdfJs();
|
|
141
|
+
// pdf.js takes ownership of the buffer it is given and detaches it, so give it a copy.
|
|
142
|
+
const task = pdfjs.getDocument({
|
|
143
|
+
data: new Uint8Array(bytes),
|
|
144
|
+
disableFontFace: true,
|
|
145
|
+
useSystemFonts: false,
|
|
146
|
+
isEvalSupported: false,
|
|
147
|
+
verbosity: 0,
|
|
148
|
+
});
|
|
149
|
+
try {
|
|
150
|
+
let doc;
|
|
151
|
+
try {
|
|
152
|
+
doc = await task.promise;
|
|
153
|
+
}
|
|
154
|
+
catch (cause) {
|
|
155
|
+
const detail = cause instanceof Error && cause.message ? `: ${cause.message}` : '';
|
|
156
|
+
throw new TypstPdfError('INVALID_REQUEST', `inspectPdf could not read the PDF${detail}`, [], { cause });
|
|
157
|
+
}
|
|
158
|
+
const { info, metadata } = await doc.getMetadata();
|
|
159
|
+
const markInfo = await doc.getMarkInfo();
|
|
160
|
+
const roles = new Map();
|
|
161
|
+
const headings = [];
|
|
162
|
+
const figures = [];
|
|
163
|
+
const fonts = new Map();
|
|
164
|
+
let tables = 0;
|
|
165
|
+
let structureTree = false;
|
|
166
|
+
for (let number = 1; number <= doc.numPages; number += 1) {
|
|
167
|
+
const page = await doc.getPage(number);
|
|
168
|
+
const tree = await page.getStructTree();
|
|
169
|
+
if (tree) {
|
|
170
|
+
structureTree = true;
|
|
171
|
+
const facts = { headings: [], figures: [], tables: 0 };
|
|
172
|
+
for (const child of tree.children ?? [])
|
|
173
|
+
walk(child, roles, facts);
|
|
174
|
+
const textById = facts.headings.length ? await textByContentId(page) : new Map();
|
|
175
|
+
for (const heading of facts.headings) {
|
|
176
|
+
const joined = heading.contentIds.map(id => textById.get(id) ?? '').join('');
|
|
177
|
+
headings.push({ level: heading.level, text: joined.replace(/\s+/g, ' ').trim() });
|
|
178
|
+
}
|
|
179
|
+
figures.push(...facts.figures);
|
|
180
|
+
tables += facts.tables;
|
|
181
|
+
}
|
|
182
|
+
await pageFonts(pdfjs, page, fonts);
|
|
183
|
+
}
|
|
184
|
+
const marked = markInfo instanceof Map ? markInfo.get('Marked') : markInfo?.Marked;
|
|
185
|
+
const part = integer(metadataValue(metadata, 'pdfaid:part'));
|
|
186
|
+
const conformance = text(metadataValue(metadata, 'pdfaid:conformance'));
|
|
187
|
+
return {
|
|
188
|
+
pages: doc.numPages,
|
|
189
|
+
tagged: marked === true,
|
|
190
|
+
structureTree,
|
|
191
|
+
title: text(info?.Title) ?? text(metadataValue(metadata, 'dc:title')),
|
|
192
|
+
language: text(info?.Language),
|
|
193
|
+
creationDate: creationDate(pdfjs, info, metadata),
|
|
194
|
+
standard: {
|
|
195
|
+
pdfUa: integer(metadataValue(metadata, 'pdfuaid:part')),
|
|
196
|
+
pdfA: part === undefined ? undefined : conformance === undefined ? { part } : { part, conformance },
|
|
197
|
+
},
|
|
198
|
+
headings,
|
|
199
|
+
figures,
|
|
200
|
+
tables,
|
|
201
|
+
fonts: [...fonts.values()],
|
|
202
|
+
structureRoles: Object.fromEntries(roles),
|
|
203
|
+
};
|
|
204
|
+
}
|
|
205
|
+
finally {
|
|
206
|
+
await task.destroy();
|
|
207
|
+
}
|
|
208
|
+
}
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
export declare const sampleFile = "sample.typ";
|
|
2
|
+
export declare const sampleSource = "// @local/typfolio: sample builder. Not used at runtime.\n//\n// `lib.typ` imports this file only from the editor branch of `input()`, so a compile under Typfolio\n// never loads it (and core does not even mount it). It ships next to `lib.typ` because an editor reads\n// the package folder from disk. `@typfolio/dev-tools` mounts it for build-time template checks.\n//\n// `sample-from-schema(schema, mode: \"full\")` builds a value from a JSON Schema (a dictionary):\n// \"full\" shape-complete, for editor completion: every property, one array item, non-null shapes.\n// \"minimal\" the sparsest value the schema allows, to expose templates that assume optional data.\n// In both, an image field (`contentMediaType: \"image/...\"`) is a placeholder image's bytes.\n// The sample is not validated against the schema.\n\n#let _is-empty(v) = (type(v) == array or type(v) == dictionary) and v.len() == 0\n\n// A field that holds the path of an image file says so with `contentMediaType: \"image/...\"`. In the\n// editor and in template checks there is no such file to read, so the sample is the bytes of a grey\n// placeholder image instead, which `image()` draws: write `image(data.logo)`, not `image(\"/\" + ...)`.\n#let _placeholder = bytes(\"<svg xmlns=\\\"http://www.w3.org/2000/svg\\\" width=\\\"400\\\" height=\\\"200\\\" viewBox=\\\"0 0 400 200\\\"><rect width=\\\"400\\\" height=\\\"200\\\" fill=\\\"#e6e8eb\\\"/><path d=\\\"M0 0L400 200M400 0L0 200\\\" stroke=\\\"#c4c8cd\\\" stroke-width=\\\"2\\\"/></svg>\")\n#let _is-image(node) = {\n let media = node.at(\"contentMediaType\", default: none)\n type(media) == str and media.starts-with(\"image/\")\n}\n\n#let _formats = (\n \"date\": \"2024-01-15\",\n \"date-time\": \"2024-01-15T09:30:00Z\",\n \"email\": \"user@example.com\",\n \"uri\": \"https://example.com\",\n \"uuid\": \"123e4567-e89b-12d3-a456-426614174000\",\n)\n\n// Resolves a local \"#\" or \"#/...\" reference against the root schema.\n#let _resolve(root, ref) = {\n if ref == \"#\" { return root }\n let node = root\n for part in ref.slice(2).split(\"/\") {\n node = node.at(part.replace(\"~1\", \"/\").replace(\"~0\", \"~\"))\n }\n node\n}\n\n// `seen` holds the $ref values being expanded on the current path. A recursive schema is expanded\n// one level (a tree gets one child) and then cut: the cut value is `_cut`, which an array turns into\n// no items and an object into a missing optional property, so a recursive list ends empty instead\n// of holding `none`. `depth` bounds anything else.\n#let _cut = \"\\u{E000}typfolio-recursion-cut\"\n#let _recursing(seen, ref) = seen.filter(r => r == ref).len() >= 2\n\n// An object's properties from their samples: a cut optional property is left out, a cut required\n// one is `none` (the schema demands an endless value there).\n#let _object(node, sampled) = {\n let required = node.at(\"required\", default: ())\n let out = (:)\n for (key, v) in sampled {\n if v == _cut {\n if key in required { out.insert(key, none) }\n } else {\n out.insert(key, v)\n }\n }\n out\n}\n\n#let _sample(root, node, depth, seen) = {\n if depth > 16 or type(node) != dictionary { return none }\n if _is-image(node) { return _placeholder }\n let candidates = ()\n if \"examples\" in node { candidates += node.at(\"examples\") }\n if \"default\" in node { candidates.push(node.at(\"default\")) }\n // An empty [] or {} default is not preferred over a generated shape.\n let filled = candidates.filter(v => v != none and not _is-empty(v))\n if filled.len() > 0 and type(filled.first()) != dictionary { return filled.first() }\n if \"const\" in node and node.at(\"const\") != none { return node.at(\"const\") }\n if \"enum\" in node {\n let e = node.at(\"enum\").filter(v => v != none)\n if e.len() > 0 { return e.first() }\n }\n if \"$ref\" in node {\n let ref = node.at(\"$ref\")\n if _recursing(seen, ref) { return _cut }\n return _sample(root, _resolve(root, ref), depth + 1, seen + (ref,))\n }\n for key in (\"anyOf\", \"oneOf\") {\n if key in node {\n for branch in node.at(key) {\n let v = _sample(root, branch, depth + 1, seen)\n if v != none and v != _cut { return v }\n }\n }\n }\n if \"allOf\" in node {\n let merged = (:)\n for part in node.at(\"allOf\") {\n let v = _sample(root, part, depth + 1, seen)\n if type(v) == dictionary { merged += v }\n }\n return merged\n }\n let t = node.at(\"type\", default: none)\n if type(t) == array { t = t.filter(x => x != \"null\").at(0, default: \"null\") }\n if t == none {\n if \"properties\" in node { t = \"object\" } else if \"items\" in node { t = \"array\" }\n }\n if t == \"object\" {\n let sampled = (:)\n for (key, sub) in node.at(\"properties\", default: (:)) {\n sampled.insert(key, _sample(root, sub, depth + 1, seen))\n }\n let out = _object(node, sampled)\n // A partial object default fills in the generated shape rather than replacing it.\n if filled.len() > 0 { out += filled.first() }\n return out\n }\n if t == \"array\" {\n let items = node.at(\"items\", default: none)\n if items == none { return () }\n let item = _sample(root, items, depth + 1, seen)\n if item == _cut { return () }\n return range(calc.max(1, node.at(\"minItems\", default: 1))).map(_ => item)\n }\n if t == \"string\" { return _formats.at(node.at(\"format\", default: \"\"), default: \"Sample text\") }\n if t == \"integer\" or t == \"number\" { return node.at(\"minimum\", default: 1) }\n if t == \"boolean\" { return true }\n none\n}\n\n// Minimal mode. Only `required` properties, nullable values are `none`, arrays are empty unless\n// `minItems` says otherwise, `examples` and `default` are ignored. `$ref` and `allOf` follow full mode.\n#let _is-nullable(node) = {\n let t = node.at(\"type\", default: none)\n if t == \"null\" or (type(t) == array and \"null\" in t) { return true }\n for key in (\"anyOf\", \"oneOf\") {\n for branch in node.at(key, default: ()) {\n if type(branch) == dictionary and branch.at(\"type\", default: none) == \"null\" { return true }\n }\n }\n false\n}\n\n#let _minimal(root, node, depth, seen) = {\n if depth > 16 or type(node) != dictionary { return none }\n if _is-nullable(node) { return none }\n if _is-image(node) { return _placeholder }\n if \"const\" in node { return node.at(\"const\") }\n if \"enum\" in node and node.at(\"enum\").len() > 0 { return node.at(\"enum\").first() }\n if \"$ref\" in node {\n let ref = node.at(\"$ref\")\n if _recursing(seen, ref) { return _cut }\n return _minimal(root, _resolve(root, ref), depth + 1, seen + (ref,))\n }\n for key in (\"anyOf\", \"oneOf\") {\n if key in node and node.at(key).len() > 0 {\n let v = _minimal(root, node.at(key).first(), depth + 1, seen)\n return if v == _cut { none } else { v }\n }\n }\n if \"allOf\" in node {\n let merged = (:)\n for part in node.at(\"allOf\") {\n let v = _minimal(root, part, depth + 1, seen)\n if type(v) == dictionary { merged += v }\n }\n return merged\n }\n let t = node.at(\"type\", default: none)\n if type(t) == array { t = t.at(0, default: none) }\n if t == none {\n if \"properties\" in node { t = \"object\" } else if \"items\" in node { t = \"array\" }\n }\n if t == \"object\" {\n let sampled = (:)\n let properties = node.at(\"properties\", default: (:))\n for key in node.at(\"required\", default: ()) {\n sampled.insert(key, _minimal(root, properties.at(key, default: none), depth + 1, seen))\n }\n return _object(node, sampled)\n }\n if t == \"array\" {\n let items = node.at(\"items\", default: none)\n let count = node.at(\"minItems\", default: 0)\n if items == none or count <= 0 { return () }\n let item = _minimal(root, items, depth + 1, seen)\n if item == _cut { return () }\n return range(count).map(_ => item)\n }\n if t == \"string\" { return \"a\" * node.at(\"minLength\", default: 0) }\n if t == \"integer\" or t == \"number\" {\n if \"minimum\" in node { return node.at(\"minimum\") }\n let above = node.at(\"exclusiveMinimum\", default: none)\n if t == \"integer\" and type(above) in (int, float) { return above + 1 }\n return 0\n }\n if t == \"boolean\" { return false }\n none\n}\n\n/// Builds a sample value from a JSON Schema (a dictionary). `mode` is \"full\" or \"minimal\".\n#let sample-from-schema(schema, mode: \"full\") = {\n // The root is itself an expansion of \"#\", so a schema that refers to itself recurses one level too.\n let value = if mode == \"full\" { _sample(schema, schema, 0, (\"#\",)) }\n else if mode == \"minimal\" { _minimal(schema, schema, 0, (\"#\",)) }\n else { panic(\"@local/typfolio: sample mode must be \\\"full\\\" or \\\"minimal\\\", got \" + repr(mode)) }\n if value == _cut { none } else { value }\n}\n";
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
// GENERATED FILE. Do not edit.
|
|
2
|
+
// Source of truth: packages/core/typst-packages/local/typfolio/0.1.0/sample.typ.
|
|
3
|
+
// Only @typfolio/dev-tools may import this module; the runtime entry must never reach it.
|
|
4
|
+
// Regenerate with `node tools/generate-typst-package.mjs` (the root `npm run build` does this first).
|
|
5
|
+
export const sampleFile = 'sample.typ';
|
|
6
|
+
export const sampleSource = "// @local/typfolio: sample builder. Not used at runtime.\n//\n// `lib.typ` imports this file only from the editor branch of `input()`, so a compile under Typfolio\n// never loads it (and core does not even mount it). It ships next to `lib.typ` because an editor reads\n// the package folder from disk. `@typfolio/dev-tools` mounts it for build-time template checks.\n//\n// `sample-from-schema(schema, mode: \"full\")` builds a value from a JSON Schema (a dictionary):\n// \"full\" shape-complete, for editor completion: every property, one array item, non-null shapes.\n// \"minimal\" the sparsest value the schema allows, to expose templates that assume optional data.\n// In both, an image field (`contentMediaType: \"image/...\"`) is a placeholder image's bytes.\n// The sample is not validated against the schema.\n\n#let _is-empty(v) = (type(v) == array or type(v) == dictionary) and v.len() == 0\n\n// A field that holds the path of an image file says so with `contentMediaType: \"image/...\"`. In the\n// editor and in template checks there is no such file to read, so the sample is the bytes of a grey\n// placeholder image instead, which `image()` draws: write `image(data.logo)`, not `image(\"/\" + ...)`.\n#let _placeholder = bytes(\"<svg xmlns=\\\"http://www.w3.org/2000/svg\\\" width=\\\"400\\\" height=\\\"200\\\" viewBox=\\\"0 0 400 200\\\"><rect width=\\\"400\\\" height=\\\"200\\\" fill=\\\"#e6e8eb\\\"/><path d=\\\"M0 0L400 200M400 0L0 200\\\" stroke=\\\"#c4c8cd\\\" stroke-width=\\\"2\\\"/></svg>\")\n#let _is-image(node) = {\n let media = node.at(\"contentMediaType\", default: none)\n type(media) == str and media.starts-with(\"image/\")\n}\n\n#let _formats = (\n \"date\": \"2024-01-15\",\n \"date-time\": \"2024-01-15T09:30:00Z\",\n \"email\": \"user@example.com\",\n \"uri\": \"https://example.com\",\n \"uuid\": \"123e4567-e89b-12d3-a456-426614174000\",\n)\n\n// Resolves a local \"#\" or \"#/...\" reference against the root schema.\n#let _resolve(root, ref) = {\n if ref == \"#\" { return root }\n let node = root\n for part in ref.slice(2).split(\"/\") {\n node = node.at(part.replace(\"~1\", \"/\").replace(\"~0\", \"~\"))\n }\n node\n}\n\n// `seen` holds the $ref values being expanded on the current path. A recursive schema is expanded\n// one level (a tree gets one child) and then cut: the cut value is `_cut`, which an array turns into\n// no items and an object into a missing optional property, so a recursive list ends empty instead\n// of holding `none`. `depth` bounds anything else.\n#let _cut = \"\\u{E000}typfolio-recursion-cut\"\n#let _recursing(seen, ref) = seen.filter(r => r == ref).len() >= 2\n\n// An object's properties from their samples: a cut optional property is left out, a cut required\n// one is `none` (the schema demands an endless value there).\n#let _object(node, sampled) = {\n let required = node.at(\"required\", default: ())\n let out = (:)\n for (key, v) in sampled {\n if v == _cut {\n if key in required { out.insert(key, none) }\n } else {\n out.insert(key, v)\n }\n }\n out\n}\n\n#let _sample(root, node, depth, seen) = {\n if depth > 16 or type(node) != dictionary { return none }\n if _is-image(node) { return _placeholder }\n let candidates = ()\n if \"examples\" in node { candidates += node.at(\"examples\") }\n if \"default\" in node { candidates.push(node.at(\"default\")) }\n // An empty [] or {} default is not preferred over a generated shape.\n let filled = candidates.filter(v => v != none and not _is-empty(v))\n if filled.len() > 0 and type(filled.first()) != dictionary { return filled.first() }\n if \"const\" in node and node.at(\"const\") != none { return node.at(\"const\") }\n if \"enum\" in node {\n let e = node.at(\"enum\").filter(v => v != none)\n if e.len() > 0 { return e.first() }\n }\n if \"$ref\" in node {\n let ref = node.at(\"$ref\")\n if _recursing(seen, ref) { return _cut }\n return _sample(root, _resolve(root, ref), depth + 1, seen + (ref,))\n }\n for key in (\"anyOf\", \"oneOf\") {\n if key in node {\n for branch in node.at(key) {\n let v = _sample(root, branch, depth + 1, seen)\n if v != none and v != _cut { return v }\n }\n }\n }\n if \"allOf\" in node {\n let merged = (:)\n for part in node.at(\"allOf\") {\n let v = _sample(root, part, depth + 1, seen)\n if type(v) == dictionary { merged += v }\n }\n return merged\n }\n let t = node.at(\"type\", default: none)\n if type(t) == array { t = t.filter(x => x != \"null\").at(0, default: \"null\") }\n if t == none {\n if \"properties\" in node { t = \"object\" } else if \"items\" in node { t = \"array\" }\n }\n if t == \"object\" {\n let sampled = (:)\n for (key, sub) in node.at(\"properties\", default: (:)) {\n sampled.insert(key, _sample(root, sub, depth + 1, seen))\n }\n let out = _object(node, sampled)\n // A partial object default fills in the generated shape rather than replacing it.\n if filled.len() > 0 { out += filled.first() }\n return out\n }\n if t == \"array\" {\n let items = node.at(\"items\", default: none)\n if items == none { return () }\n let item = _sample(root, items, depth + 1, seen)\n if item == _cut { return () }\n return range(calc.max(1, node.at(\"minItems\", default: 1))).map(_ => item)\n }\n if t == \"string\" { return _formats.at(node.at(\"format\", default: \"\"), default: \"Sample text\") }\n if t == \"integer\" or t == \"number\" { return node.at(\"minimum\", default: 1) }\n if t == \"boolean\" { return true }\n none\n}\n\n// Minimal mode. Only `required` properties, nullable values are `none`, arrays are empty unless\n// `minItems` says otherwise, `examples` and `default` are ignored. `$ref` and `allOf` follow full mode.\n#let _is-nullable(node) = {\n let t = node.at(\"type\", default: none)\n if t == \"null\" or (type(t) == array and \"null\" in t) { return true }\n for key in (\"anyOf\", \"oneOf\") {\n for branch in node.at(key, default: ()) {\n if type(branch) == dictionary and branch.at(\"type\", default: none) == \"null\" { return true }\n }\n }\n false\n}\n\n#let _minimal(root, node, depth, seen) = {\n if depth > 16 or type(node) != dictionary { return none }\n if _is-nullable(node) { return none }\n if _is-image(node) { return _placeholder }\n if \"const\" in node { return node.at(\"const\") }\n if \"enum\" in node and node.at(\"enum\").len() > 0 { return node.at(\"enum\").first() }\n if \"$ref\" in node {\n let ref = node.at(\"$ref\")\n if _recursing(seen, ref) { return _cut }\n return _minimal(root, _resolve(root, ref), depth + 1, seen + (ref,))\n }\n for key in (\"anyOf\", \"oneOf\") {\n if key in node and node.at(key).len() > 0 {\n let v = _minimal(root, node.at(key).first(), depth + 1, seen)\n return if v == _cut { none } else { v }\n }\n }\n if \"allOf\" in node {\n let merged = (:)\n for part in node.at(\"allOf\") {\n let v = _minimal(root, part, depth + 1, seen)\n if type(v) == dictionary { merged += v }\n }\n return merged\n }\n let t = node.at(\"type\", default: none)\n if type(t) == array { t = t.at(0, default: none) }\n if t == none {\n if \"properties\" in node { t = \"object\" } else if \"items\" in node { t = \"array\" }\n }\n if t == \"object\" {\n let sampled = (:)\n let properties = node.at(\"properties\", default: (:))\n for key in node.at(\"required\", default: ()) {\n sampled.insert(key, _minimal(root, properties.at(key, default: none), depth + 1, seen))\n }\n return _object(node, sampled)\n }\n if t == \"array\" {\n let items = node.at(\"items\", default: none)\n let count = node.at(\"minItems\", default: 0)\n if items == none or count <= 0 { return () }\n let item = _minimal(root, items, depth + 1, seen)\n if item == _cut { return () }\n return range(count).map(_ => item)\n }\n if t == \"string\" { return \"a\" * node.at(\"minLength\", default: 0) }\n if t == \"integer\" or t == \"number\" {\n if \"minimum\" in node { return node.at(\"minimum\") }\n let above = node.at(\"exclusiveMinimum\", default: none)\n if t == \"integer\" and type(above) in (int, float) { return above + 1 }\n return 0\n }\n if t == \"boolean\" { return false }\n none\n}\n\n/// Builds a sample value from a JSON Schema (a dictionary). `mode` is \"full\" or \"minimal\".\n#let sample-from-schema(schema, mode: \"full\") = {\n // The root is itself an expansion of \"#\", so a schema that refers to itself recurses one level too.\n let value = if mode == \"full\" { _sample(schema, schema, 0, (\"#\",)) }\n else if mode == \"minimal\" { _minimal(schema, schema, 0, (\"#\",)) }\n else { panic(\"@local/typfolio: sample mode must be \\\"full\\\" or \\\"minimal\\\", got \" + repr(mode)) }\n if value == _cut { none } else { value }\n}\n";
|
package/package.json
CHANGED
|
@@ -1,6 +1,40 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@typfolio/dev-tools",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.2.0",
|
|
4
|
+
"description": "Dev-time tools for Typfolio templates: schema-based template checks and PDF inspection (install as a devDependency)",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"license": "Apache-2.0",
|
|
7
|
+
"keywords": ["typst", "pdf", "pdf-ua", "accessibility", "template", "testing", "ci", "json-schema"],
|
|
8
|
+
"repository": {
|
|
9
|
+
"type": "git",
|
|
10
|
+
"url": "git+https://github.com/HelioCampus/typfolio.git",
|
|
11
|
+
"directory": "packages/dev-tools"
|
|
12
|
+
},
|
|
13
|
+
"homepage": "https://github.com/HelioCampus/typfolio#readme",
|
|
14
|
+
"bugs": { "url": "https://github.com/HelioCampus/typfolio/issues" },
|
|
15
|
+
"files": ["dist", "README.md"],
|
|
16
|
+
"types": "./dist/index.d.ts",
|
|
17
|
+
"exports": {
|
|
18
|
+
".": { "types": "./dist/index.d.ts", "import": "./dist/index.js" },
|
|
19
|
+
"./pdf": { "types": "./dist/pdf.d.ts", "import": "./dist/pdf.js" }
|
|
20
|
+
},
|
|
21
|
+
"typesVersions": {
|
|
22
|
+
"*": {
|
|
23
|
+
"pdf": ["./dist/pdf.d.ts"]
|
|
24
|
+
}
|
|
25
|
+
},
|
|
26
|
+
"dependencies": {
|
|
27
|
+
"@typfolio/core": "^0.2.0"
|
|
28
|
+
},
|
|
29
|
+
"peerDependencies": {
|
|
30
|
+
"pdfjs-dist": "^6.3.0"
|
|
31
|
+
},
|
|
32
|
+
"peerDependenciesMeta": {
|
|
33
|
+
"pdfjs-dist": { "optional": true }
|
|
34
|
+
},
|
|
35
|
+
"publishConfig": {
|
|
36
|
+
"access": "public"
|
|
37
|
+
},
|
|
38
|
+
"engines": { "node": ">=22" },
|
|
39
|
+
"sideEffects": false
|
|
40
|
+
}
|