@yaks/yaml 0.0.0-stage → 0.2.36
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 +49 -2
- package/_dist/mod.d.ts +41 -0
- package/_dist/mod.d.ts.map +1 -0
- package/_dist/yaml.d.ts +44 -0
- package/_dist/yaml.d.ts.map +1 -0
- package/deno.json +1 -0
- package/mod.js +41 -0
- package/mod.js.map +1 -0
- package/mod.ts +42 -0
- package/package.json +17 -4
- package/yaml.js +91 -0
- package/yaml.js.map +1 -0
- package/yaml.ts +107 -0
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 2026 Jeff Peterson
|
|
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,50 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @yaks/yaml
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Parse YAML or JSON values, split Markdown frontmatter from document text, and
|
|
4
|
+
substitute named placeholders in strings. This package accepts strings; callers
|
|
5
|
+
read files and decide how to validate or apply the returned data. It has no
|
|
6
|
+
platform API dependency.
|
|
7
|
+
|
|
8
|
+
```ts
|
|
9
|
+
import { front, read } from '@yaks/yaml'
|
|
10
|
+
|
|
11
|
+
let config = read('port: 8080', 'config.yml')
|
|
12
|
+
let page = front('---\ndoc:\n title: Hello\n---\nBody', 'page.md')
|
|
13
|
+
// page.meta = { doc: { title: 'Hello' } }; page.body = 'Body'
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
## Parsing and validation
|
|
17
|
+
|
|
18
|
+
`read(text, file?)` uses the YAML parser, including for JSON input. The optional
|
|
19
|
+
file label appears in parse errors; it does not cause a filesystem read.
|
|
20
|
+
|
|
21
|
+
`front(text, file?)` recognizes a `---` block only at the start of the string.
|
|
22
|
+
Without a complete block it returns empty metadata and the original text.
|
|
23
|
+
Empty/null frontmatter becomes `{}`; arrays and scalar metadata are rejected.
|
|
24
|
+
The body after the closing delimiter is preserved, including blank lines.
|
|
25
|
+
|
|
26
|
+
Metadata is typed as a partial graph bundle (a JSON object containing one
|
|
27
|
+
entity's identifier and named component objects), for example
|
|
28
|
+
`{entity: {eid: '$page'}, doc: {title: 'Hello'}}`. The parser checks that it is
|
|
29
|
+
an object, not that components or properties match a vocabulary. The caller owns
|
|
30
|
+
schema validation, entity identity and alias resolution. References are not
|
|
31
|
+
resolved as file paths, and parsing does not write to a graph.
|
|
32
|
+
|
|
33
|
+
## String templates
|
|
34
|
+
|
|
35
|
+
`fill(text, slots)` replaces `{{name}}` with `slots.name`. Names may contain
|
|
36
|
+
letters, digits, underscores and hyphens; unmatched placeholders remain
|
|
37
|
+
unchanged. Replacement values are inserted literally, without escaping or
|
|
38
|
+
recursive expansion. Do not use this as an HTML or SQL escaping function.
|
|
39
|
+
|
|
40
|
+
```ts
|
|
41
|
+
import { fill } from '@yaks/yaml'
|
|
42
|
+
|
|
43
|
+
fill('Hello, {{name}}. {{unknown}}', { name: 'Ada' })
|
|
44
|
+
// 'Hello, Ada. {{unknown}}'
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
The root exports `read`, `front`, `fill`, and the `Front` result type.
|
|
48
|
+
|
|
49
|
+
Run `deno task test packages/yaml` from the repository root to check parsing and
|
|
50
|
+
frontmatter boundaries.
|
package/_dist/mod.d.ts
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @yaks/yaml — reading declaration files: YAML as a value, and a markdown
|
|
3
|
+
* file's frontmatter as a {@link https://jsr.io/@yaks/graph | bundle}.
|
|
4
|
+
*
|
|
5
|
+
* A vocabulary, a seed, a page of a guide, a tool's own description — each is a
|
|
6
|
+
* declaration, and each is better as a file somebody can read and edit than as
|
|
7
|
+
* a string inside a module. This package is the single entry point for all of
|
|
8
|
+
* them:
|
|
9
|
+
*
|
|
10
|
+
* ```ts
|
|
11
|
+
* import { front, read } from '@yaks/yaml'
|
|
12
|
+
*
|
|
13
|
+
* read('recipe:\n serves: number\n') // { recipe: { serves: 'number' } }
|
|
14
|
+
* front('---\ndoc:\n title: Soup\n---\nStir.') // { meta, body: 'Stir.' }
|
|
15
|
+
* ```
|
|
16
|
+
*
|
|
17
|
+
* ## Two calls, because there are two kinds of file
|
|
18
|
+
* A `.yml` (or `.json` — see below) is one value: `read` parses it. A `.md` is
|
|
19
|
+
* a bundle with a document under it: `front` splits the two and returns both.
|
|
20
|
+
*
|
|
21
|
+
* ## JSON keeps working, and needs no second function
|
|
22
|
+
* Every JSON file is also a YAML file, so a loader written against `read` still
|
|
23
|
+
* reads the `.json` files it always read, and gains `.yml` for nothing. No file
|
|
24
|
+
* has to be converted, and YAML is simply the more legible of the two.
|
|
25
|
+
*
|
|
26
|
+
* ## Frontmatter is a bundle
|
|
27
|
+
* `{entity: {eid}, <comp>: {…}}` — the same JSON structure `graph.apply()`
|
|
28
|
+
* accepts. So the metadata about a page is written in the same vocabulary as
|
|
29
|
+
* everything else in the graph, and a page's title is `doc.title` because a
|
|
30
|
+
* page is a doc.
|
|
31
|
+
*
|
|
32
|
+
* A reference in that frontmatter names an entity by eid or by `$alias`, never
|
|
33
|
+
* by a file path: nothing here resolves anything, and whoever applies the
|
|
34
|
+
* bundle resolves its aliases the way every other write does.
|
|
35
|
+
*
|
|
36
|
+
* It imports no platform API, so the same loader runs on a server, in a
|
|
37
|
+
* worker, and in a browser tab.
|
|
38
|
+
*
|
|
39
|
+
* @module
|
|
40
|
+
*/ export * from "./yaml.js";
|
|
41
|
+
//# sourceMappingURL=mod.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mod.d.ts","sources":["../mod.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CAuCC,GAED,0BAAyB"}
|
package/_dist/yaml.d.ts
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
export { stringify, YAMLParseError as YamlSyntaxError } from "yaml";
|
|
2
|
+
/** YAML with unresolved tags treated as syntax errors, like malformed input. */ export declare let parse: (text: string) => unknown;
|
|
3
|
+
import type { Bundle } from "@yaks/graph";
|
|
4
|
+
/**
|
|
5
|
+
* YAML — and JSON, which the YAML parser also reads — as a value. `file` is the
|
|
6
|
+
* name an error message uses for the text.
|
|
7
|
+
*
|
|
8
|
+
* ```ts
|
|
9
|
+
* read('doc:\n title: Lemon cake\n') // { doc: { title: 'Lemon cake' } }
|
|
10
|
+
* read('[{"doc": {"title": "Hi"}}]') // [ { doc: { title: 'Hi' } } ]
|
|
11
|
+
* ```
|
|
12
|
+
*/ export declare let read: (text: string, file?: string) => unknown;
|
|
13
|
+
/**
|
|
14
|
+
* `fill('Publish {{app}}', {app: 'recipes'})` — a placeholder with no value
|
|
15
|
+
* given is left as it is, so a value the caller forgot shows up as the named
|
|
16
|
+
* placeholder rather than as a gap in the text.
|
|
17
|
+
*/ export declare let fill: (text: string, slots?: Record<string, string>) => string;
|
|
18
|
+
/**
|
|
19
|
+
* What a content file holds: the bundle at the top, and the document under it.
|
|
20
|
+
*
|
|
21
|
+
* `meta` is partial because two ordinary kinds of file have no `entity` in
|
|
22
|
+
* them — one with no frontmatter at all (`{}`), and one whose frontmatter names
|
|
23
|
+
* only components, which is a bundle whose identity has not been given yet.
|
|
24
|
+
* Whether that is acceptable is the loader's decision, not this function's.
|
|
25
|
+
*/ export type Front = {
|
|
26
|
+
meta: Partial<Bundle>;
|
|
27
|
+
body: string;
|
|
28
|
+
};
|
|
29
|
+
/**
|
|
30
|
+
* A markdown file, split: the frontmatter as a bundle — `{entity: {eid},
|
|
31
|
+
* <comp>: {…}}`, the same structure `graph.apply()` accepts — and the body
|
|
32
|
+
* under it.
|
|
33
|
+
*
|
|
34
|
+
* ```ts
|
|
35
|
+
* front('---\ndoc:\n title: Hi\n---\n\nwords\n')
|
|
36
|
+
* // { meta: { doc: { title: 'Hi' } }, body: 'words\n' }
|
|
37
|
+
* ```
|
|
38
|
+
*
|
|
39
|
+
* A reference in that frontmatter names an entity by eid or by `$alias`, never
|
|
40
|
+
* by a path, so nothing here resolves anything: what is returned is the bundle
|
|
41
|
+
* as written, and whoever applies it resolves the aliases the way every other
|
|
42
|
+
* write does.
|
|
43
|
+
*/ export declare let front: (text: string, file?: string) => Front;
|
|
44
|
+
//# sourceMappingURL=yaml.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"yaml.d.ts","sources":["../yaml.ts"],"names":[],"mappings":"AAWA,SAAS,SAAS,EAAE,kBAAkB,eAAe,eAAyB;AAE9E,8EAA8E,GAC9E,OAAO,YAAI,QAAS,MAAM,MAAM,KAAG,OAAO,CAMzC;AACD,cAAc,MAAM,2BAAgC;AAOpD;;;;;;;;CAQC,GACD,OAAO,YAAI,OAAQ,MAAM,MAAM,EAAE,kBAAgB,OAAO,CAYvD;AAOD;;;;CAIC,GACD,OAAO,YAAI,OAAQ,MAAM,MAAM,EAAE,QAAO,OAAO,MAAM,EAAE,MAAM,MAAS,MAAM,CACrB;AAEvD;;;;;;;CAOC,GACD,YAAY;EAAU,MAAM,QAAQ;EAAS,MAAM,MAAM;;AAOzD;;;;;;;;;;;;;;CAcC,GACD,OAAO,YAAI,QAAS,MAAM,MAAM,EAAE,kBAAgB,MAYjD"}
|
package/deno.json
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"publish":{"exclude":["**/*_test.ts"]},"name":"@yaks/yaml","version":"0.2.36","license":"Apache-2.0","description":"Parses YAML and JSON declarations and extracts Markdown frontmatter as graph bundles.","exports":"./mod.ts","imports":{"@yaks/yaml":"./mod.ts"}}
|
package/mod.js
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @yaks/yaml — reading declaration files: YAML as a value, and a markdown
|
|
3
|
+
* file's frontmatter as a {@link https://jsr.io/@yaks/graph | bundle}.
|
|
4
|
+
*
|
|
5
|
+
* A vocabulary, a seed, a page of a guide, a tool's own description — each is a
|
|
6
|
+
* declaration, and each is better as a file somebody can read and edit than as
|
|
7
|
+
* a string inside a module. This package is the single entry point for all of
|
|
8
|
+
* them:
|
|
9
|
+
*
|
|
10
|
+
* ```ts
|
|
11
|
+
* import { front, read } from '@yaks/yaml'
|
|
12
|
+
*
|
|
13
|
+
* read('recipe:\n serves: number\n') // { recipe: { serves: 'number' } }
|
|
14
|
+
* front('---\ndoc:\n title: Soup\n---\nStir.') // { meta, body: 'Stir.' }
|
|
15
|
+
* ```
|
|
16
|
+
*
|
|
17
|
+
* ## Two calls, because there are two kinds of file
|
|
18
|
+
* A `.yml` (or `.json` — see below) is one value: `read` parses it. A `.md` is
|
|
19
|
+
* a bundle with a document under it: `front` splits the two and returns both.
|
|
20
|
+
*
|
|
21
|
+
* ## JSON keeps working, and needs no second function
|
|
22
|
+
* Every JSON file is also a YAML file, so a loader written against `read` still
|
|
23
|
+
* reads the `.json` files it always read, and gains `.yml` for nothing. No file
|
|
24
|
+
* has to be converted, and YAML is simply the more legible of the two.
|
|
25
|
+
*
|
|
26
|
+
* ## Frontmatter is a bundle
|
|
27
|
+
* `{entity: {eid}, <comp>: {…}}` — the same JSON structure `graph.apply()`
|
|
28
|
+
* accepts. So the metadata about a page is written in the same vocabulary as
|
|
29
|
+
* everything else in the graph, and a page's title is `doc.title` because a
|
|
30
|
+
* page is a doc.
|
|
31
|
+
*
|
|
32
|
+
* A reference in that frontmatter names an entity by eid or by `$alias`, never
|
|
33
|
+
* by a file path: nothing here resolves anything, and whoever applies the
|
|
34
|
+
* bundle resolves its aliases the way every other write does.
|
|
35
|
+
*
|
|
36
|
+
* It imports no platform API, so the same loader runs on a server, in a
|
|
37
|
+
* worker, and in a browser tab.
|
|
38
|
+
*
|
|
39
|
+
* @module
|
|
40
|
+
*/ export * from "./yaml.js";
|
|
41
|
+
//# sourceMappingURL=mod.js.map
|
package/mod.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mod.js","sources":["./mod.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CAuCC,GAED,0BAAyB"}
|
package/mod.ts
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @yaks/yaml — reading declaration files: YAML as a value, and a markdown
|
|
3
|
+
* file's frontmatter as a {@link https://jsr.io/@yaks/graph | bundle}.
|
|
4
|
+
*
|
|
5
|
+
* A vocabulary, a seed, a page of a guide, a tool's own description — each is a
|
|
6
|
+
* declaration, and each is better as a file somebody can read and edit than as
|
|
7
|
+
* a string inside a module. This package is the single entry point for all of
|
|
8
|
+
* them:
|
|
9
|
+
*
|
|
10
|
+
* ```ts
|
|
11
|
+
* import { front, read } from '@yaks/yaml'
|
|
12
|
+
*
|
|
13
|
+
* read('recipe:\n serves: number\n') // { recipe: { serves: 'number' } }
|
|
14
|
+
* front('---\ndoc:\n title: Soup\n---\nStir.') // { meta, body: 'Stir.' }
|
|
15
|
+
* ```
|
|
16
|
+
*
|
|
17
|
+
* ## Two calls, because there are two kinds of file
|
|
18
|
+
* A `.yml` (or `.json` — see below) is one value: `read` parses it. A `.md` is
|
|
19
|
+
* a bundle with a document under it: `front` splits the two and returns both.
|
|
20
|
+
*
|
|
21
|
+
* ## JSON keeps working, and needs no second function
|
|
22
|
+
* Every JSON file is also a YAML file, so a loader written against `read` still
|
|
23
|
+
* reads the `.json` files it always read, and gains `.yml` for nothing. No file
|
|
24
|
+
* has to be converted, and YAML is simply the more legible of the two.
|
|
25
|
+
*
|
|
26
|
+
* ## Frontmatter is a bundle
|
|
27
|
+
* `{entity: {eid}, <comp>: {…}}` — the same JSON structure `graph.apply()`
|
|
28
|
+
* accepts. So the metadata about a page is written in the same vocabulary as
|
|
29
|
+
* everything else in the graph, and a page's title is `doc.title` because a
|
|
30
|
+
* page is a doc.
|
|
31
|
+
*
|
|
32
|
+
* A reference in that frontmatter names an entity by eid or by `$alias`, never
|
|
33
|
+
* by a file path: nothing here resolves anything, and whoever applies the
|
|
34
|
+
* bundle resolves its aliases the way every other write does.
|
|
35
|
+
*
|
|
36
|
+
* It imports no platform API, so the same loader runs on a server, in a
|
|
37
|
+
* worker, and in a browser tab.
|
|
38
|
+
*
|
|
39
|
+
* @module
|
|
40
|
+
*/
|
|
41
|
+
|
|
42
|
+
export * from './yaml.js'
|
package/package.json
CHANGED
|
@@ -1,6 +1,19 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@yaks/yaml",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.2.36",
|
|
4
|
+
"homepage": "https://jsr.io/@yaks/yaml",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"exports": {
|
|
7
|
+
".": {
|
|
8
|
+
"types": "./_dist/mod.d.ts",
|
|
9
|
+
"default": "./mod.js"
|
|
10
|
+
}
|
|
11
|
+
},
|
|
12
|
+
"license": "Apache-2.0",
|
|
13
|
+
"dependencies": {
|
|
14
|
+
"@yaks/graph": "0.2.36",
|
|
15
|
+
"yaml": "^2.9.1"
|
|
16
|
+
},
|
|
17
|
+
"description": "Parses YAML and JSON declarations and extracts Markdown frontmatter as graph bundles.",
|
|
18
|
+
"repository": "https://github.com/yak-sh/yak.git"
|
|
19
|
+
}
|
package/yaml.js
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
// Reading a declaration file. Two calls: YAML as a value, and a markdown
|
|
2
|
+
// file's frontmatter as a bundle with the document under it.
|
|
3
|
+
//
|
|
4
|
+
// JSON needs no second function — every JSON file is also a YAML file — so a
|
|
5
|
+
// loader written against `read` keeps its `.json` callers and gains `.yml` for
|
|
6
|
+
// free.
|
|
7
|
+
//
|
|
8
|
+
// An error names the file. A loader is usually reading a dozen of them and the
|
|
9
|
+
// parser only knows the text it was given, so the name is passed in and used in
|
|
10
|
+
// the one message that has to say which file was wrong.
|
|
11
|
+
import { parseDocument, YAMLParseError } from "yaml";
|
|
12
|
+
export { stringify, YAMLParseError as YamlSyntaxError } from "yaml";
|
|
13
|
+
/** YAML with unresolved tags treated as syntax errors, like malformed input. */ export let parse = (text)=>{
|
|
14
|
+
let doc = parseDocument(text);
|
|
15
|
+
if (doc.errors.length) throw doc.errors[0];
|
|
16
|
+
let tag = doc.warnings.find((warning)=>warning.code == 'TAG_RESOLVE_FAILED');
|
|
17
|
+
if (tag) throw new YAMLParseError(tag.pos, 'TAG_RESOLVE_FAILED', tag.message);
|
|
18
|
+
return doc.toJS();
|
|
19
|
+
};
|
|
20
|
+
// What an error message calls the language. A file named `.json` is JSON to
|
|
21
|
+
// whoever wrote it, whichever parser happens to read it — being told their JSON
|
|
22
|
+
// is not YAML would send them looking in the wrong place.
|
|
23
|
+
let lang = (file)=>file.endsWith('.json') ? 'JSON' : 'YAML';
|
|
24
|
+
/**
|
|
25
|
+
* YAML — and JSON, which the YAML parser also reads — as a value. `file` is the
|
|
26
|
+
* name an error message uses for the text.
|
|
27
|
+
*
|
|
28
|
+
* ```ts
|
|
29
|
+
* read('doc:\n title: Lemon cake\n') // { doc: { title: 'Lemon cake' } }
|
|
30
|
+
* read('[{"doc": {"title": "Hi"}}]') // [ { doc: { title: 'Hi' } } ]
|
|
31
|
+
* ```
|
|
32
|
+
*/ export let read = (text, file = 'yaml')=>{
|
|
33
|
+
// Most of what is read is JSON, which the runtime's own parser reads many
|
|
34
|
+
// times faster than the YAML one: a store reads its vocab.json this way on
|
|
35
|
+
// every wake. Whatever it refuses is the YAML parser's to read or explain.
|
|
36
|
+
try {
|
|
37
|
+
return JSON.parse(text);
|
|
38
|
+
} catch {}
|
|
39
|
+
try {
|
|
40
|
+
return parse(text);
|
|
41
|
+
} catch (e) {
|
|
42
|
+
throw new Error(`${file} is not ${lang(file)}: ${e.message}`);
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
// Named placeholders in a passage of text. The other half of a translation
|
|
46
|
+
// file: the file holds the wording, and the code supplies the one or two values
|
|
47
|
+
// it could not know — which app, which person, which pages exist today.
|
|
48
|
+
let HOLE = /\{\{([\w-]+)\}\}/g;
|
|
49
|
+
/**
|
|
50
|
+
* `fill('Publish {{app}}', {app: 'recipes'})` — a placeholder with no value
|
|
51
|
+
* given is left as it is, so a value the caller forgot shows up as the named
|
|
52
|
+
* placeholder rather than as a gap in the text.
|
|
53
|
+
*/ export let fill = (text, slots = {})=>text.replace(HOLE, (had, name)=>slots[name] ?? had);
|
|
54
|
+
// The frontmatter block, and everything after it. `---` on its own first line
|
|
55
|
+
// opens it and `---` on its own line closes it; anything else is a document
|
|
56
|
+
// that happens to start with a horizontal rule.
|
|
57
|
+
let BLOCK = /^---[ \t]*\r?\n([\s\S]*?)\r?\n---[ \t]*(?:\r?\n|$)/;
|
|
58
|
+
/**
|
|
59
|
+
* A markdown file, split: the frontmatter as a bundle — `{entity: {eid},
|
|
60
|
+
* <comp>: {…}}`, the same structure `graph.apply()` accepts — and the body
|
|
61
|
+
* under it.
|
|
62
|
+
*
|
|
63
|
+
* ```ts
|
|
64
|
+
* front('---\ndoc:\n title: Hi\n---\n\nwords\n')
|
|
65
|
+
* // { meta: { doc: { title: 'Hi' } }, body: 'words\n' }
|
|
66
|
+
* ```
|
|
67
|
+
*
|
|
68
|
+
* A reference in that frontmatter names an entity by eid or by `$alias`, never
|
|
69
|
+
* by a path, so nothing here resolves anything: what is returned is the bundle
|
|
70
|
+
* as written, and whoever applies it resolves the aliases the way every other
|
|
71
|
+
* write does.
|
|
72
|
+
*/ export let front = (text, file = 'file')=>{
|
|
73
|
+
let hit = BLOCK.exec(text);
|
|
74
|
+
if (!hit) return {
|
|
75
|
+
meta: {},
|
|
76
|
+
body: text
|
|
77
|
+
};
|
|
78
|
+
let meta = read(hit[1], `${file} frontmatter`);
|
|
79
|
+
if (meta == null) return {
|
|
80
|
+
meta: {},
|
|
81
|
+
body: text.slice(hit[0].length)
|
|
82
|
+
};
|
|
83
|
+
if (typeof meta != 'object' || Array.isArray(meta)) {
|
|
84
|
+
throw new Error(`${file} frontmatter is not a bundle — ` + '{entity: {eid: $a}, doc: {title: "…"}}');
|
|
85
|
+
}
|
|
86
|
+
return {
|
|
87
|
+
meta: meta,
|
|
88
|
+
body: text.slice(hit[0].length)
|
|
89
|
+
};
|
|
90
|
+
};
|
|
91
|
+
//# sourceMappingURL=yaml.js.map
|
package/yaml.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"yaml.js","sources":["./yaml.ts"],"names":[],"mappings":"AAAA,yEAAyE;AACzE,6DAA6D;AAC7D,EAAE;AACF,6EAA6E;AAC7E,+EAA+E;AAC/E,QAAQ;AACR,EAAE;AACF,+EAA+E;AAC/E,gFAAgF;AAChF,wDAAwD;AACxD,SAAS,aAAa,EAAE,cAAc,eAAyB;AAC/D,SAAS,SAAS,EAAE,kBAAkB,eAAe,eAAyB;AAE9E,8EAA8E,GAC9E,OAAO,IAAI,QAAQ,CAAC;EAClB,IAAI,MAAM,cAAc;EACxB,IAAI,IAAI,MAAM,CAAC,MAAM,EAAE,MAAM,IAAI,MAAM,CAAC,EAAE;EAC1C,IAAI,MAAM,IAAI,QAAQ,CAAC,IAAI,CAAC,CAAC,UAAY,QAAQ,IAAI,IAAI;EACzD,IAAI,KAAK,MAAM,IAAI,eAAe,IAAI,GAAG,EAAE,sBAAsB,IAAI,OAAO;EAC5E,OAAO,IAAI,IAAI;AACjB,EAAC;AAGD,4EAA4E;AAC5E,gFAAgF;AAChF,0DAA0D;AAC1D,IAAI,OAAO,CAAC,OAAiB,KAAK,QAAQ,CAAC,WAAW,SAAS;AAE/D;;;;;;;;CAQC,GACD,OAAO,IAAI,OAAO,CAAC,MAAc,OAAO,MAAM;EAC5C,0EAA0E;EAC1E,2EAA2E;EAC3E,2EAA2E;EAC3E,IAAI;IACF,OAAO,KAAK,KAAK,CAAC;EACpB,EAAE,OAAM,CAAyB;EACjC,IAAI;IACF,OAAO,MAAM;EACf,EAAE,OAAO,GAAG;IACV,MAAM,IAAI,MAAM,GAAG,KAAK,QAAQ,EAAE,KAAK,MAAM,EAAE,EAAE,AAAC,EAAY,OAAO,EAAE;EACzE;AACF,EAAC;AAED,2EAA2E;AAC3E,gFAAgF;AAChF,wEAAwE;AACxE,IAAI,OAAO;AAEX;;;;CAIC,GACD,OAAO,IAAI,OAAO,CAAC,MAAc,QAAgC,CAAC,CAAC,GACjE,KAAK,OAAO,CAAC,MAAM,CAAC,KAAK,OAAS,KAAK,CAAC,KAAK,IAAI,KAAI;AAYvD,8EAA8E;AAC9E,4EAA4E;AAC5E,gDAAgD;AAChD,IAAI,QAAQ;AAEZ;;;;;;;;;;;;;;CAcC,GACD,OAAO,IAAI,QAAQ,CAAC,MAAc,OAAO,MAAM;EAC7C,IAAI,MAAM,MAAM,IAAI,CAAC;EACrB,IAAI,CAAC,KAAK,OAAO;IAAE,MAAM,CAAC;IAAG,MAAM;EAAK;EACxC,IAAI,OAAO,KAAK,GAAG,CAAC,EAAE,EAAE,GAAG,KAAK,YAAY,CAAC;EAC7C,IAAI,QAAQ,MAAM,OAAO;IAAE,MAAM,CAAC;IAAG,MAAM,KAAK,KAAK,CAAC,GAAG,CAAC,EAAE,CAAC,MAAM;EAAE;EACrE,IAAI,OAAO,QAAQ,YAAY,MAAM,OAAO,CAAC,OAAO;IAClD,MAAM,IAAI,MACR,GAAG,KAAK,+BAA+B,CAAC,GACtC;EAEN;EACA,OAAO;IAAE,MAAM;IAAgB,MAAM,KAAK,KAAK,CAAC,GAAG,CAAC,EAAE,CAAC,MAAM;EAAE;AACjE,EAAC"}
|
package/yaml.ts
ADDED
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
// Reading a declaration file. Two calls: YAML as a value, and a markdown
|
|
2
|
+
// file's frontmatter as a bundle with the document under it.
|
|
3
|
+
//
|
|
4
|
+
// JSON needs no second function — every JSON file is also a YAML file — so a
|
|
5
|
+
// loader written against `read` keeps its `.json` callers and gains `.yml` for
|
|
6
|
+
// free.
|
|
7
|
+
//
|
|
8
|
+
// An error names the file. A loader is usually reading a dozen of them and the
|
|
9
|
+
// parser only knows the text it was given, so the name is passed in and used in
|
|
10
|
+
// the one message that has to say which file was wrong.
|
|
11
|
+
import { parseDocument, YAMLParseError } from 'yaml'
|
|
12
|
+
export { stringify, YAMLParseError as YamlSyntaxError } from 'yaml'
|
|
13
|
+
|
|
14
|
+
/** YAML with unresolved tags treated as syntax errors, like malformed input. */
|
|
15
|
+
export let parse = (text: string): unknown => {
|
|
16
|
+
let doc = parseDocument(text)
|
|
17
|
+
if (doc.errors.length) throw doc.errors[0]
|
|
18
|
+
let tag = doc.warnings.find((warning) => warning.code == 'TAG_RESOLVE_FAILED')
|
|
19
|
+
if (tag) throw new YAMLParseError(tag.pos, 'TAG_RESOLVE_FAILED', tag.message)
|
|
20
|
+
return doc.toJS()
|
|
21
|
+
}
|
|
22
|
+
import type { Bundle } from 'jsr:@yaks/graph@0.2.36'
|
|
23
|
+
|
|
24
|
+
// What an error message calls the language. A file named `.json` is JSON to
|
|
25
|
+
// whoever wrote it, whichever parser happens to read it — being told their JSON
|
|
26
|
+
// is not YAML would send them looking in the wrong place.
|
|
27
|
+
let lang = (file: string) => file.endsWith('.json') ? 'JSON' : 'YAML'
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* YAML — and JSON, which the YAML parser also reads — as a value. `file` is the
|
|
31
|
+
* name an error message uses for the text.
|
|
32
|
+
*
|
|
33
|
+
* ```ts
|
|
34
|
+
* read('doc:\n title: Lemon cake\n') // { doc: { title: 'Lemon cake' } }
|
|
35
|
+
* read('[{"doc": {"title": "Hi"}}]') // [ { doc: { title: 'Hi' } } ]
|
|
36
|
+
* ```
|
|
37
|
+
*/
|
|
38
|
+
export let read = (text: string, file = 'yaml'): unknown => {
|
|
39
|
+
// Most of what is read is JSON, which the runtime's own parser reads many
|
|
40
|
+
// times faster than the YAML one: a store reads its vocab.json this way on
|
|
41
|
+
// every wake. Whatever it refuses is the YAML parser's to read or explain.
|
|
42
|
+
try {
|
|
43
|
+
return JSON.parse(text)
|
|
44
|
+
} catch { /* YAML, or neither */ }
|
|
45
|
+
try {
|
|
46
|
+
return parse(text)
|
|
47
|
+
} catch (e) {
|
|
48
|
+
throw new Error(`${file} is not ${lang(file)}: ${(e as Error).message}`)
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
// Named placeholders in a passage of text. The other half of a translation
|
|
53
|
+
// file: the file holds the wording, and the code supplies the one or two values
|
|
54
|
+
// it could not know — which app, which person, which pages exist today.
|
|
55
|
+
let HOLE = /\{\{([\w-]+)\}\}/g
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* `fill('Publish {{app}}', {app: 'recipes'})` — a placeholder with no value
|
|
59
|
+
* given is left as it is, so a value the caller forgot shows up as the named
|
|
60
|
+
* placeholder rather than as a gap in the text.
|
|
61
|
+
*/
|
|
62
|
+
export let fill = (text: string, slots: Record<string, string> = {}): string =>
|
|
63
|
+
text.replace(HOLE, (had, name) => slots[name] ?? had)
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* What a content file holds: the bundle at the top, and the document under it.
|
|
67
|
+
*
|
|
68
|
+
* `meta` is partial because two ordinary kinds of file have no `entity` in
|
|
69
|
+
* them — one with no frontmatter at all (`{}`), and one whose frontmatter names
|
|
70
|
+
* only components, which is a bundle whose identity has not been given yet.
|
|
71
|
+
* Whether that is acceptable is the loader's decision, not this function's.
|
|
72
|
+
*/
|
|
73
|
+
export type Front = { meta: Partial<Bundle>; body: string }
|
|
74
|
+
|
|
75
|
+
// The frontmatter block, and everything after it. `---` on its own first line
|
|
76
|
+
// opens it and `---` on its own line closes it; anything else is a document
|
|
77
|
+
// that happens to start with a horizontal rule.
|
|
78
|
+
let BLOCK = /^---[ \t]*\r?\n([\s\S]*?)\r?\n---[ \t]*(?:\r?\n|$)/
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* A markdown file, split: the frontmatter as a bundle — `{entity: {eid},
|
|
82
|
+
* <comp>: {…}}`, the same structure `graph.apply()` accepts — and the body
|
|
83
|
+
* under it.
|
|
84
|
+
*
|
|
85
|
+
* ```ts
|
|
86
|
+
* front('---\ndoc:\n title: Hi\n---\n\nwords\n')
|
|
87
|
+
* // { meta: { doc: { title: 'Hi' } }, body: 'words\n' }
|
|
88
|
+
* ```
|
|
89
|
+
*
|
|
90
|
+
* A reference in that frontmatter names an entity by eid or by `$alias`, never
|
|
91
|
+
* by a path, so nothing here resolves anything: what is returned is the bundle
|
|
92
|
+
* as written, and whoever applies it resolves the aliases the way every other
|
|
93
|
+
* write does.
|
|
94
|
+
*/
|
|
95
|
+
export let front = (text: string, file = 'file'): Front => {
|
|
96
|
+
let hit = BLOCK.exec(text)
|
|
97
|
+
if (!hit) return { meta: {}, body: text }
|
|
98
|
+
let meta = read(hit[1], `${file} frontmatter`)
|
|
99
|
+
if (meta == null) return { meta: {}, body: text.slice(hit[0].length) }
|
|
100
|
+
if (typeof meta != 'object' || Array.isArray(meta)) {
|
|
101
|
+
throw new Error(
|
|
102
|
+
`${file} frontmatter is not a bundle — ` +
|
|
103
|
+
'{entity: {eid: $a}, doc: {title: "…"}}',
|
|
104
|
+
)
|
|
105
|
+
}
|
|
106
|
+
return { meta: meta as Bundle, body: text.slice(hit[0].length) }
|
|
107
|
+
}
|