@neurowire/core 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/CHANGELOG.md +5 -0
- package/LICENSE +201 -0
- package/README.md +47 -0
- package/dist/index.d.ts +388 -0
- package/dist/index.js +594 -0
- package/package.json +58 -0
package/CHANGELOG.md
ADDED
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or Derivative
|
|
95
|
+
Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright 2026 Médéric Burlet
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
# @neurowire/core
|
|
2
|
+
|
|
3
|
+
The format authority for [Neurowire](https://github.com/starside-io/neurowire): the canonical feed model, the serializers, and the merge logic. Pure TypeScript, no network and no DOM, with `zod` as the only runtime dependency.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @neurowire/core
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## What it gives you
|
|
12
|
+
|
|
13
|
+
- **Canonical model** (`NeurowireFeed`, `NeurowireEntry`, `Mesh`) plus the zod schemas (`NeurowireFeedSchema`, `MeshSchema`, ...). Every parser produces this model and every serializer consumes it. It carries list metadata only, not full article bodies.
|
|
14
|
+
- **Serializers** for four output formats, dispatched by `serialize(feed, format)`:
|
|
15
|
+
- `atom`: Atom 1.0 (`application/atom+xml`)
|
|
16
|
+
- `json`: JSON Feed 1.1 (`application/feed+json`)
|
|
17
|
+
- `md`: Markdown digest
|
|
18
|
+
- `nwf`: Neurowire Feed, a compact line-oriented format (interned authors, tags and sources, relative links, delta timestamps)
|
|
19
|
+
- **nwf round-trip and validation**: `fromNwf` parses it back to the model, and `validateNwf` returns line-numbered diagnostics.
|
|
20
|
+
- **Merge**: `mergeFeeds` combines many feeds into one, tagged by source, deduped, newest first.
|
|
21
|
+
|
|
22
|
+
HTML is deliberately not a core format. It lives in [`@neurowire/web`](https://www.npmjs.com/package/@neurowire/web) so core stays format-pure and dependency-light.
|
|
23
|
+
|
|
24
|
+
## Usage
|
|
25
|
+
|
|
26
|
+
```ts
|
|
27
|
+
import { serialize, mergeFeeds, validateNwf, type NeurowireFeed } from '@neurowire/core'
|
|
28
|
+
|
|
29
|
+
const feed: NeurowireFeed = {
|
|
30
|
+
id: 'https://example.com/',
|
|
31
|
+
title: 'Example',
|
|
32
|
+
updated: new Date().toISOString(),
|
|
33
|
+
entries: [{ id: '1', title: 'Hello', link: 'https://example.com/hello' }],
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const atom = serialize(feed, 'atom')
|
|
37
|
+
const nwf = serialize(feed, 'nwf')
|
|
38
|
+
|
|
39
|
+
const result = validateNwf(nwf)
|
|
40
|
+
if (!result.valid) console.error(result.errors)
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
`FORMATS`, `MEDIA_TYPES`, and `EXTENSIONS` enumerate the supported formats and their content types and file extensions.
|
|
44
|
+
|
|
45
|
+
## License
|
|
46
|
+
|
|
47
|
+
Apache-2.0
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,388 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
|
|
3
|
+
/** A person referenced by a feed or entry (author). */
|
|
4
|
+
declare const PersonSchema: z.ZodObject<{
|
|
5
|
+
name: z.ZodString;
|
|
6
|
+
url: z.ZodOptional<z.ZodString>;
|
|
7
|
+
email: z.ZodOptional<z.ZodString>;
|
|
8
|
+
}, "strip", z.ZodTypeAny, {
|
|
9
|
+
name: string;
|
|
10
|
+
url?: string | undefined;
|
|
11
|
+
email?: string | undefined;
|
|
12
|
+
}, {
|
|
13
|
+
name: string;
|
|
14
|
+
url?: string | undefined;
|
|
15
|
+
email?: string | undefined;
|
|
16
|
+
}>;
|
|
17
|
+
type Person = z.infer<typeof PersonSchema>;
|
|
18
|
+
/** A single article in a feed. List-metadata only: no full article body. */
|
|
19
|
+
declare const EntrySchema: z.ZodObject<{
|
|
20
|
+
id: z.ZodString;
|
|
21
|
+
title: z.ZodString;
|
|
22
|
+
link: z.ZodString;
|
|
23
|
+
published: z.ZodOptional<z.ZodString>;
|
|
24
|
+
updated: z.ZodOptional<z.ZodString>;
|
|
25
|
+
summary: z.ZodOptional<z.ZodString>;
|
|
26
|
+
authors: z.ZodOptional<z.ZodArray<z.ZodObject<{
|
|
27
|
+
name: z.ZodString;
|
|
28
|
+
url: z.ZodOptional<z.ZodString>;
|
|
29
|
+
email: z.ZodOptional<z.ZodString>;
|
|
30
|
+
}, "strip", z.ZodTypeAny, {
|
|
31
|
+
name: string;
|
|
32
|
+
url?: string | undefined;
|
|
33
|
+
email?: string | undefined;
|
|
34
|
+
}, {
|
|
35
|
+
name: string;
|
|
36
|
+
url?: string | undefined;
|
|
37
|
+
email?: string | undefined;
|
|
38
|
+
}>, "many">>;
|
|
39
|
+
tags: z.ZodOptional<z.ZodArray<z.ZodString, "many">>;
|
|
40
|
+
source: z.ZodOptional<z.ZodObject<{
|
|
41
|
+
name: z.ZodOptional<z.ZodString>;
|
|
42
|
+
url: z.ZodOptional<z.ZodString>;
|
|
43
|
+
}, "strip", z.ZodTypeAny, {
|
|
44
|
+
name?: string | undefined;
|
|
45
|
+
url?: string | undefined;
|
|
46
|
+
}, {
|
|
47
|
+
name?: string | undefined;
|
|
48
|
+
url?: string | undefined;
|
|
49
|
+
}>>;
|
|
50
|
+
}, "strip", z.ZodTypeAny, {
|
|
51
|
+
id: string;
|
|
52
|
+
title: string;
|
|
53
|
+
link: string;
|
|
54
|
+
published?: string | undefined;
|
|
55
|
+
updated?: string | undefined;
|
|
56
|
+
summary?: string | undefined;
|
|
57
|
+
authors?: {
|
|
58
|
+
name: string;
|
|
59
|
+
url?: string | undefined;
|
|
60
|
+
email?: string | undefined;
|
|
61
|
+
}[] | undefined;
|
|
62
|
+
tags?: string[] | undefined;
|
|
63
|
+
source?: {
|
|
64
|
+
name?: string | undefined;
|
|
65
|
+
url?: string | undefined;
|
|
66
|
+
} | undefined;
|
|
67
|
+
}, {
|
|
68
|
+
id: string;
|
|
69
|
+
title: string;
|
|
70
|
+
link: string;
|
|
71
|
+
published?: string | undefined;
|
|
72
|
+
updated?: string | undefined;
|
|
73
|
+
summary?: string | undefined;
|
|
74
|
+
authors?: {
|
|
75
|
+
name: string;
|
|
76
|
+
url?: string | undefined;
|
|
77
|
+
email?: string | undefined;
|
|
78
|
+
}[] | undefined;
|
|
79
|
+
tags?: string[] | undefined;
|
|
80
|
+
source?: {
|
|
81
|
+
name?: string | undefined;
|
|
82
|
+
url?: string | undefined;
|
|
83
|
+
} | undefined;
|
|
84
|
+
}>;
|
|
85
|
+
type NeurowireEntry = z.infer<typeof EntrySchema>;
|
|
86
|
+
/** The canonical Neurowire feed. Every serializer reads this; every parser produces it. */
|
|
87
|
+
declare const FeedSchema: z.ZodObject<{
|
|
88
|
+
id: z.ZodString;
|
|
89
|
+
title: z.ZodString;
|
|
90
|
+
home: z.ZodOptional<z.ZodString>;
|
|
91
|
+
self: z.ZodOptional<z.ZodString>;
|
|
92
|
+
updated: z.ZodString;
|
|
93
|
+
authors: z.ZodOptional<z.ZodArray<z.ZodObject<{
|
|
94
|
+
name: z.ZodString;
|
|
95
|
+
url: z.ZodOptional<z.ZodString>;
|
|
96
|
+
email: z.ZodOptional<z.ZodString>;
|
|
97
|
+
}, "strip", z.ZodTypeAny, {
|
|
98
|
+
name: string;
|
|
99
|
+
url?: string | undefined;
|
|
100
|
+
email?: string | undefined;
|
|
101
|
+
}, {
|
|
102
|
+
name: string;
|
|
103
|
+
url?: string | undefined;
|
|
104
|
+
email?: string | undefined;
|
|
105
|
+
}>, "many">>;
|
|
106
|
+
generator: z.ZodOptional<z.ZodObject<{
|
|
107
|
+
name: z.ZodString;
|
|
108
|
+
version: z.ZodOptional<z.ZodString>;
|
|
109
|
+
}, "strip", z.ZodTypeAny, {
|
|
110
|
+
name: string;
|
|
111
|
+
version?: string | undefined;
|
|
112
|
+
}, {
|
|
113
|
+
name: string;
|
|
114
|
+
version?: string | undefined;
|
|
115
|
+
}>>;
|
|
116
|
+
entries: z.ZodArray<z.ZodObject<{
|
|
117
|
+
id: z.ZodString;
|
|
118
|
+
title: z.ZodString;
|
|
119
|
+
link: z.ZodString;
|
|
120
|
+
published: z.ZodOptional<z.ZodString>;
|
|
121
|
+
updated: z.ZodOptional<z.ZodString>;
|
|
122
|
+
summary: z.ZodOptional<z.ZodString>;
|
|
123
|
+
authors: z.ZodOptional<z.ZodArray<z.ZodObject<{
|
|
124
|
+
name: z.ZodString;
|
|
125
|
+
url: z.ZodOptional<z.ZodString>;
|
|
126
|
+
email: z.ZodOptional<z.ZodString>;
|
|
127
|
+
}, "strip", z.ZodTypeAny, {
|
|
128
|
+
name: string;
|
|
129
|
+
url?: string | undefined;
|
|
130
|
+
email?: string | undefined;
|
|
131
|
+
}, {
|
|
132
|
+
name: string;
|
|
133
|
+
url?: string | undefined;
|
|
134
|
+
email?: string | undefined;
|
|
135
|
+
}>, "many">>;
|
|
136
|
+
tags: z.ZodOptional<z.ZodArray<z.ZodString, "many">>;
|
|
137
|
+
source: z.ZodOptional<z.ZodObject<{
|
|
138
|
+
name: z.ZodOptional<z.ZodString>;
|
|
139
|
+
url: z.ZodOptional<z.ZodString>;
|
|
140
|
+
}, "strip", z.ZodTypeAny, {
|
|
141
|
+
name?: string | undefined;
|
|
142
|
+
url?: string | undefined;
|
|
143
|
+
}, {
|
|
144
|
+
name?: string | undefined;
|
|
145
|
+
url?: string | undefined;
|
|
146
|
+
}>>;
|
|
147
|
+
}, "strip", z.ZodTypeAny, {
|
|
148
|
+
id: string;
|
|
149
|
+
title: string;
|
|
150
|
+
link: string;
|
|
151
|
+
published?: string | undefined;
|
|
152
|
+
updated?: string | undefined;
|
|
153
|
+
summary?: string | undefined;
|
|
154
|
+
authors?: {
|
|
155
|
+
name: string;
|
|
156
|
+
url?: string | undefined;
|
|
157
|
+
email?: string | undefined;
|
|
158
|
+
}[] | undefined;
|
|
159
|
+
tags?: string[] | undefined;
|
|
160
|
+
source?: {
|
|
161
|
+
name?: string | undefined;
|
|
162
|
+
url?: string | undefined;
|
|
163
|
+
} | undefined;
|
|
164
|
+
}, {
|
|
165
|
+
id: string;
|
|
166
|
+
title: string;
|
|
167
|
+
link: string;
|
|
168
|
+
published?: string | undefined;
|
|
169
|
+
updated?: string | undefined;
|
|
170
|
+
summary?: string | undefined;
|
|
171
|
+
authors?: {
|
|
172
|
+
name: string;
|
|
173
|
+
url?: string | undefined;
|
|
174
|
+
email?: string | undefined;
|
|
175
|
+
}[] | undefined;
|
|
176
|
+
tags?: string[] | undefined;
|
|
177
|
+
source?: {
|
|
178
|
+
name?: string | undefined;
|
|
179
|
+
url?: string | undefined;
|
|
180
|
+
} | undefined;
|
|
181
|
+
}>, "many">;
|
|
182
|
+
}, "strip", z.ZodTypeAny, {
|
|
183
|
+
entries: {
|
|
184
|
+
id: string;
|
|
185
|
+
title: string;
|
|
186
|
+
link: string;
|
|
187
|
+
published?: string | undefined;
|
|
188
|
+
updated?: string | undefined;
|
|
189
|
+
summary?: string | undefined;
|
|
190
|
+
authors?: {
|
|
191
|
+
name: string;
|
|
192
|
+
url?: string | undefined;
|
|
193
|
+
email?: string | undefined;
|
|
194
|
+
}[] | undefined;
|
|
195
|
+
tags?: string[] | undefined;
|
|
196
|
+
source?: {
|
|
197
|
+
name?: string | undefined;
|
|
198
|
+
url?: string | undefined;
|
|
199
|
+
} | undefined;
|
|
200
|
+
}[];
|
|
201
|
+
id: string;
|
|
202
|
+
title: string;
|
|
203
|
+
updated: string;
|
|
204
|
+
authors?: {
|
|
205
|
+
name: string;
|
|
206
|
+
url?: string | undefined;
|
|
207
|
+
email?: string | undefined;
|
|
208
|
+
}[] | undefined;
|
|
209
|
+
home?: string | undefined;
|
|
210
|
+
self?: string | undefined;
|
|
211
|
+
generator?: {
|
|
212
|
+
name: string;
|
|
213
|
+
version?: string | undefined;
|
|
214
|
+
} | undefined;
|
|
215
|
+
}, {
|
|
216
|
+
entries: {
|
|
217
|
+
id: string;
|
|
218
|
+
title: string;
|
|
219
|
+
link: string;
|
|
220
|
+
published?: string | undefined;
|
|
221
|
+
updated?: string | undefined;
|
|
222
|
+
summary?: string | undefined;
|
|
223
|
+
authors?: {
|
|
224
|
+
name: string;
|
|
225
|
+
url?: string | undefined;
|
|
226
|
+
email?: string | undefined;
|
|
227
|
+
}[] | undefined;
|
|
228
|
+
tags?: string[] | undefined;
|
|
229
|
+
source?: {
|
|
230
|
+
name?: string | undefined;
|
|
231
|
+
url?: string | undefined;
|
|
232
|
+
} | undefined;
|
|
233
|
+
}[];
|
|
234
|
+
id: string;
|
|
235
|
+
title: string;
|
|
236
|
+
updated: string;
|
|
237
|
+
authors?: {
|
|
238
|
+
name: string;
|
|
239
|
+
url?: string | undefined;
|
|
240
|
+
email?: string | undefined;
|
|
241
|
+
}[] | undefined;
|
|
242
|
+
home?: string | undefined;
|
|
243
|
+
self?: string | undefined;
|
|
244
|
+
generator?: {
|
|
245
|
+
name: string;
|
|
246
|
+
version?: string | undefined;
|
|
247
|
+
} | undefined;
|
|
248
|
+
}>;
|
|
249
|
+
type NeurowireFeed = z.infer<typeof FeedSchema>;
|
|
250
|
+
/** Validate an unknown value into a NeurowireFeed. Throws (ZodError) on invalid input. */
|
|
251
|
+
declare function parseNeurowireFeed(data: unknown): NeurowireFeed;
|
|
252
|
+
/** Generator stamp written into feeds produced by Neurowire. */
|
|
253
|
+
declare const GENERATOR: {
|
|
254
|
+
readonly name: "Neurowire";
|
|
255
|
+
readonly version: "0.1.0";
|
|
256
|
+
};
|
|
257
|
+
/** A single source within a {@link Mesh}: a display name and a URL (feed or website). */
|
|
258
|
+
declare const MeshSourceSchema: z.ZodObject<{
|
|
259
|
+
name: z.ZodString;
|
|
260
|
+
url: z.ZodString;
|
|
261
|
+
}, "strip", z.ZodTypeAny, {
|
|
262
|
+
name: string;
|
|
263
|
+
url: string;
|
|
264
|
+
}, {
|
|
265
|
+
name: string;
|
|
266
|
+
url: string;
|
|
267
|
+
}>;
|
|
268
|
+
type MeshSource = z.infer<typeof MeshSourceSchema>;
|
|
269
|
+
/** A named bundle of sources that fetch and merge into one feed (e.g. "AI News"). */
|
|
270
|
+
declare const MeshSchema: z.ZodObject<{
|
|
271
|
+
name: z.ZodString;
|
|
272
|
+
sources: z.ZodArray<z.ZodObject<{
|
|
273
|
+
name: z.ZodString;
|
|
274
|
+
url: z.ZodString;
|
|
275
|
+
}, "strip", z.ZodTypeAny, {
|
|
276
|
+
name: string;
|
|
277
|
+
url: string;
|
|
278
|
+
}, {
|
|
279
|
+
name: string;
|
|
280
|
+
url: string;
|
|
281
|
+
}>, "many">;
|
|
282
|
+
}, "strip", z.ZodTypeAny, {
|
|
283
|
+
name: string;
|
|
284
|
+
sources: {
|
|
285
|
+
name: string;
|
|
286
|
+
url: string;
|
|
287
|
+
}[];
|
|
288
|
+
}, {
|
|
289
|
+
name: string;
|
|
290
|
+
sources: {
|
|
291
|
+
name: string;
|
|
292
|
+
url: string;
|
|
293
|
+
}[];
|
|
294
|
+
}>;
|
|
295
|
+
type Mesh = z.infer<typeof MeshSchema>;
|
|
296
|
+
/** Validate an unknown value into a Mesh. Throws (ZodError) on invalid input. */
|
|
297
|
+
declare function parseMesh(data: unknown): Mesh;
|
|
298
|
+
|
|
299
|
+
/** One feed plus the source label its entries should carry in a merge. */
|
|
300
|
+
interface MergePart {
|
|
301
|
+
feed: NeurowireFeed;
|
|
302
|
+
source?: {
|
|
303
|
+
name?: string;
|
|
304
|
+
url?: string;
|
|
305
|
+
};
|
|
306
|
+
}
|
|
307
|
+
interface MergeOptions {
|
|
308
|
+
/** Stable id for the merged feed. Defaults to a urn derived from the title. */
|
|
309
|
+
id?: string;
|
|
310
|
+
/** Keep only the newest N entries. */
|
|
311
|
+
limit?: number;
|
|
312
|
+
}
|
|
313
|
+
/**
|
|
314
|
+
* Merge several feeds into one named feed. Each entry is tagged with its source
|
|
315
|
+
* (falling back to the source feed's own title/home), entries are de-duplicated
|
|
316
|
+
* by link, sorted newest first, and capped by an optional `limit`.
|
|
317
|
+
*/
|
|
318
|
+
declare function mergeFeeds(title: string, parts: MergePart[], options?: MergeOptions): NeurowireFeed;
|
|
319
|
+
|
|
320
|
+
/** Serialize a feed to an Atom 1.0 document. */
|
|
321
|
+
declare function toAtom(feed: NeurowireFeed): string;
|
|
322
|
+
|
|
323
|
+
interface JsonFeedAuthor {
|
|
324
|
+
name: string;
|
|
325
|
+
url?: string;
|
|
326
|
+
}
|
|
327
|
+
interface JsonFeedItem {
|
|
328
|
+
id: string;
|
|
329
|
+
url: string;
|
|
330
|
+
title: string;
|
|
331
|
+
summary?: string;
|
|
332
|
+
date_published?: string;
|
|
333
|
+
date_modified?: string;
|
|
334
|
+
authors?: JsonFeedAuthor[];
|
|
335
|
+
tags?: string[];
|
|
336
|
+
}
|
|
337
|
+
interface JsonFeedDocument {
|
|
338
|
+
version: string;
|
|
339
|
+
title: string;
|
|
340
|
+
home_page_url?: string;
|
|
341
|
+
feed_url?: string;
|
|
342
|
+
authors?: JsonFeedAuthor[];
|
|
343
|
+
items: JsonFeedItem[];
|
|
344
|
+
}
|
|
345
|
+
/** Build a JSON Feed 1.1 document object from a feed. */
|
|
346
|
+
declare function toJsonFeedObject(feed: NeurowireFeed): JsonFeedDocument;
|
|
347
|
+
/** Serialize a feed to a JSON Feed 1.1 string. */
|
|
348
|
+
declare function toJsonFeed(feed: NeurowireFeed): string;
|
|
349
|
+
|
|
350
|
+
/** Serialize a feed to a human-readable Markdown digest. */
|
|
351
|
+
declare function toMarkdown(feed: NeurowireFeed): string;
|
|
352
|
+
|
|
353
|
+
/** Serialize a feed to the compact Neurowire Feed format. */
|
|
354
|
+
declare function toNwf(feed: NeurowireFeed): string;
|
|
355
|
+
/** Parse a compact Neurowire Feed back into the canonical model. */
|
|
356
|
+
declare function fromNwf(text: string): NeurowireFeed;
|
|
357
|
+
/** A problem found while validating a Neurowire Feed, with a 1-based line number. */
|
|
358
|
+
interface NwfIssue {
|
|
359
|
+
line: number;
|
|
360
|
+
message: string;
|
|
361
|
+
}
|
|
362
|
+
/** Result of validating an nwf document. */
|
|
363
|
+
interface NwfValidation {
|
|
364
|
+
valid: boolean;
|
|
365
|
+
errors: NwfIssue[];
|
|
366
|
+
warnings: NwfIssue[];
|
|
367
|
+
/** The parsed feed, present when there are no errors. */
|
|
368
|
+
feed?: NeurowireFeed;
|
|
369
|
+
}
|
|
370
|
+
/**
|
|
371
|
+
* Validate a Neurowire Feed document: the header, the feed line, line kinds,
|
|
372
|
+
* cell counts, timestamp deltas, and that every dictionary reference is in
|
|
373
|
+
* range. Then parse it. Returns errors and warnings with 1-based line numbers.
|
|
374
|
+
*/
|
|
375
|
+
declare function validateNwf(text: string): NwfValidation;
|
|
376
|
+
|
|
377
|
+
/** Supported output formats. */
|
|
378
|
+
declare const FORMATS: readonly ["atom", "json", "md", "nwf"];
|
|
379
|
+
type Format = (typeof FORMATS)[number];
|
|
380
|
+
/** Content-Type for each format. */
|
|
381
|
+
declare const MEDIA_TYPES: Record<Format, string>;
|
|
382
|
+
/** File extension for each format. */
|
|
383
|
+
declare const EXTENSIONS: Record<Format, string>;
|
|
384
|
+
declare function isFormat(value: string): value is Format;
|
|
385
|
+
/** Serialize a feed to the requested format. */
|
|
386
|
+
declare function serialize(feed: NeurowireFeed, format: Format): string;
|
|
387
|
+
|
|
388
|
+
export { EXTENSIONS, EntrySchema, FORMATS, FeedSchema, type Format, GENERATOR, type JsonFeedDocument, MEDIA_TYPES, type MergeOptions, type MergePart, type Mesh, MeshSchema, type MeshSource, MeshSourceSchema, type NeurowireEntry, type NeurowireFeed, type NwfIssue, type NwfValidation, type Person, PersonSchema, fromNwf, isFormat, mergeFeeds, parseMesh, parseNeurowireFeed, serialize, toAtom, toJsonFeed, toJsonFeedObject, toMarkdown, toNwf, validateNwf };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,594 @@
|
|
|
1
|
+
// src/model.ts
|
|
2
|
+
import { z } from "zod";
|
|
3
|
+
var PersonSchema = z.object({
|
|
4
|
+
name: z.string(),
|
|
5
|
+
url: z.string().optional(),
|
|
6
|
+
email: z.string().optional()
|
|
7
|
+
});
|
|
8
|
+
var EntrySchema = z.object({
|
|
9
|
+
id: z.string(),
|
|
10
|
+
title: z.string(),
|
|
11
|
+
link: z.string(),
|
|
12
|
+
published: z.string().optional(),
|
|
13
|
+
updated: z.string().optional(),
|
|
14
|
+
summary: z.string().optional(),
|
|
15
|
+
authors: z.array(PersonSchema).optional(),
|
|
16
|
+
tags: z.array(z.string()).optional(),
|
|
17
|
+
source: z.object({
|
|
18
|
+
name: z.string().optional(),
|
|
19
|
+
url: z.string().optional()
|
|
20
|
+
}).optional()
|
|
21
|
+
});
|
|
22
|
+
var FeedSchema = z.object({
|
|
23
|
+
id: z.string(),
|
|
24
|
+
title: z.string(),
|
|
25
|
+
home: z.string().optional(),
|
|
26
|
+
self: z.string().optional(),
|
|
27
|
+
updated: z.string(),
|
|
28
|
+
authors: z.array(PersonSchema).optional(),
|
|
29
|
+
generator: z.object({
|
|
30
|
+
name: z.string(),
|
|
31
|
+
version: z.string().optional()
|
|
32
|
+
}).optional(),
|
|
33
|
+
entries: z.array(EntrySchema)
|
|
34
|
+
});
|
|
35
|
+
function parseNeurowireFeed(data) {
|
|
36
|
+
return FeedSchema.parse(data);
|
|
37
|
+
}
|
|
38
|
+
var GENERATOR = { name: "Neurowire", version: "0.1.0" };
|
|
39
|
+
var MeshSourceSchema = z.object({
|
|
40
|
+
name: z.string(),
|
|
41
|
+
url: z.string()
|
|
42
|
+
});
|
|
43
|
+
var MeshSchema = z.object({
|
|
44
|
+
name: z.string(),
|
|
45
|
+
sources: z.array(MeshSourceSchema)
|
|
46
|
+
});
|
|
47
|
+
function parseMesh(data) {
|
|
48
|
+
return MeshSchema.parse(data);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// src/merge.ts
|
|
52
|
+
function entryTime(entry) {
|
|
53
|
+
const value = entry.updated ?? entry.published;
|
|
54
|
+
const ms = value ? Date.parse(value) : Number.NaN;
|
|
55
|
+
return Number.isNaN(ms) ? 0 : ms;
|
|
56
|
+
}
|
|
57
|
+
function mergeFeeds(title, parts, options = {}) {
|
|
58
|
+
const seen = /* @__PURE__ */ new Set();
|
|
59
|
+
const entries = [];
|
|
60
|
+
for (const part of parts) {
|
|
61
|
+
const source = {};
|
|
62
|
+
const name = part.source?.name ?? part.feed.title;
|
|
63
|
+
const url = part.source?.url ?? part.feed.home;
|
|
64
|
+
if (name) source.name = name;
|
|
65
|
+
if (url) source.url = url;
|
|
66
|
+
for (const entry of part.feed.entries) {
|
|
67
|
+
const key = entry.link || entry.id;
|
|
68
|
+
if (seen.has(key)) continue;
|
|
69
|
+
seen.add(key);
|
|
70
|
+
entries.push({ ...entry, source: { ...source } });
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
entries.sort((a, b) => entryTime(b) - entryTime(a));
|
|
74
|
+
const limited = options.limit ? entries.slice(0, options.limit) : entries;
|
|
75
|
+
const times = limited.map(entryTime).filter((time) => time > 0);
|
|
76
|
+
const updated = times.length ? new Date(Math.max(...times)).toISOString() : (/* @__PURE__ */ new Date()).toISOString();
|
|
77
|
+
return {
|
|
78
|
+
id: options.id ?? `urn:neurowire:mesh:${encodeURIComponent(title)}`,
|
|
79
|
+
title,
|
|
80
|
+
updated,
|
|
81
|
+
entries: limited,
|
|
82
|
+
generator: { name: GENERATOR.name, version: GENERATOR.version }
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
// src/serialize/atom.ts
|
|
87
|
+
var escapeText = (s) => s.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">");
|
|
88
|
+
var escapeAttr = (s) => escapeText(s).replace(/"/g, """);
|
|
89
|
+
function rfc3339(value, fallback) {
|
|
90
|
+
const source = value ?? fallback;
|
|
91
|
+
const ms = Date.parse(source);
|
|
92
|
+
return Number.isNaN(ms) ? fallback : new Date(ms).toISOString();
|
|
93
|
+
}
|
|
94
|
+
function personXml(tag, p, indent) {
|
|
95
|
+
const lines = [`${indent}<${tag}>`, `${indent} <name>${escapeText(p.name)}</name>`];
|
|
96
|
+
if (p.url) lines.push(`${indent} <uri>${escapeText(p.url)}</uri>`);
|
|
97
|
+
if (p.email) lines.push(`${indent} <email>${escapeText(p.email)}</email>`);
|
|
98
|
+
lines.push(`${indent}</${tag}>`);
|
|
99
|
+
return lines.join("\n");
|
|
100
|
+
}
|
|
101
|
+
function entryXml(entry, feedUpdated) {
|
|
102
|
+
const updated = rfc3339(entry.updated ?? entry.published, feedUpdated);
|
|
103
|
+
const lines = [
|
|
104
|
+
" <entry>",
|
|
105
|
+
` <id>${escapeText(entry.id)}</id>`,
|
|
106
|
+
` <title>${escapeText(entry.title)}</title>`,
|
|
107
|
+
` <link rel="alternate" href="${escapeAttr(entry.link)}"/>`,
|
|
108
|
+
` <updated>${updated}</updated>`
|
|
109
|
+
];
|
|
110
|
+
if (entry.published) {
|
|
111
|
+
lines.push(` <published>${rfc3339(entry.published, updated)}</published>`);
|
|
112
|
+
}
|
|
113
|
+
for (const author of entry.authors ?? []) {
|
|
114
|
+
lines.push(personXml("author", author, " "));
|
|
115
|
+
}
|
|
116
|
+
for (const tag of entry.tags ?? []) {
|
|
117
|
+
lines.push(` <category term="${escapeAttr(tag)}"/>`);
|
|
118
|
+
}
|
|
119
|
+
if (entry.summary) {
|
|
120
|
+
lines.push(` <summary type="text">${escapeText(entry.summary)}</summary>`);
|
|
121
|
+
}
|
|
122
|
+
lines.push(" </entry>");
|
|
123
|
+
return lines.join("\n");
|
|
124
|
+
}
|
|
125
|
+
function toAtom(feed) {
|
|
126
|
+
const updated = rfc3339(feed.updated, (/* @__PURE__ */ new Date(0)).toISOString());
|
|
127
|
+
const lines = [
|
|
128
|
+
'<?xml version="1.0" encoding="utf-8"?>',
|
|
129
|
+
'<feed xmlns="http://www.w3.org/2005/Atom">',
|
|
130
|
+
` <id>${escapeText(feed.id)}</id>`,
|
|
131
|
+
` <title>${escapeText(feed.title)}</title>`,
|
|
132
|
+
` <updated>${updated}</updated>`
|
|
133
|
+
];
|
|
134
|
+
if (feed.home) lines.push(` <link rel="alternate" href="${escapeAttr(feed.home)}"/>`);
|
|
135
|
+
if (feed.self) lines.push(` <link rel="self" href="${escapeAttr(feed.self)}"/>`);
|
|
136
|
+
for (const author of feed.authors ?? []) {
|
|
137
|
+
lines.push(personXml("author", author, " "));
|
|
138
|
+
}
|
|
139
|
+
if (feed.generator) {
|
|
140
|
+
const version = feed.generator.version ? ` version="${escapeAttr(feed.generator.version)}"` : "";
|
|
141
|
+
lines.push(` <generator${version}>${escapeText(feed.generator.name)}</generator>`);
|
|
142
|
+
}
|
|
143
|
+
for (const entry of feed.entries) {
|
|
144
|
+
lines.push(entryXml(entry, updated));
|
|
145
|
+
}
|
|
146
|
+
lines.push("</feed>", "");
|
|
147
|
+
return lines.join("\n");
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
// src/serialize/jsonfeed.ts
|
|
151
|
+
function rfc33392(value) {
|
|
152
|
+
if (!value) return void 0;
|
|
153
|
+
const ms = Date.parse(value);
|
|
154
|
+
return Number.isNaN(ms) ? void 0 : new Date(ms).toISOString();
|
|
155
|
+
}
|
|
156
|
+
var toAuthor = (p) => p.url ? { name: p.name, url: p.url } : { name: p.name };
|
|
157
|
+
function toItem(entry) {
|
|
158
|
+
const item = { id: entry.id, url: entry.link, title: entry.title };
|
|
159
|
+
if (entry.summary) item.summary = entry.summary;
|
|
160
|
+
const published = rfc33392(entry.published);
|
|
161
|
+
const modified = rfc33392(entry.updated);
|
|
162
|
+
if (published) item.date_published = published;
|
|
163
|
+
if (modified) item.date_modified = modified;
|
|
164
|
+
if (entry.authors?.length) item.authors = entry.authors.map(toAuthor);
|
|
165
|
+
if (entry.tags?.length) item.tags = entry.tags;
|
|
166
|
+
return item;
|
|
167
|
+
}
|
|
168
|
+
function toJsonFeedObject(feed) {
|
|
169
|
+
return {
|
|
170
|
+
version: "https://jsonfeed.org/version/1.1",
|
|
171
|
+
title: feed.title,
|
|
172
|
+
...feed.home ? { home_page_url: feed.home } : {},
|
|
173
|
+
...feed.self ? { feed_url: feed.self } : {},
|
|
174
|
+
...feed.authors?.length ? { authors: feed.authors.map(toAuthor) } : {},
|
|
175
|
+
items: feed.entries.map(toItem)
|
|
176
|
+
};
|
|
177
|
+
}
|
|
178
|
+
function toJsonFeed(feed) {
|
|
179
|
+
return `${JSON.stringify(toJsonFeedObject(feed), null, 2)}
|
|
180
|
+
`;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
// src/serialize/markdown.ts
|
|
184
|
+
var DOT = " \xB7 ";
|
|
185
|
+
function formatDate(value) {
|
|
186
|
+
if (!value) return void 0;
|
|
187
|
+
const ms = Date.parse(value);
|
|
188
|
+
return Number.isNaN(ms) ? value : new Date(ms).toISOString().slice(0, 10);
|
|
189
|
+
}
|
|
190
|
+
function entryMd(entry) {
|
|
191
|
+
const lines = [`### [${entry.title}](${entry.link})`];
|
|
192
|
+
const meta = [];
|
|
193
|
+
const date = formatDate(entry.updated ?? entry.published);
|
|
194
|
+
if (date) meta.push(date);
|
|
195
|
+
if (entry.authors?.length) meta.push(entry.authors.map((a) => a.name).join(", "));
|
|
196
|
+
if (entry.tags?.length) meta.push(entry.tags.map((t) => `\`${t}\``).join(" "));
|
|
197
|
+
if (meta.length) lines.push("", meta.join(DOT));
|
|
198
|
+
if (entry.summary) lines.push("", entry.summary);
|
|
199
|
+
return lines.join("\n");
|
|
200
|
+
}
|
|
201
|
+
function toMarkdown(feed) {
|
|
202
|
+
const header = [`# ${feed.title}`];
|
|
203
|
+
const meta = [];
|
|
204
|
+
const updated = formatDate(feed.updated);
|
|
205
|
+
if (updated) meta.push(`Updated ${updated}`);
|
|
206
|
+
if (feed.home) meta.push(`[Home](${feed.home})`);
|
|
207
|
+
if (feed.self) meta.push(`[Feed](${feed.self})`);
|
|
208
|
+
if (meta.length) header.push("", meta.join(DOT));
|
|
209
|
+
return `${[header.join("\n"), ...feed.entries.map(entryMd)].join("\n\n")}
|
|
210
|
+
`;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
// src/serialize/nwf.ts
|
|
214
|
+
var SEP = " ";
|
|
215
|
+
var UNIT = String.fromCharCode(31);
|
|
216
|
+
var enc = (s) => s.replace(/\\/g, "\\\\").replace(/\t/g, "\\t").replace(/\r/g, "\\r").replace(/\n/g, "\\n");
|
|
217
|
+
var dec = (s) => s.replace(
|
|
218
|
+
/\\(.)/g,
|
|
219
|
+
(_, c) => c === "t" ? " " : c === "r" ? "\r" : c === "n" ? "\n" : c
|
|
220
|
+
);
|
|
221
|
+
function toEpoch(iso) {
|
|
222
|
+
if (!iso) return void 0;
|
|
223
|
+
const ms = Date.parse(iso);
|
|
224
|
+
return Number.isNaN(ms) ? void 0 : Math.floor(ms / 1e3);
|
|
225
|
+
}
|
|
226
|
+
var fromEpoch = (sec) => new Date(sec * 1e3).toISOString();
|
|
227
|
+
function encPerson(p) {
|
|
228
|
+
const parts = [p.name];
|
|
229
|
+
if (p.url !== void 0 || p.email !== void 0) parts.push(p.url ?? "");
|
|
230
|
+
if (p.email !== void 0) parts.push(p.email ?? "");
|
|
231
|
+
return parts.map(enc).join(UNIT);
|
|
232
|
+
}
|
|
233
|
+
function decPerson(cell) {
|
|
234
|
+
const [name = "", url, email] = cell.split(UNIT).map(dec);
|
|
235
|
+
const person = { name };
|
|
236
|
+
if (url) person.url = url;
|
|
237
|
+
if (email) person.email = email;
|
|
238
|
+
return person;
|
|
239
|
+
}
|
|
240
|
+
function encSource(source) {
|
|
241
|
+
const name = enc(source.name ?? "");
|
|
242
|
+
return source.url ? `${name}${UNIT}${enc(source.url)}` : name;
|
|
243
|
+
}
|
|
244
|
+
function decSource(cell) {
|
|
245
|
+
const [name = "", url] = cell.split(UNIT).map(dec);
|
|
246
|
+
const source = {};
|
|
247
|
+
if (name) source.name = name;
|
|
248
|
+
if (url) source.url = url;
|
|
249
|
+
return source;
|
|
250
|
+
}
|
|
251
|
+
function hasSource(source) {
|
|
252
|
+
return source !== void 0 && (Boolean(source.name) || Boolean(source.url));
|
|
253
|
+
}
|
|
254
|
+
function parseRefs(cell) {
|
|
255
|
+
if (!cell) return [];
|
|
256
|
+
return cell.split(",").map((n) => Number.parseInt(n, 10));
|
|
257
|
+
}
|
|
258
|
+
function commonBase(links) {
|
|
259
|
+
if (links.length < 2) return "";
|
|
260
|
+
let prefix = links[0] ?? "";
|
|
261
|
+
for (let i = 1; i < links.length; i++) {
|
|
262
|
+
const link = links[i] ?? "";
|
|
263
|
+
let n = 0;
|
|
264
|
+
while (n < prefix.length && n < link.length && prefix[n] === link[n]) n++;
|
|
265
|
+
prefix = prefix.slice(0, n);
|
|
266
|
+
if (!prefix) break;
|
|
267
|
+
}
|
|
268
|
+
const cut = prefix.lastIndexOf("/");
|
|
269
|
+
return cut > 8 ? prefix.slice(0, cut + 1) : "";
|
|
270
|
+
}
|
|
271
|
+
function toNwf(feed) {
|
|
272
|
+
const updatedEpoch = toEpoch(feed.updated) ?? 0;
|
|
273
|
+
const authors = [];
|
|
274
|
+
const authorKeys = /* @__PURE__ */ new Map();
|
|
275
|
+
const internAuthor = (p) => {
|
|
276
|
+
const key = `${p.name}${UNIT}${p.url ?? ""}${UNIT}${p.email ?? ""}`;
|
|
277
|
+
const existing = authorKeys.get(key);
|
|
278
|
+
if (existing !== void 0) return existing;
|
|
279
|
+
const index = authors.length;
|
|
280
|
+
authors.push(p);
|
|
281
|
+
authorKeys.set(key, index);
|
|
282
|
+
return index;
|
|
283
|
+
};
|
|
284
|
+
const tags = [];
|
|
285
|
+
const tagKeys = /* @__PURE__ */ new Map();
|
|
286
|
+
const internTag = (t) => {
|
|
287
|
+
const existing = tagKeys.get(t);
|
|
288
|
+
if (existing !== void 0) return existing;
|
|
289
|
+
const index = tags.length;
|
|
290
|
+
tags.push(t);
|
|
291
|
+
tagKeys.set(t, index);
|
|
292
|
+
return index;
|
|
293
|
+
};
|
|
294
|
+
const sources = [];
|
|
295
|
+
const sourceKeys = /* @__PURE__ */ new Map();
|
|
296
|
+
const internSource = (source) => {
|
|
297
|
+
const key = `${source.name ?? ""}${UNIT}${source.url ?? ""}`;
|
|
298
|
+
const existing = sourceKeys.get(key);
|
|
299
|
+
if (existing !== void 0) return existing;
|
|
300
|
+
const index = sources.length;
|
|
301
|
+
sources.push(source);
|
|
302
|
+
sourceKeys.set(key, index);
|
|
303
|
+
return index;
|
|
304
|
+
};
|
|
305
|
+
const feedAuthorRefs = (feed.authors ?? []).map(internAuthor);
|
|
306
|
+
const rows = feed.entries.map((entry) => ({
|
|
307
|
+
entry,
|
|
308
|
+
authorRefs: (entry.authors ?? []).map(internAuthor),
|
|
309
|
+
tagRefs: (entry.tags ?? []).map(internTag),
|
|
310
|
+
sourceRef: hasSource(entry.source) ? String(internSource(entry.source)) : ""
|
|
311
|
+
}));
|
|
312
|
+
const base = commonBase(feed.entries.map((e) => e.link));
|
|
313
|
+
const lines = ["NWF1"];
|
|
314
|
+
lines.push(
|
|
315
|
+
[
|
|
316
|
+
"F",
|
|
317
|
+
enc(feed.id),
|
|
318
|
+
enc(feed.title),
|
|
319
|
+
enc(feed.home ?? ""),
|
|
320
|
+
enc(feed.self ?? ""),
|
|
321
|
+
String(updatedEpoch),
|
|
322
|
+
feedAuthorRefs.join(",")
|
|
323
|
+
].join(SEP)
|
|
324
|
+
);
|
|
325
|
+
if (authors.length) lines.push(["A", ...authors.map(encPerson)].join(SEP));
|
|
326
|
+
if (tags.length) lines.push(["T", ...tags.map(enc)].join(SEP));
|
|
327
|
+
if (sources.length) lines.push(["S", ...sources.map(encSource)].join(SEP));
|
|
328
|
+
if (base) lines.push(["B", enc(base)].join(SEP));
|
|
329
|
+
for (const { entry, authorRefs, tagRefs, sourceRef } of rows) {
|
|
330
|
+
const epoch = toEpoch(entry.updated ?? entry.published);
|
|
331
|
+
const delta = epoch === void 0 ? "-" : String(updatedEpoch - epoch);
|
|
332
|
+
const link = base && entry.link.startsWith(base) ? `~${entry.link.slice(base.length)}` : entry.link;
|
|
333
|
+
lines.push(
|
|
334
|
+
[
|
|
335
|
+
"E",
|
|
336
|
+
enc(entry.id),
|
|
337
|
+
delta,
|
|
338
|
+
enc(link),
|
|
339
|
+
authorRefs.join(","),
|
|
340
|
+
tagRefs.join(","),
|
|
341
|
+
enc(entry.title),
|
|
342
|
+
enc(entry.summary ?? ""),
|
|
343
|
+
sourceRef
|
|
344
|
+
].join(SEP)
|
|
345
|
+
);
|
|
346
|
+
}
|
|
347
|
+
return `${lines.join("\n")}
|
|
348
|
+
`;
|
|
349
|
+
}
|
|
350
|
+
function fromNwf(text) {
|
|
351
|
+
const lines = text.split("\n");
|
|
352
|
+
if (lines[0] !== "NWF1") {
|
|
353
|
+
throw new Error("Not a Neurowire Feed (missing NWF1 header)");
|
|
354
|
+
}
|
|
355
|
+
let id = "";
|
|
356
|
+
let title = "";
|
|
357
|
+
let home = "";
|
|
358
|
+
let self = "";
|
|
359
|
+
let updatedEpoch = 0;
|
|
360
|
+
let feedAuthorRefs = [];
|
|
361
|
+
let authors = [];
|
|
362
|
+
let tags = [];
|
|
363
|
+
let sources = [];
|
|
364
|
+
let base = "";
|
|
365
|
+
const entries = [];
|
|
366
|
+
for (let i = 1; i < lines.length; i++) {
|
|
367
|
+
const line = lines[i];
|
|
368
|
+
if (!line) continue;
|
|
369
|
+
const cells = line.split(SEP);
|
|
370
|
+
switch (cells[0]) {
|
|
371
|
+
case "F":
|
|
372
|
+
id = dec(cells[1] ?? "");
|
|
373
|
+
title = dec(cells[2] ?? "");
|
|
374
|
+
home = dec(cells[3] ?? "");
|
|
375
|
+
self = dec(cells[4] ?? "");
|
|
376
|
+
updatedEpoch = Number.parseInt(cells[5] ?? "0", 10) || 0;
|
|
377
|
+
feedAuthorRefs = parseRefs(cells[6]);
|
|
378
|
+
break;
|
|
379
|
+
case "A":
|
|
380
|
+
authors = cells.slice(1).map(decPerson);
|
|
381
|
+
break;
|
|
382
|
+
case "T":
|
|
383
|
+
tags = cells.slice(1).map(dec);
|
|
384
|
+
break;
|
|
385
|
+
case "S":
|
|
386
|
+
sources = cells.slice(1).map(decSource);
|
|
387
|
+
break;
|
|
388
|
+
case "B":
|
|
389
|
+
base = dec(cells[1] ?? "");
|
|
390
|
+
break;
|
|
391
|
+
case "E": {
|
|
392
|
+
const rawLink = dec(cells[3] ?? "");
|
|
393
|
+
const entry = {
|
|
394
|
+
id: dec(cells[1] ?? ""),
|
|
395
|
+
title: dec(cells[6] ?? ""),
|
|
396
|
+
link: rawLink.startsWith("~") ? base + rawLink.slice(1) : rawLink
|
|
397
|
+
};
|
|
398
|
+
const delta = cells[2] ?? "-";
|
|
399
|
+
if (delta !== "-") {
|
|
400
|
+
entry.updated = fromEpoch(updatedEpoch - (Number.parseInt(delta, 10) || 0));
|
|
401
|
+
}
|
|
402
|
+
const entryAuthors = parseRefs(cells[4]).map((r) => authors[r]).filter((a) => a !== void 0);
|
|
403
|
+
if (entryAuthors.length) entry.authors = entryAuthors;
|
|
404
|
+
const entryTags = parseRefs(cells[5]).map((r) => tags[r]).filter((t) => t !== void 0);
|
|
405
|
+
if (entryTags.length) entry.tags = entryTags;
|
|
406
|
+
const summary = dec(cells[7] ?? "");
|
|
407
|
+
if (summary) entry.summary = summary;
|
|
408
|
+
const sourceRef = cells[8] ?? "";
|
|
409
|
+
if (sourceRef !== "") {
|
|
410
|
+
const source = sources[Number.parseInt(sourceRef, 10)];
|
|
411
|
+
if (source) entry.source = source;
|
|
412
|
+
}
|
|
413
|
+
entries.push(entry);
|
|
414
|
+
break;
|
|
415
|
+
}
|
|
416
|
+
}
|
|
417
|
+
}
|
|
418
|
+
const feed = { id, title, updated: fromEpoch(updatedEpoch), entries };
|
|
419
|
+
if (home) feed.home = home;
|
|
420
|
+
if (self) feed.self = self;
|
|
421
|
+
const fAuthors = feedAuthorRefs.map((r) => authors[r]).filter((a) => a !== void 0);
|
|
422
|
+
if (fAuthors.length) feed.authors = fAuthors;
|
|
423
|
+
return feed;
|
|
424
|
+
}
|
|
425
|
+
var isUint = (s) => s !== void 0 && /^\d+$/.test(s);
|
|
426
|
+
function checkRefs(cell, dictSize, label, line, errors) {
|
|
427
|
+
if (!cell) return;
|
|
428
|
+
for (const part of cell.split(",")) {
|
|
429
|
+
if (!/^\d+$/.test(part)) {
|
|
430
|
+
errors.push({ line, message: `${label}: "${part}" is not an integer index` });
|
|
431
|
+
} else if (Number(part) >= dictSize) {
|
|
432
|
+
errors.push({
|
|
433
|
+
line,
|
|
434
|
+
message: `${label}: index ${part} is out of range (dictionary has ${dictSize})`
|
|
435
|
+
});
|
|
436
|
+
}
|
|
437
|
+
}
|
|
438
|
+
}
|
|
439
|
+
function validateNwf(text) {
|
|
440
|
+
const errors = [];
|
|
441
|
+
const warnings = [];
|
|
442
|
+
const lines = text.split("\n");
|
|
443
|
+
if (lines[0] !== "NWF1") {
|
|
444
|
+
errors.push({
|
|
445
|
+
line: 1,
|
|
446
|
+
message: `expected "NWF1" header, got ${JSON.stringify(lines[0] ?? "")}`
|
|
447
|
+
});
|
|
448
|
+
return { valid: false, errors, warnings };
|
|
449
|
+
}
|
|
450
|
+
let authorCount = 0;
|
|
451
|
+
let tagCount = 0;
|
|
452
|
+
let sourceCount = 0;
|
|
453
|
+
for (let i = 1; i < lines.length; i++) {
|
|
454
|
+
const cells = (lines[i] ?? "").split(SEP);
|
|
455
|
+
if (cells[0] === "A") authorCount = cells.length - 1;
|
|
456
|
+
else if (cells[0] === "T") tagCount = cells.length - 1;
|
|
457
|
+
else if (cells[0] === "S") sourceCount = cells.length - 1;
|
|
458
|
+
}
|
|
459
|
+
let feedLines = 0;
|
|
460
|
+
let entryCount = 0;
|
|
461
|
+
for (let i = 1; i < lines.length; i++) {
|
|
462
|
+
const line = lines[i];
|
|
463
|
+
if (!line) continue;
|
|
464
|
+
const lineNo = i + 1;
|
|
465
|
+
const cells = line.split(SEP);
|
|
466
|
+
switch (cells[0]) {
|
|
467
|
+
case "F": {
|
|
468
|
+
feedLines++;
|
|
469
|
+
if (cells.length < 6) {
|
|
470
|
+
errors.push({
|
|
471
|
+
line: lineNo,
|
|
472
|
+
message: `F line needs at least 6 cells, got ${cells.length}`
|
|
473
|
+
});
|
|
474
|
+
}
|
|
475
|
+
if (!isUint(cells[5])) {
|
|
476
|
+
errors.push({
|
|
477
|
+
line: lineNo,
|
|
478
|
+
message: `F updatedEpoch must be an integer, got ${JSON.stringify(cells[5] ?? "")}`
|
|
479
|
+
});
|
|
480
|
+
}
|
|
481
|
+
checkRefs(cells[6], authorCount, "F authorRefs", lineNo, errors);
|
|
482
|
+
break;
|
|
483
|
+
}
|
|
484
|
+
case "A":
|
|
485
|
+
case "T":
|
|
486
|
+
case "S":
|
|
487
|
+
break;
|
|
488
|
+
case "B":
|
|
489
|
+
if (!cells[1]) warnings.push({ line: lineNo, message: "B line has no base URL" });
|
|
490
|
+
break;
|
|
491
|
+
case "E": {
|
|
492
|
+
entryCount++;
|
|
493
|
+
if (cells.length < 7) {
|
|
494
|
+
errors.push({
|
|
495
|
+
line: lineNo,
|
|
496
|
+
message: `E line needs at least 7 cells, got ${cells.length}`
|
|
497
|
+
});
|
|
498
|
+
break;
|
|
499
|
+
}
|
|
500
|
+
if (!cells[1]) errors.push({ line: lineNo, message: "E entry has an empty id" });
|
|
501
|
+
const delta = cells[2] ?? "";
|
|
502
|
+
if (delta !== "-" && !/^-?\d+$/.test(delta)) {
|
|
503
|
+
errors.push({
|
|
504
|
+
line: lineNo,
|
|
505
|
+
message: `E delta must be "-" or an integer, got ${JSON.stringify(delta)}`
|
|
506
|
+
});
|
|
507
|
+
}
|
|
508
|
+
if (!cells[3]) warnings.push({ line: lineNo, message: "E entry has an empty link" });
|
|
509
|
+
checkRefs(cells[4], authorCount, "E authorRefs", lineNo, errors);
|
|
510
|
+
checkRefs(cells[5], tagCount, "E tagRefs", lineNo, errors);
|
|
511
|
+
const sourceRef = cells[8];
|
|
512
|
+
if (sourceRef) {
|
|
513
|
+
if (!/^\d+$/.test(sourceRef)) {
|
|
514
|
+
errors.push({
|
|
515
|
+
line: lineNo,
|
|
516
|
+
message: `E sourceRef must be an integer index, got ${JSON.stringify(sourceRef)}`
|
|
517
|
+
});
|
|
518
|
+
} else if (Number(sourceRef) >= sourceCount) {
|
|
519
|
+
errors.push({
|
|
520
|
+
line: lineNo,
|
|
521
|
+
message: `E sourceRef ${sourceRef} is out of range (S has ${sourceCount})`
|
|
522
|
+
});
|
|
523
|
+
}
|
|
524
|
+
}
|
|
525
|
+
break;
|
|
526
|
+
}
|
|
527
|
+
default:
|
|
528
|
+
errors.push({
|
|
529
|
+
line: lineNo,
|
|
530
|
+
message: `unknown line kind ${JSON.stringify(cells[0] ?? "")}`
|
|
531
|
+
});
|
|
532
|
+
}
|
|
533
|
+
}
|
|
534
|
+
if (feedLines === 0)
|
|
535
|
+
errors.push({ line: 1, message: "missing the required F (feed header) line" });
|
|
536
|
+
if (feedLines > 1)
|
|
537
|
+
errors.push({ line: 1, message: `expected exactly one F line, found ${feedLines}` });
|
|
538
|
+
if (entryCount === 0) warnings.push({ line: 1, message: "feed has no entries" });
|
|
539
|
+
const feed = errors.length === 0 ? fromNwf(text) : void 0;
|
|
540
|
+
return { valid: errors.length === 0, errors, warnings, feed };
|
|
541
|
+
}
|
|
542
|
+
|
|
543
|
+
// src/serialize/index.ts
|
|
544
|
+
var FORMATS = ["atom", "json", "md", "nwf"];
|
|
545
|
+
var MEDIA_TYPES = {
|
|
546
|
+
atom: "application/atom+xml; charset=utf-8",
|
|
547
|
+
json: "application/feed+json; charset=utf-8",
|
|
548
|
+
md: "text/markdown; charset=utf-8",
|
|
549
|
+
nwf: "text/x-neurowire; charset=utf-8"
|
|
550
|
+
};
|
|
551
|
+
var EXTENSIONS = {
|
|
552
|
+
atom: "xml",
|
|
553
|
+
json: "json",
|
|
554
|
+
md: "md",
|
|
555
|
+
nwf: "nwf"
|
|
556
|
+
};
|
|
557
|
+
function isFormat(value) {
|
|
558
|
+
return FORMATS.includes(value);
|
|
559
|
+
}
|
|
560
|
+
function serialize(feed, format) {
|
|
561
|
+
switch (format) {
|
|
562
|
+
case "atom":
|
|
563
|
+
return toAtom(feed);
|
|
564
|
+
case "json":
|
|
565
|
+
return toJsonFeed(feed);
|
|
566
|
+
case "md":
|
|
567
|
+
return toMarkdown(feed);
|
|
568
|
+
case "nwf":
|
|
569
|
+
return toNwf(feed);
|
|
570
|
+
}
|
|
571
|
+
}
|
|
572
|
+
export {
|
|
573
|
+
EXTENSIONS,
|
|
574
|
+
EntrySchema,
|
|
575
|
+
FORMATS,
|
|
576
|
+
FeedSchema,
|
|
577
|
+
GENERATOR,
|
|
578
|
+
MEDIA_TYPES,
|
|
579
|
+
MeshSchema,
|
|
580
|
+
MeshSourceSchema,
|
|
581
|
+
PersonSchema,
|
|
582
|
+
fromNwf,
|
|
583
|
+
isFormat,
|
|
584
|
+
mergeFeeds,
|
|
585
|
+
parseMesh,
|
|
586
|
+
parseNeurowireFeed,
|
|
587
|
+
serialize,
|
|
588
|
+
toAtom,
|
|
589
|
+
toJsonFeed,
|
|
590
|
+
toJsonFeedObject,
|
|
591
|
+
toMarkdown,
|
|
592
|
+
toNwf,
|
|
593
|
+
validateNwf
|
|
594
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@neurowire/core",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Canonical feed model and serializers (Atom, JSON Feed 1.1, Markdown, nwf) for Neurowire.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/index.js",
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"import": "./dist/index.js"
|
|
12
|
+
}
|
|
13
|
+
},
|
|
14
|
+
"files": [
|
|
15
|
+
"dist",
|
|
16
|
+
"CHANGELOG.md"
|
|
17
|
+
],
|
|
18
|
+
"keywords": [
|
|
19
|
+
"neurowire",
|
|
20
|
+
"feed",
|
|
21
|
+
"atom",
|
|
22
|
+
"json-feed",
|
|
23
|
+
"jsonfeed",
|
|
24
|
+
"rss",
|
|
25
|
+
"markdown",
|
|
26
|
+
"nwf",
|
|
27
|
+
"serializer"
|
|
28
|
+
],
|
|
29
|
+
"author": "Médéric Burlet <mederic.burlet@gmail.com>",
|
|
30
|
+
"license": "Apache-2.0",
|
|
31
|
+
"repository": {
|
|
32
|
+
"type": "git",
|
|
33
|
+
"url": "git+https://github.com/starside-io/neurowire.git",
|
|
34
|
+
"directory": "packages/core"
|
|
35
|
+
},
|
|
36
|
+
"homepage": "https://github.com/starside-io/neurowire#readme",
|
|
37
|
+
"bugs": {
|
|
38
|
+
"url": "https://github.com/starside-io/neurowire/issues"
|
|
39
|
+
},
|
|
40
|
+
"engines": {
|
|
41
|
+
"node": ">=20"
|
|
42
|
+
},
|
|
43
|
+
"publishConfig": {
|
|
44
|
+
"access": "public"
|
|
45
|
+
},
|
|
46
|
+
"dependencies": {
|
|
47
|
+
"zod": "^3.24.1"
|
|
48
|
+
},
|
|
49
|
+
"devDependencies": {
|
|
50
|
+
"tsup": "^8.3.5",
|
|
51
|
+
"typescript": "^5.7.3"
|
|
52
|
+
},
|
|
53
|
+
"scripts": {
|
|
54
|
+
"build": "tsup",
|
|
55
|
+
"dev": "tsup --watch",
|
|
56
|
+
"typecheck": "tsc --noEmit"
|
|
57
|
+
}
|
|
58
|
+
}
|