@typfolio/dev-tools 0.0.0-stage → 0.1.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 +40 -2
- package/dist/index.d.ts +39 -0
- package/dist/index.js +85 -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 +32 -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,41 @@
|
|
|
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
|
+
`checkTemplate(template, { wasm, fonts, modes?, pdf? })` is the one-shot form that creates and disposes a checker.
|
|
21
|
+
|
|
22
|
+
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.
|
|
23
|
+
|
|
24
|
+
## `inspectPdf`
|
|
25
|
+
|
|
26
|
+
```ts
|
|
27
|
+
import { inspectPdf } from '@typfolio/dev-tools/pdf';
|
|
28
|
+
|
|
29
|
+
const report = await inspectPdf(bytes); // the bytes of a compiled PDF
|
|
30
|
+
// report.pages, tagged, structureTree, title, language, creationDate
|
|
31
|
+
// report.standard: { pdfUa?, pdfA?: { part, conformance? } } what the XMP metadata declares
|
|
32
|
+
// report.headings: [{ level, text }] in document order
|
|
33
|
+
// report.figures: [{ alt? }], report.tables, report.fonts: [{ name, embedded }]
|
|
34
|
+
// report.structureRoles: { H1: 1, Table: 1, ... } how often each structure role occurs
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
`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.
|
|
38
|
+
|
|
39
|
+
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.
|
|
40
|
+
|
|
41
|
+
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,39 @@
|
|
|
1
|
+
import { TypstPdfError, type CompilerOptions, type Diagnostic, type PdfOptions, type PdfTemplate } 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 TemplateCheckOptions {
|
|
5
|
+
/** Defaults to `['minimal', 'full']`. Each mode is a separate compile. */
|
|
6
|
+
modes?: readonly SampleMode[];
|
|
7
|
+
/** Same options as a request's `pdf`. */
|
|
8
|
+
pdf?: PdfOptions;
|
|
9
|
+
}
|
|
10
|
+
export interface TemplateCheckResult {
|
|
11
|
+
mode: SampleMode;
|
|
12
|
+
ok: boolean;
|
|
13
|
+
/** Warnings when `ok`; the compile errors otherwise. */
|
|
14
|
+
diagnostics: readonly Diagnostic[];
|
|
15
|
+
/** The `COMPILATION` error of a failed mode. */
|
|
16
|
+
error?: TypstPdfError;
|
|
17
|
+
/** The PDF of a successful mode. Held in memory only; nothing is written anywhere. */
|
|
18
|
+
bytes?: Uint8Array;
|
|
19
|
+
}
|
|
20
|
+
export interface TemplateCheckReport {
|
|
21
|
+
/** True only if every requested mode compiled. */
|
|
22
|
+
ok: boolean;
|
|
23
|
+
results: TemplateCheckResult[];
|
|
24
|
+
}
|
|
25
|
+
export interface TemplateChecker {
|
|
26
|
+
/**
|
|
27
|
+
* Compiles `template` once per mode in editor mode: no runtime marker, so `input()` reads the schema
|
|
28
|
+
* from `template.files` (at the path its `json.with(...)` names, relative to the file that writes it)
|
|
29
|
+
* and builds the sample. A failed compile is reported in its result; a malformed template or option
|
|
30
|
+
* rejects with `INVALID_REQUEST` or `UNSUPPORTED_OPTION`.
|
|
31
|
+
*/
|
|
32
|
+
check(template: PdfTemplate, options?: TemplateCheckOptions): Promise<TemplateCheckReport>;
|
|
33
|
+
/** Stops accepting checks; waits for an accepted one and frees the compiler. */
|
|
34
|
+
dispose(): Promise<void>;
|
|
35
|
+
}
|
|
36
|
+
/** Same options as `createCompiler`. One active check per checker. */
|
|
37
|
+
export declare function createTemplateChecker(options: CompilerOptions): Promise<TemplateChecker>;
|
|
38
|
+
/** One-shot form of `createTemplateChecker`: creates a checker, runs one check, disposes it. */
|
|
39
|
+
export declare function checkTemplate(template: PdfTemplate, options: CompilerOptions & TemplateCheckOptions): Promise<TemplateCheckReport>;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
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 { createEngine, initialize, prepareRequest, resolveFonts, bundledPackageDirectory, packageFileMounts } from '@typfolio/core/internal';
|
|
5
|
+
import { sampleFile, sampleSource } from './sample-source.generated.js';
|
|
6
|
+
const sampleModes = ['minimal', 'full'];
|
|
7
|
+
function parseOptions(options) {
|
|
8
|
+
if (options === undefined)
|
|
9
|
+
return { modes: sampleModes };
|
|
10
|
+
if (!options || typeof options !== 'object' || Array.isArray(options))
|
|
11
|
+
throw new TypstPdfError('INVALID_REQUEST', 'check options must be a plain object');
|
|
12
|
+
const { modes = sampleModes, pdf, ...rest } = options;
|
|
13
|
+
for (const key of Object.keys(rest))
|
|
14
|
+
throw new TypstPdfError('UNSUPPORTED_OPTION', `Unsupported check option ${key}`);
|
|
15
|
+
if (!Array.isArray(modes) || !modes.length)
|
|
16
|
+
throw new TypstPdfError('INVALID_REQUEST', 'modes must be a non-empty array');
|
|
17
|
+
for (const mode of modes)
|
|
18
|
+
if (!sampleModes.includes(mode))
|
|
19
|
+
throw new TypstPdfError('UNSUPPORTED_OPTION', `Unsupported sample mode ${String(mode)}`);
|
|
20
|
+
if (new Set(modes).size !== modes.length)
|
|
21
|
+
throw new TypstPdfError('INVALID_REQUEST', 'modes must not repeat');
|
|
22
|
+
return { modes, pdf };
|
|
23
|
+
}
|
|
24
|
+
/** Same options as `createCompiler`. One active check per checker. */
|
|
25
|
+
export async function createTemplateChecker(options) {
|
|
26
|
+
const fonts = await resolveFonts(options?.fonts);
|
|
27
|
+
await initialize(options.wasm);
|
|
28
|
+
const engine = await createEngine(fonts);
|
|
29
|
+
const encoder = new TextEncoder();
|
|
30
|
+
// The package as the editor sees it: typst.toml, lib.typ and sample.typ, but no input at the package root.
|
|
31
|
+
const mounts = [...packageFileMounts(), [`${bundledPackageDirectory}/${sampleFile}`, encoder.encode(sampleSource)]];
|
|
32
|
+
let closed = false;
|
|
33
|
+
let busy = false;
|
|
34
|
+
let finished = Promise.resolve();
|
|
35
|
+
let disposal;
|
|
36
|
+
return {
|
|
37
|
+
async check(template, checkOptions) {
|
|
38
|
+
if (closed)
|
|
39
|
+
throw new TypstPdfError('DISPOSED', 'The template checker is disposed');
|
|
40
|
+
if (busy)
|
|
41
|
+
throw new TypstPdfError('BUSY', 'The template checker already has an active check');
|
|
42
|
+
const { modes, pdf } = parseOptions(checkOptions);
|
|
43
|
+
// `input: null` only fills the reserved /input.json; the editor branch never reads it.
|
|
44
|
+
const prepared = prepareRequest({ template, input: null, pdf });
|
|
45
|
+
busy = true;
|
|
46
|
+
let release;
|
|
47
|
+
finished = new Promise(resolve => { release = resolve; });
|
|
48
|
+
try {
|
|
49
|
+
const results = [];
|
|
50
|
+
for (const mode of modes) {
|
|
51
|
+
await Promise.resolve();
|
|
52
|
+
try {
|
|
53
|
+
const compiled = engine.compile(prepared, { mounts, inputs: [['typfolio-sample', mode]] });
|
|
54
|
+
results.push({ mode, ok: true, diagnostics: compiled.diagnostics, bytes: compiled.bytes });
|
|
55
|
+
}
|
|
56
|
+
catch (error) {
|
|
57
|
+
if (!(error instanceof TypstPdfError) || error.code !== 'COMPILATION')
|
|
58
|
+
throw error;
|
|
59
|
+
results.push({ mode, ok: false, diagnostics: error.diagnostics, error });
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
return { ok: results.every(result => result.ok), results };
|
|
63
|
+
}
|
|
64
|
+
finally {
|
|
65
|
+
busy = false;
|
|
66
|
+
release();
|
|
67
|
+
}
|
|
68
|
+
},
|
|
69
|
+
dispose() {
|
|
70
|
+
closed = true;
|
|
71
|
+
return disposal ??= finished.then(() => engine.dispose());
|
|
72
|
+
},
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
/** One-shot form of `createTemplateChecker`: creates a checker, runs one check, disposes it. */
|
|
76
|
+
export async function checkTemplate(template, options) {
|
|
77
|
+
const { wasm, fonts, ...checkOptions } = options ?? {};
|
|
78
|
+
const checker = await createTemplateChecker({ wasm, fonts });
|
|
79
|
+
try {
|
|
80
|
+
return await checker.check(template, checkOptions);
|
|
81
|
+
}
|
|
82
|
+
finally {
|
|
83
|
+
await checker.dispose();
|
|
84
|
+
}
|
|
85
|
+
}
|
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// 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#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 \"#/...\" reference against the root schema.\n#let _resolve(root, ref) = {\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, so a recursive schema ends at\n// `none` instead of recursing; `depth` bounds anything else.\n#let _sample(root, node, depth, seen) = {\n if depth > 16 or type(node) != dictionary { return none }\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 ref in seen { return none }\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 { 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 out = (:)\n for (key, sub) in node.at(\"properties\", default: (:)) {\n out.insert(key, _sample(root, sub, depth + 1, seen))\n }\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 return range(calc.max(1, node.at(\"minItems\", default: 1))).map(_ => _sample(root, items, depth + 1, seen))\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/// Builds a shape-complete sample value from a JSON Schema (a dictionary).\n#let sample-from-schema(schema) = _sample(schema, schema, 0, ())\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 \"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 ref in seen { return none }\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 return _minimal(root, node.at(key).first(), depth + 1, seen)\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 out = (:)\n let properties = node.at(\"properties\", default: (:))\n for key in node.at(\"required\", default: ()) {\n out.insert(key, _minimal(root, properties.at(key, default: none), depth + 1, seen))\n }\n return out\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 return range(count).map(_ => _minimal(root, items, depth + 1, seen))\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 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}\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// 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#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 \"#/...\" reference against the root schema.\n#let _resolve(root, ref) = {\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, so a recursive schema ends at\n// `none` instead of recursing; `depth` bounds anything else.\n#let _sample(root, node, depth, seen) = {\n if depth > 16 or type(node) != dictionary { return none }\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 ref in seen { return none }\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 { 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 out = (:)\n for (key, sub) in node.at(\"properties\", default: (:)) {\n out.insert(key, _sample(root, sub, depth + 1, seen))\n }\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 return range(calc.max(1, node.at(\"minItems\", default: 1))).map(_ => _sample(root, items, depth + 1, seen))\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/// Builds a shape-complete sample value from a JSON Schema (a dictionary).\n#let sample-from-schema(schema) = _sample(schema, schema, 0, ())\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 \"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 ref in seen { return none }\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 return _minimal(root, node.at(key).first(), depth + 1, seen)\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 out = (:)\n let properties = node.at(\"properties\", default: (:))\n for key in node.at(\"required\", default: ()) {\n out.insert(key, _minimal(root, properties.at(key, default: none), depth + 1, seen))\n }\n return out\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 return range(count).map(_ => _minimal(root, items, depth + 1, seen))\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 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}\n";
|
package/package.json
CHANGED
|
@@ -1,6 +1,34 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@typfolio/dev-tools",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.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
|
+
"exports": {
|
|
17
|
+
".": { "types": "./dist/index.d.ts", "import": "./dist/index.js" },
|
|
18
|
+
"./pdf": { "types": "./dist/pdf.d.ts", "import": "./dist/pdf.js" }
|
|
19
|
+
},
|
|
20
|
+
"dependencies": {
|
|
21
|
+
"@typfolio/core": "^0.1.0"
|
|
22
|
+
},
|
|
23
|
+
"peerDependencies": {
|
|
24
|
+
"pdfjs-dist": "^6.3.0"
|
|
25
|
+
},
|
|
26
|
+
"peerDependenciesMeta": {
|
|
27
|
+
"pdfjs-dist": { "optional": true }
|
|
28
|
+
},
|
|
29
|
+
"publishConfig": {
|
|
30
|
+
"access": "public"
|
|
31
|
+
},
|
|
32
|
+
"engines": { "node": ">=22" },
|
|
33
|
+
"sideEffects": false
|
|
34
|
+
}
|