@osdlc/frontmatter-lossless 0.0.0-stage → 0.0.1-stage
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/CHANGELOG.md +33 -0
- package/CONTRIBUTING.md +69 -0
- package/LICENSE-APACHE +202 -0
- package/LICENSE-MIT +21 -0
- package/README.md +125 -2
- package/SECURITY.md +23 -0
- package/dist/index.d.ts +137 -0
- package/dist/index.js +216 -0
- package/package.json +68 -3
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
## 1.0.0
|
|
4
|
+
|
|
5
|
+
### Major Changes
|
|
6
|
+
|
|
7
|
+
- 727940a: The first release on npm, and the start of the stable 1.x line. Version 0.1.0 was used only during development and was never published, so this entry lists everything the package does. It splits a Markdown note into its YAML frontmatter and body, with offsets into the source, and puts it back together byte for byte: the byte-order mark, CRLF line endings and the exact fence bytes are kept. No dependencies. It is the TypeScript twin of the `frontmatter-lossless` Rust crate, held to it by a shared conformance corpus.
|
|
8
|
+
|
|
9
|
+
- `split(src)` returns a `Split`: `yaml` (verbatim, without the `---` fences, or `null` when the note has no block), `body`, the offsets `yamlRange` (a `SourceRange`) and `bodyStart`, and a `shape` (`NoteShape`) that records the byte-order mark and the line endings of both fences (`OpenEol`, `CloseEol`). A missing or malformed closing fence is not an error: the whole note is body.
|
|
10
|
+
- `compose(frontmatter, body, shape)` rebuilds the note; passing back what `split` returned gives the original text byte for byte. New YAML written into a shape taken from an empty block gets its own line, and so does a new body written into a shape taken from a note that ended at its closing fence. Take `NoteShape` from `split` rather than building one: a minor release may add a field.
|
|
11
|
+
- `splitLenient(content)` reads hand-written input with leading blank lines or indentation before the opening fence, or trailing spaces on the fence lines. The closing fence must not be indented. It returns a `LenientSplit` (`yaml` and `body`), or `null` when there is no block. It is for reading only.
|
|
12
|
+
|
|
13
|
+
All nine exports are stable. Node.js 22 or later, ESM only, shipped as compiled JavaScript with type declarations. Tested on Linux and macOS. Licensed `MIT OR Apache-2.0`.
|
|
14
|
+
|
|
15
|
+
## History before the first public release
|
|
16
|
+
|
|
17
|
+
This version was developed in a private repository before the package moved here, and was
|
|
18
|
+
never published. It is rebuilt from the git history.
|
|
19
|
+
|
|
20
|
+
### [0.1.0] - 2026-08-07 (unpublished)
|
|
21
|
+
|
|
22
|
+
#### Added
|
|
23
|
+
|
|
24
|
+
- `split(src)` splits a note into `yaml` (verbatim, without the `---` fences, or `null` when the
|
|
25
|
+
note has no block), `body`, the source offsets `yamlRange` and `bodyStart`, and a `shape` that
|
|
26
|
+
records the byte-order mark and the line endings of both fences.
|
|
27
|
+
- `compose(frontmatter, body, shape)` rebuilds the note; passing back what `split` returned gives
|
|
28
|
+
the original text byte for byte.
|
|
29
|
+
- `splitLenient(content)` reads a block from hand-written input that may have leading blank lines
|
|
30
|
+
or trailing spaces on the fence lines. It returns `{ yaml, body }`, or `null` when there is no
|
|
31
|
+
block. It is for reading only and does not round-trip.
|
|
32
|
+
- Types `Split`, `LenientSplit`, `NoteShape`, `SourceRange`, `OpenEol` and `CloseEol`.
|
|
33
|
+
- A missing or malformed closing fence is not an error: the whole note is treated as body.
|
package/CONTRIBUTING.md
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
# Contributing
|
|
2
|
+
|
|
3
|
+
Thanks for your interest in `@osdlc/frontmatter-lossless`.
|
|
4
|
+
|
|
5
|
+
## Issues
|
|
6
|
+
|
|
7
|
+
Report bugs and ask for features in the
|
|
8
|
+
[issue tracker](https://github.com/osdlc/osdlc/issues). For a bug, include the
|
|
9
|
+
library version, your runtime and its version, and the note text that
|
|
10
|
+
misbehaves (escape a BOM or `\r` so it survives copying). Report security
|
|
11
|
+
problems privately. See [SECURITY.md](./SECURITY.md).
|
|
12
|
+
|
|
13
|
+
## Pull requests
|
|
14
|
+
|
|
15
|
+
This package lives in [osdlc/osdlc](https://github.com/osdlc/osdlc) under
|
|
16
|
+
`packages/ts/frontmatter-lossless`. Open pull requests there. They are
|
|
17
|
+
reviewed and merged there.
|
|
18
|
+
|
|
19
|
+
## Running the tests
|
|
20
|
+
|
|
21
|
+
The repository is a pnpm workspace. Install from the repository root, then
|
|
22
|
+
work in the package directory. The unit tests run under Bun, so install
|
|
23
|
+
[Bun](https://bun.sh) as well as Node 22 or later.
|
|
24
|
+
|
|
25
|
+
```sh
|
|
26
|
+
pnpm install
|
|
27
|
+
cd packages/ts/frontmatter-lossless
|
|
28
|
+
pnpm run build
|
|
29
|
+
pnpm test
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
`pnpm test` runs the Bun suite on the source, then
|
|
33
|
+
`tests/corpus-node.mjs`, which runs the conformance corpus on the built
|
|
34
|
+
package under Node. Build first.
|
|
35
|
+
|
|
36
|
+
`pnpm run smoke` runs `tests/smoke.mjs`, a short check of the built package
|
|
37
|
+
under Node. It is not published.
|
|
38
|
+
`pnpm run coverage` reports test coverage.
|
|
39
|
+
|
|
40
|
+
Other checks:
|
|
41
|
+
|
|
42
|
+
```sh
|
|
43
|
+
pnpm run typecheck
|
|
44
|
+
pnpm run lint
|
|
45
|
+
pnpm run format:check
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
## The conformance corpus
|
|
49
|
+
|
|
50
|
+
`tests/fixtures/conformance.json` is shared with the `frontmatter-lossless`
|
|
51
|
+
Rust crate. A change to how notes are split changes both implementations and
|
|
52
|
+
the corpus together, and the copy here stays byte-identical to the crate's.
|
|
53
|
+
See
|
|
54
|
+
[D-07](https://github.com/osdlc/osdlc/blob/main/packages/ts/frontmatter-lossless/docs/planning/decisions/D-07-conformance-corpus.md).
|
|
55
|
+
|
|
56
|
+
## Code style
|
|
57
|
+
|
|
58
|
+
- TypeScript, ES modules, and explicit `.ts` import paths.
|
|
59
|
+
- Prettier formats the code and oxlint checks it. Run `pnpm run format` and
|
|
60
|
+
`pnpm run lint` before you commit.
|
|
61
|
+
- Add a test for each behaviour change or bug fix.
|
|
62
|
+
- A new export gets TSDoc and a use in `examples/`, which imports the package
|
|
63
|
+
by name. A test fails if an export has no example.
|
|
64
|
+
- An experimental export carries `@experimental` and is listed in the
|
|
65
|
+
README's Stability section. A test checks that the two agree.
|
|
66
|
+
- For a change users can see, add a changeset: run `pnpm changeset` at the
|
|
67
|
+
repository root. Start a breaking change's summary with `**Breaking:**`.
|
|
68
|
+
Do not edit `CHANGELOG.md` or `version` by hand; the release writes them.
|
|
69
|
+
See [packages/README.md](https://github.com/osdlc/osdlc/blob/main/packages/README.md).
|
package/LICENSE-APACHE
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 sksizer
|
|
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/LICENSE-MIT
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 sksizer
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,126 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @osdlc/frontmatter-lossless
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Split a Markdown note into its YAML frontmatter and its body, with offsets
|
|
4
|
+
into the source, and put it back together byte for byte. The byte-order mark,
|
|
5
|
+
CRLF line endings and the exact fence bytes are kept. No dependencies.
|
|
6
|
+
|
|
7
|
+
## Why
|
|
8
|
+
|
|
9
|
+
A program that edits a note's frontmatter has to take the note apart and put
|
|
10
|
+
it back. The common tools parse the YAML and write it out again, so the
|
|
11
|
+
result can differ from the input even when no value changed: a BOM is lost,
|
|
12
|
+
`---\r\n` fences become `---\n`, or the YAML is reformatted. In version
|
|
13
|
+
control every line of the note then shows as changed.
|
|
14
|
+
|
|
15
|
+
This package does only the framing. It finds the `---` block, returns the YAML
|
|
16
|
+
text exactly as written, and rebuilds the note from the parts. Hand it back
|
|
17
|
+
what it gave you and you get the original text.
|
|
18
|
+
|
|
19
|
+
It is for programs that read or rewrite notes that people also edit: note
|
|
20
|
+
apps, static site generators, linters and sync tools.
|
|
21
|
+
|
|
22
|
+
When not to use it:
|
|
23
|
+
|
|
24
|
+
- To read the frontmatter's values. Pass the `yaml` string to a YAML parser,
|
|
25
|
+
or use `gray-matter`, which does both steps.
|
|
26
|
+
- For `+++` TOML or `;;;` JSON frontmatter. Only `---` YAML blocks are found.
|
|
27
|
+
- To parse the Markdown body. The body is returned as a string.
|
|
28
|
+
|
|
29
|
+
## Install
|
|
30
|
+
|
|
31
|
+
```sh
|
|
32
|
+
npm install @osdlc/frontmatter-lossless
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
ESM only. Node.js 22 or later.
|
|
36
|
+
|
|
37
|
+
## Quick start
|
|
38
|
+
|
|
39
|
+
```ts
|
|
40
|
+
import { compose, split } from '@osdlc/frontmatter-lossless'
|
|
41
|
+
|
|
42
|
+
const note = '---\r\ntitle: Old\r\n---\r\n# Heading\r\n'
|
|
43
|
+
|
|
44
|
+
const { yaml, body, shape } = split(note)
|
|
45
|
+
yaml // 'title: Old\r' (the YAML text, verbatim)
|
|
46
|
+
body // '# Heading\r\n'
|
|
47
|
+
|
|
48
|
+
// Change the YAML text however you like, then put the note back.
|
|
49
|
+
const updated = compose(yaml!.replace('Old', 'New'), body, shape)
|
|
50
|
+
updated // '---\r\ntitle: New\r\n---\r\n# Heading\r\n' (the CRLF fences stay)
|
|
51
|
+
|
|
52
|
+
compose(yaml, body, shape) === note // true
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
## API
|
|
56
|
+
|
|
57
|
+
- `split(src)` returns a `Split`: `yaml` (the text between the fences,
|
|
58
|
+
without them, or `null` when the note has no block), `body`, the offsets
|
|
59
|
+
`yamlRange` (a `SourceRange`) and `bodyStart`, and a `shape`. A block is
|
|
60
|
+
found only when the note starts with `---` and a line break, after an
|
|
61
|
+
optional BOM, and is closed by `---` alone on a line. Anything else,
|
|
62
|
+
including a block with no closing fence, is body. It never throws.
|
|
63
|
+
- `compose(frontmatter, body, shape)` rebuilds the note. `frontmatter` is
|
|
64
|
+
`null` for no block, `''` for an empty block, or the YAML text. Passing back
|
|
65
|
+
what `split` returned gives the original text.
|
|
66
|
+
- `NoteShape` holds the framing: `bom`, `openEol` (an `OpenEol`, `'\n'` or
|
|
67
|
+
`'\r\n'`), `closeEol` (a `CloseEol`, which can also be `''` when the note
|
|
68
|
+
ends at the closing fence) and `closeSep`. Take it from `split` and pass it
|
|
69
|
+
to `compose`. Do not build one by hand: a minor release may add a field.
|
|
70
|
+
- `splitLenient(content)` reads hand-written input that `split` rejects: blank lines or indentation
|
|
71
|
+
before the opening fence, and trailing spaces on the fence lines. The closing fence must not be
|
|
72
|
+
indented. It returns a `LenientSplit` (`yaml` and `body`), or `null` when there is no block. It
|
|
73
|
+
keeps no framing, so use it to read, not to rewrite.
|
|
74
|
+
|
|
75
|
+
Offsets are JavaScript string indices (UTF-16 code units):
|
|
76
|
+
`src.slice(yamlRange.start, yamlRange.end) === yaml` and
|
|
77
|
+
`src.slice(bodyStart) === body`.
|
|
78
|
+
|
|
79
|
+
```ts
|
|
80
|
+
import { splitLenient } from '@osdlc/frontmatter-lossless'
|
|
81
|
+
|
|
82
|
+
splitLenient('\n--- \ntitle: A\n---\nbody\n') // { yaml: 'title: A', body: 'body\n' }
|
|
83
|
+
splitLenient('no frontmatter\n') // null
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
Each export has TSDoc in the source. Your editor shows it.
|
|
87
|
+
|
|
88
|
+
## How it compares
|
|
89
|
+
|
|
90
|
+
- [gray-matter](https://github.com/jonschlinkert/gray-matter) and
|
|
91
|
+
[front-matter](https://github.com/jxson/front-matter) parse the frontmatter
|
|
92
|
+
into data. gray-matter writes a note back by serializing the data again,
|
|
93
|
+
so comments, quoting and line endings can change. front-matter only reads.
|
|
94
|
+
Use them when you want values; use this package when the note must come
|
|
95
|
+
back unchanged.
|
|
96
|
+
- [remark-frontmatter](https://github.com/remarkjs/remark-frontmatter),
|
|
97
|
+
micromark-extension-frontmatter and vfile-matter find frontmatter as part of
|
|
98
|
+
a Markdown syntax tree or a vfile. This package needs no Markdown parser and
|
|
99
|
+
works on a plain string.
|
|
100
|
+
- To edit single YAML values in place, keeping comments and key order, use a
|
|
101
|
+
YAML editor on the `yaml` string, then `compose` the result. This package
|
|
102
|
+
is the layer under such an editor.
|
|
103
|
+
|
|
104
|
+
The `frontmatter-lossless` Rust crate is this package's twin: the same
|
|
105
|
+
algorithm and the same conformance corpus. Its offsets are UTF-8 byte offsets.
|
|
106
|
+
|
|
107
|
+
## Stability
|
|
108
|
+
|
|
109
|
+
Every export is stable from 1.0.0. A breaking change to any of them needs a
|
|
110
|
+
new major version and is listed in the CHANGELOG under "Breaking".
|
|
111
|
+
|
|
112
|
+
The experimental exports:
|
|
113
|
+
|
|
114
|
+
- none
|
|
115
|
+
|
|
116
|
+
## Runtimes and platforms
|
|
117
|
+
|
|
118
|
+
Node.js 22 or later, ESM only. The built package is tested on Node.js 22 and
|
|
119
|
+
24, and the test suite also runs under Bun. The code is plain ES2022 string
|
|
120
|
+
handling with no imports, but other runtimes, such as Deno or a browser, are
|
|
121
|
+
not tested. It has no file system or platform code, so it runs on any
|
|
122
|
+
operating system. Its tests run on Linux and macOS.
|
|
123
|
+
|
|
124
|
+
## License
|
|
125
|
+
|
|
126
|
+
`MIT OR Apache-2.0`, at your option.
|
package/SECURITY.md
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
# Security policy
|
|
2
|
+
|
|
3
|
+
## Reporting a vulnerability
|
|
4
|
+
|
|
5
|
+
Please report vulnerabilities privately. Do not open a public issue.
|
|
6
|
+
|
|
7
|
+
Use GitHub private vulnerability reporting on the repository: open the
|
|
8
|
+
[Security tab](https://github.com/osdlc/osdlc/security) and choose
|
|
9
|
+
"Report a vulnerability".
|
|
10
|
+
|
|
11
|
+
Include the version, a short description, and steps to reproduce. A minimal
|
|
12
|
+
code sample helps.
|
|
13
|
+
|
|
14
|
+
## Supported versions
|
|
15
|
+
|
|
16
|
+
Only the latest release of the newest major version gets security fixes.
|
|
17
|
+
Upgrade to it.
|
|
18
|
+
|
|
19
|
+
## What to expect
|
|
20
|
+
|
|
21
|
+
This is a small project. We aim to reply within a week. We will tell you
|
|
22
|
+
whether we accept the report and keep you updated until a fix is released.
|
|
23
|
+
We will credit you in the release notes if you want that.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Byte-exact Markdown frontmatter framing.
|
|
3
|
+
*
|
|
4
|
+
* A note is `---`-fenced YAML frontmatter followed by a body. This module
|
|
5
|
+
* splits the two and recomposes them without touching a single byte the
|
|
6
|
+
* author wrote: the YAML text comes back verbatim (fences excluded), and
|
|
7
|
+
* {@link compose} rebuilds the original file exactly — same BOM, same `\n` vs
|
|
8
|
+
* `\r\n` fences, same trailing state.
|
|
9
|
+
*
|
|
10
|
+
* No YAML parsing, no file system, no dependencies: plain ES2022 string
|
|
11
|
+
* handling. Reading the frontmatter's *values* is a job for a YAML parser; this
|
|
12
|
+
* module only finds where the block is and puts it back.
|
|
13
|
+
*
|
|
14
|
+
* It is the TypeScript twin of the `frontmatter-lossless` Rust crate. It
|
|
15
|
+
* follows the crate's algorithm function for function (`split`, `compose`,
|
|
16
|
+
* `split_lenient`), and both run one shared conformance corpus.
|
|
17
|
+
*
|
|
18
|
+
* Three tolerances, each one a bug someone has already hit:
|
|
19
|
+
*
|
|
20
|
+
* - **BOM.** Notes written by some Windows editors start `\uFEFF---`. The
|
|
21
|
+
* split recognises and remembers it so a later write can restore it.
|
|
22
|
+
* - **CRLF.** The `\r` stays attached to the YAML slice, so callers see the
|
|
23
|
+
* exact text and {@link compose} rebuilds `---\r\n` fences rather than
|
|
24
|
+
* silently rewriting them to `---\n` and churning the whole file in a diff.
|
|
25
|
+
* - **A missing or malformed terminator is not an error.** An unterminated
|
|
26
|
+
* block is simply body: a projection over thousands of notes must degrade,
|
|
27
|
+
* never abort.
|
|
28
|
+
*
|
|
29
|
+
* @module @osdlc/frontmatter-lossless
|
|
30
|
+
*/
|
|
31
|
+
/**
|
|
32
|
+
* Line ending after the opening `---` fence. A closed set: a new member would
|
|
33
|
+
* be a major release, so an exhaustive `switch` over it is safe.
|
|
34
|
+
*/
|
|
35
|
+
export type OpenEol = '\n' | '\r\n';
|
|
36
|
+
/**
|
|
37
|
+
* Line ending after the closing `---` fence. Empty when the file ended there.
|
|
38
|
+
* A closed set, like {@link OpenEol}.
|
|
39
|
+
*/
|
|
40
|
+
export type CloseEol = '' | '\n' | '\r\n';
|
|
41
|
+
/**
|
|
42
|
+
* Everything about a note's framing that a writer has to put back exactly.
|
|
43
|
+
*
|
|
44
|
+
* It exists because reassembling a note is where byte fidelity quietly dies:
|
|
45
|
+
* the *values* survive a careful splice and then `---\n` gets written over a
|
|
46
|
+
* file whose fences were `---\r\n`, and now every line of the note shows as
|
|
47
|
+
* changed in git. The fences are data too.
|
|
48
|
+
*
|
|
49
|
+
* Take it from {@link split} and pass it back to {@link compose}; do not build
|
|
50
|
+
* one by hand. A minor release may add a field, which would break an object
|
|
51
|
+
* literal typed as `NoteShape`.
|
|
52
|
+
*/
|
|
53
|
+
export interface NoteShape {
|
|
54
|
+
/** The byte-order mark, if the file had one. */
|
|
55
|
+
readonly bom: boolean;
|
|
56
|
+
/** Line ending of the opening fence. */
|
|
57
|
+
readonly openEol: OpenEol;
|
|
58
|
+
/** Line ending of the closing fence, empty when the note ends at it. */
|
|
59
|
+
readonly closeEol: CloseEol;
|
|
60
|
+
/** Whether the `\n` separating the block's content from the closing `---`
|
|
61
|
+
* is present. True for every block except the adjacent-fence empty one
|
|
62
|
+
* (`---\n---`), where the closing fence directly follows the opener's
|
|
63
|
+
* newline. It is a separate bit because {@link Split.yaml} alone cannot
|
|
64
|
+
* distinguish that block from a blank-line-only one (`---\n\n---`): both
|
|
65
|
+
* yield `""`, but only the latter has a separator byte to restore. */
|
|
66
|
+
readonly closeSep: boolean;
|
|
67
|
+
}
|
|
68
|
+
/** A half-open index range `[start, end)` into the source string. */
|
|
69
|
+
export interface SourceRange {
|
|
70
|
+
readonly start: number;
|
|
71
|
+
readonly end: number;
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* A note split into its parts, with offsets into the original source.
|
|
75
|
+
*
|
|
76
|
+
* Offsets are JavaScript string indices (UTF-16 code units), so
|
|
77
|
+
* `src.slice(yamlRange.start, yamlRange.end) === yaml` and
|
|
78
|
+
* `src.slice(bodyStart) === body` hold on the string you passed in. (The Rust
|
|
79
|
+
* crate's offsets are UTF-8 byte offsets; each side indexes its own native
|
|
80
|
+
* representation — the *content* invariants are what the shared corpus pins.)
|
|
81
|
+
*/
|
|
82
|
+
export interface Split {
|
|
83
|
+
/** The frontmatter YAML, verbatim and without the `---` fences. `null`
|
|
84
|
+
* when the note has no frontmatter block at all. */
|
|
85
|
+
readonly yaml: string | null;
|
|
86
|
+
/** Range of `yaml` within the source (empty when `yaml` is `null`). */
|
|
87
|
+
readonly yamlRange: SourceRange;
|
|
88
|
+
/** Everything after the closing fence. */
|
|
89
|
+
readonly body: string;
|
|
90
|
+
/** Offset of `body` within the source. */
|
|
91
|
+
readonly bodyStart: number;
|
|
92
|
+
/** The framing to restore when writing this note back. */
|
|
93
|
+
readonly shape: NoteShape;
|
|
94
|
+
}
|
|
95
|
+
/** The result of a tolerant read-only split: see {@link splitLenient}. */
|
|
96
|
+
export interface LenientSplit {
|
|
97
|
+
/** The text between the fence lines, without the line break before the
|
|
98
|
+
* closing fence. */
|
|
99
|
+
readonly yaml: string;
|
|
100
|
+
/** Everything after the closing fence line. */
|
|
101
|
+
readonly body: string;
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* Split a note into its frontmatter YAML and body.
|
|
105
|
+
*
|
|
106
|
+
* Frontmatter is recognised only when the document begins with `---\n` (or
|
|
107
|
+
* `---\r\n`, or either after a BOM) and is closed by `---` alone on a line.
|
|
108
|
+
* Anything else — including a missing terminator — is body. A leading BOM is
|
|
109
|
+
* framing: it is recorded in {@link Split.shape} and excluded from both
|
|
110
|
+
* `yaml` and `body`, never duplicated into the content.
|
|
111
|
+
*/
|
|
112
|
+
export declare function split(src: string): Split;
|
|
113
|
+
/**
|
|
114
|
+
* Reassemble a note from a (possibly spliced) frontmatter block and a body —
|
|
115
|
+
* the exact inverse of {@link split}.
|
|
116
|
+
*
|
|
117
|
+
* `frontmatter` mirrors {@link Split.yaml}: `null` is a note with no block
|
|
118
|
+
* (just the body, after any BOM), `""` is an empty `---` / `---` block, and
|
|
119
|
+
* text is a block with content. Handing `split(src).yaml` straight back
|
|
120
|
+
* reproduces `src` byte for byte for every shape.
|
|
121
|
+
*/
|
|
122
|
+
export declare function compose(frontmatter: string | null, body: string, shape: NoteShape): string;
|
|
123
|
+
/**
|
|
124
|
+
* A tolerant, read-only frontmatter read for possibly-sloppy input.
|
|
125
|
+
*
|
|
126
|
+
* {@link split} is strict and lossless — the basis of the write path. This
|
|
127
|
+
* accepts what hand-authored notes drift into and a *reader* can safely
|
|
128
|
+
* ignore: a leading BOM, blank lines or indentation before the opening
|
|
129
|
+
* fence, and trailing whitespace on the `---` fence lines. The closing fence
|
|
130
|
+
* must not be indented. It returns `{ yaml, body }`, or `null`
|
|
131
|
+
* when there is no block (where {@link split} gives a result with
|
|
132
|
+
* `yaml: null`). It tracks no framing, so it is not a round trip —
|
|
133
|
+
* parse with it, never rewrite. A fence still closes only on `---` alone on
|
|
134
|
+
* its line (trailing whitespace aside), so a `---heading` in the body does
|
|
135
|
+
* not end the block.
|
|
136
|
+
*/
|
|
137
|
+
export declare function splitLenient(content: string): LenientSplit | null;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,216 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Byte-exact Markdown frontmatter framing.
|
|
3
|
+
*
|
|
4
|
+
* A note is `---`-fenced YAML frontmatter followed by a body. This module
|
|
5
|
+
* splits the two and recomposes them without touching a single byte the
|
|
6
|
+
* author wrote: the YAML text comes back verbatim (fences excluded), and
|
|
7
|
+
* {@link compose} rebuilds the original file exactly — same BOM, same `\n` vs
|
|
8
|
+
* `\r\n` fences, same trailing state.
|
|
9
|
+
*
|
|
10
|
+
* No YAML parsing, no file system, no dependencies: plain ES2022 string
|
|
11
|
+
* handling. Reading the frontmatter's *values* is a job for a YAML parser; this
|
|
12
|
+
* module only finds where the block is and puts it back.
|
|
13
|
+
*
|
|
14
|
+
* It is the TypeScript twin of the `frontmatter-lossless` Rust crate. It
|
|
15
|
+
* follows the crate's algorithm function for function (`split`, `compose`,
|
|
16
|
+
* `split_lenient`), and both run one shared conformance corpus.
|
|
17
|
+
*
|
|
18
|
+
* Three tolerances, each one a bug someone has already hit:
|
|
19
|
+
*
|
|
20
|
+
* - **BOM.** Notes written by some Windows editors start `\uFEFF---`. The
|
|
21
|
+
* split recognises and remembers it so a later write can restore it.
|
|
22
|
+
* - **CRLF.** The `\r` stays attached to the YAML slice, so callers see the
|
|
23
|
+
* exact text and {@link compose} rebuilds `---\r\n` fences rather than
|
|
24
|
+
* silently rewriting them to `---\n` and churning the whole file in a diff.
|
|
25
|
+
* - **A missing or malformed terminator is not an error.** An unterminated
|
|
26
|
+
* block is simply body: a projection over thousands of notes must degrade,
|
|
27
|
+
* never abort.
|
|
28
|
+
*
|
|
29
|
+
* @module @osdlc/frontmatter-lossless
|
|
30
|
+
*/
|
|
31
|
+
const BOM = '\uFEFF';
|
|
32
|
+
/**
|
|
33
|
+
* Split a note into its frontmatter YAML and body.
|
|
34
|
+
*
|
|
35
|
+
* Frontmatter is recognised only when the document begins with `---\n` (or
|
|
36
|
+
* `---\r\n`, or either after a BOM) and is closed by `---` alone on a line.
|
|
37
|
+
* Anything else — including a missing terminator — is body. A leading BOM is
|
|
38
|
+
* framing: it is recorded in {@link Split.shape} and excluded from both
|
|
39
|
+
* `yaml` and `body`, never duplicated into the content.
|
|
40
|
+
*/
|
|
41
|
+
export function split(src) {
|
|
42
|
+
const bom = src.startsWith(BOM);
|
|
43
|
+
const rest = bom ? src.slice(BOM.length) : src;
|
|
44
|
+
const offset = bom ? BOM.length : 0;
|
|
45
|
+
const noFrontmatter = () => ({
|
|
46
|
+
yaml: null,
|
|
47
|
+
yamlRange: { start: 0, end: 0 },
|
|
48
|
+
body: rest,
|
|
49
|
+
bodyStart: offset,
|
|
50
|
+
shape: { bom, openEol: '\n', closeEol: '\n', closeSep: true },
|
|
51
|
+
});
|
|
52
|
+
let afterMarker;
|
|
53
|
+
let markerLen;
|
|
54
|
+
let openEol;
|
|
55
|
+
if (rest.startsWith('---\n')) {
|
|
56
|
+
afterMarker = rest.slice(4);
|
|
57
|
+
markerLen = 4;
|
|
58
|
+
openEol = '\n';
|
|
59
|
+
}
|
|
60
|
+
else if (rest.startsWith('---\r\n')) {
|
|
61
|
+
afterMarker = rest.slice(5);
|
|
62
|
+
markerLen = 5;
|
|
63
|
+
openEol = '\r\n';
|
|
64
|
+
}
|
|
65
|
+
else {
|
|
66
|
+
return noFrontmatter();
|
|
67
|
+
}
|
|
68
|
+
const yamlStart = offset + markerLen;
|
|
69
|
+
// Empty frontmatter: the closing fence sits directly after the opener.
|
|
70
|
+
for (const [fence, len, closeEol] of [
|
|
71
|
+
['---\n', 4, '\n'],
|
|
72
|
+
['---\r\n', 5, '\r\n'],
|
|
73
|
+
]) {
|
|
74
|
+
if (afterMarker.startsWith(fence)) {
|
|
75
|
+
return {
|
|
76
|
+
yaml: '',
|
|
77
|
+
yamlRange: { start: yamlStart, end: yamlStart },
|
|
78
|
+
body: afterMarker.slice(len),
|
|
79
|
+
bodyStart: yamlStart + len,
|
|
80
|
+
shape: { bom, openEol, closeEol, closeSep: false },
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
if (afterMarker === '---') {
|
|
85
|
+
return {
|
|
86
|
+
yaml: '',
|
|
87
|
+
yamlRange: { start: yamlStart, end: yamlStart },
|
|
88
|
+
body: '',
|
|
89
|
+
bodyStart: src.length,
|
|
90
|
+
shape: { bom, openEol, closeEol: '', closeSep: false },
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
let searchStart = 0;
|
|
94
|
+
for (;;) {
|
|
95
|
+
const found = afterMarker.indexOf('\n---', searchStart);
|
|
96
|
+
if (found === -1)
|
|
97
|
+
return noFrontmatter();
|
|
98
|
+
const dashesStart = found + 1;
|
|
99
|
+
const afterDashes = dashesStart + 3;
|
|
100
|
+
const yamlLen = found;
|
|
101
|
+
let bodyRel;
|
|
102
|
+
let closeEol;
|
|
103
|
+
if (afterDashes >= afterMarker.length) {
|
|
104
|
+
bodyRel = afterMarker.length;
|
|
105
|
+
closeEol = '';
|
|
106
|
+
}
|
|
107
|
+
else if (afterMarker[afterDashes] === '\n') {
|
|
108
|
+
bodyRel = afterDashes + 1;
|
|
109
|
+
closeEol = '\n';
|
|
110
|
+
}
|
|
111
|
+
else if (afterMarker[afterDashes] === '\r' && afterMarker[afterDashes + 1] === '\n') {
|
|
112
|
+
bodyRel = afterDashes + 2;
|
|
113
|
+
closeEol = '\r\n';
|
|
114
|
+
}
|
|
115
|
+
else {
|
|
116
|
+
// `\n---foo` is not a terminator; keep looking.
|
|
117
|
+
searchStart = afterDashes;
|
|
118
|
+
continue;
|
|
119
|
+
}
|
|
120
|
+
return {
|
|
121
|
+
yaml: afterMarker.slice(0, yamlLen),
|
|
122
|
+
yamlRange: { start: yamlStart, end: yamlStart + yamlLen },
|
|
123
|
+
body: afterMarker.slice(bodyRel),
|
|
124
|
+
bodyStart: yamlStart + bodyRel,
|
|
125
|
+
shape: { bom, openEol, closeEol, closeSep: true },
|
|
126
|
+
};
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* Reassemble a note from a (possibly spliced) frontmatter block and a body —
|
|
131
|
+
* the exact inverse of {@link split}.
|
|
132
|
+
*
|
|
133
|
+
* `frontmatter` mirrors {@link Split.yaml}: `null` is a note with no block
|
|
134
|
+
* (just the body, after any BOM), `""` is an empty `---` / `---` block, and
|
|
135
|
+
* text is a block with content. Handing `split(src).yaml` straight back
|
|
136
|
+
* reproduces `src` byte for byte for every shape.
|
|
137
|
+
*/
|
|
138
|
+
export function compose(frontmatter, body, shape) {
|
|
139
|
+
let out = shape.bom ? BOM : '';
|
|
140
|
+
if (frontmatter === null) {
|
|
141
|
+
// No block: the document is just its body (which the BOM, if any, now
|
|
142
|
+
// prefixes exactly once).
|
|
143
|
+
return out + body;
|
|
144
|
+
}
|
|
145
|
+
out += '---' + shape.openEol;
|
|
146
|
+
out += frontmatter;
|
|
147
|
+
// Restore the separator the split consumed while finding the terminator —
|
|
148
|
+
// a single `\n` (under CRLF the block ends in a bare `\r`, so `\n`, not
|
|
149
|
+
// `openEol`, rebuilds the original text). It is driven by the recorded
|
|
150
|
+
// shape, never inferred from the yaml text: a block ENDING in a blank
|
|
151
|
+
// line legitimately ends with `\n` and still had a separator after it.
|
|
152
|
+
// Only empty YAML can omit it: text written into a shape taken from an
|
|
153
|
+
// empty `---\n---` block still needs its own line before the closing fence.
|
|
154
|
+
if (shape.closeSep || frontmatter !== '')
|
|
155
|
+
out += '\n';
|
|
156
|
+
// A shape from a file that ended at its closing fence has no line ending
|
|
157
|
+
// there; a new body still needs one, so borrow the opening fence's.
|
|
158
|
+
out += '---' + (shape.closeEol === '' && body !== '' ? shape.openEol : shape.closeEol) + body;
|
|
159
|
+
return out;
|
|
160
|
+
}
|
|
161
|
+
/**
|
|
162
|
+
* A tolerant, read-only frontmatter read for possibly-sloppy input.
|
|
163
|
+
*
|
|
164
|
+
* {@link split} is strict and lossless — the basis of the write path. This
|
|
165
|
+
* accepts what hand-authored notes drift into and a *reader* can safely
|
|
166
|
+
* ignore: a leading BOM, blank lines or indentation before the opening
|
|
167
|
+
* fence, and trailing whitespace on the `---` fence lines. The closing fence
|
|
168
|
+
* must not be indented. It returns `{ yaml, body }`, or `null`
|
|
169
|
+
* when there is no block (where {@link split} gives a result with
|
|
170
|
+
* `yaml: null`). It tracks no framing, so it is not a round trip —
|
|
171
|
+
* parse with it, never rewrite. A fence still closes only on `---` alone on
|
|
172
|
+
* its line (trailing whitespace aside), so a `---heading` in the body does
|
|
173
|
+
* not end the block.
|
|
174
|
+
*/
|
|
175
|
+
export function splitLenient(content) {
|
|
176
|
+
const afterBom = content.startsWith(BOM) ? content.slice(BOM.length) : content;
|
|
177
|
+
const start = trimStartWhitespace(afterBom);
|
|
178
|
+
const nl = start.indexOf('\n');
|
|
179
|
+
if (nl === -1)
|
|
180
|
+
return null;
|
|
181
|
+
if (trimEndWhitespace(start.slice(0, nl)) !== '---')
|
|
182
|
+
return null;
|
|
183
|
+
const rest = start.slice(nl + 1);
|
|
184
|
+
let offset = 0;
|
|
185
|
+
while (offset < rest.length) {
|
|
186
|
+
const lineNl = rest.indexOf('\n', offset);
|
|
187
|
+
const lineEnd = lineNl === -1 ? rest.length : lineNl + 1;
|
|
188
|
+
if (trimEndWhitespace(rest.slice(offset, lineEnd)) === '---') {
|
|
189
|
+
let yaml = rest.slice(0, offset);
|
|
190
|
+
if (yaml.endsWith('\n'))
|
|
191
|
+
yaml = yaml.slice(0, -1);
|
|
192
|
+
if (yaml.endsWith('\r'))
|
|
193
|
+
yaml = yaml.slice(0, -1);
|
|
194
|
+
return { yaml, body: rest.slice(lineEnd) };
|
|
195
|
+
}
|
|
196
|
+
offset = lineEnd;
|
|
197
|
+
}
|
|
198
|
+
return null;
|
|
199
|
+
}
|
|
200
|
+
// Rust's `str::trim_start`/`trim_end` trim exactly Unicode `White_Space`.
|
|
201
|
+
// JavaScript's `trimStart`/`trimEnd` additionally strip U+FEFF (a BOM mid-way
|
|
202
|
+
// through leading spaces would silently vanish) and miss U+0085 NEL, so the
|
|
203
|
+
// port trims by the same character class the reference does.
|
|
204
|
+
const WHITESPACE_RE = /[\t\n\v\f\r \u0085\u00a0\u1680\u2000-\u200a\u2028\u2029\u202f\u205f\u3000]/;
|
|
205
|
+
function trimStartWhitespace(s) {
|
|
206
|
+
let i = 0;
|
|
207
|
+
while (i < s.length && WHITESPACE_RE.test(s[i]))
|
|
208
|
+
i++;
|
|
209
|
+
return s.slice(i);
|
|
210
|
+
}
|
|
211
|
+
function trimEndWhitespace(s) {
|
|
212
|
+
let end = s.length;
|
|
213
|
+
while (end > 0 && WHITESPACE_RE.test(s[end - 1]))
|
|
214
|
+
end--;
|
|
215
|
+
return s.slice(0, end);
|
|
216
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,71 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@osdlc/frontmatter-lossless",
|
|
3
|
-
"version": "0.0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "0.0.1-stage",
|
|
4
|
+
"description": "Byte-exact Markdown frontmatter framing: split a note into verbatim YAML and body with source offsets, and put it back together byte for byte. BOM, CRLF and fence bytes are kept. No dependencies.",
|
|
5
|
+
"license": "MIT OR Apache-2.0",
|
|
6
|
+
"author": "sksizer",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "git+https://github.com/osdlc/osdlc.git",
|
|
10
|
+
"directory": "packages/ts/frontmatter-lossless"
|
|
11
|
+
},
|
|
12
|
+
"homepage": "https://github.com/osdlc/osdlc/tree/main/packages/ts/frontmatter-lossless#readme",
|
|
13
|
+
"bugs": "https://github.com/osdlc/osdlc/issues",
|
|
14
|
+
"keywords": [
|
|
15
|
+
"markdown",
|
|
16
|
+
"frontmatter",
|
|
17
|
+
"front-matter",
|
|
18
|
+
"yaml",
|
|
19
|
+
"split",
|
|
20
|
+
"lossless",
|
|
21
|
+
"round-trip",
|
|
22
|
+
"byte-identical",
|
|
23
|
+
"crlf",
|
|
24
|
+
"bom",
|
|
25
|
+
"typescript"
|
|
26
|
+
],
|
|
27
|
+
"publishConfig": {
|
|
28
|
+
"access": "public"
|
|
29
|
+
},
|
|
30
|
+
"type": "module",
|
|
31
|
+
"sideEffects": false,
|
|
32
|
+
"engines": {
|
|
33
|
+
"node": ">=22"
|
|
34
|
+
},
|
|
35
|
+
"exports": {
|
|
36
|
+
".": {
|
|
37
|
+
"types": "./dist/index.d.ts",
|
|
38
|
+
"default": "./dist/index.js"
|
|
39
|
+
}
|
|
40
|
+
},
|
|
41
|
+
"files": [
|
|
42
|
+
"dist/",
|
|
43
|
+
"README.md",
|
|
44
|
+
"CHANGELOG.md",
|
|
45
|
+
"LICENSE-MIT",
|
|
46
|
+
"LICENSE-APACHE",
|
|
47
|
+
"SECURITY.md",
|
|
48
|
+
"CONTRIBUTING.md"
|
|
49
|
+
],
|
|
50
|
+
"devDependencies": {
|
|
51
|
+
"@types/bun": "^1.1.14",
|
|
52
|
+
"@types/node": "^26.1.0",
|
|
53
|
+
"oxlint": "^1.65.0",
|
|
54
|
+
"prettier": "3.9.6",
|
|
55
|
+
"typescript": "~6.0.3",
|
|
56
|
+
"@osdlc/prettier-config": "^0.2.3",
|
|
57
|
+
"@osdlc/tsconfig": "^0.3.3"
|
|
58
|
+
},
|
|
59
|
+
"scripts": {
|
|
60
|
+
"build": "tsc -p tsconfig.build.json",
|
|
61
|
+
"clean": "node -e \"require('node:fs').rmSync('dist', { recursive: true, force: true })\"",
|
|
62
|
+
"test": "bun test && npm run test:node",
|
|
63
|
+
"test:node": "pnpm run build && node --test tests/corpus-node.mjs",
|
|
64
|
+
"coverage": "bun test --coverage",
|
|
65
|
+
"smoke": "node tests/smoke.mjs",
|
|
66
|
+
"format": "prettier --write .",
|
|
67
|
+
"format:check": "prettier --check .",
|
|
68
|
+
"lint": "oxlint .",
|
|
69
|
+
"typecheck": "tsc --noEmit && tsc -p tsconfig.build.json --noEmit"
|
|
70
|
+
}
|
|
6
71
|
}
|