@mastrojs/feed 0.0.0-stage → 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +47 -2
- package/dist/feed.d.ts +58 -0
- package/dist/feed.d.ts.map +1 -0
- package/dist/feed.js +54 -0
- package/package.json +36 -3
- package/src/feed.ts +105 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Mastro
|
|
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,48 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @mastrojs/feed
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Generate feeds with [Mastro](https://mastrojs.github.io/).
|
|
4
|
+
|
|
5
|
+
Currently only Atom, but we might add RSS in the future if there's demand.
|
|
6
|
+
|
|
7
|
+
## Install
|
|
8
|
+
|
|
9
|
+
pnpm add @mastrojs/feed
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
## Usage
|
|
13
|
+
|
|
14
|
+
Put this e.g. in `routes/feed.xml.server.ts`:
|
|
15
|
+
|
|
16
|
+
```ts
|
|
17
|
+
import { atomResponse } from "@mastrojs/feed";
|
|
18
|
+
import { html } from "@mastrojs/mastro";
|
|
19
|
+
|
|
20
|
+
const baseUrl = "https://mywebsite.org/";
|
|
21
|
+
|
|
22
|
+
export const GET = () => {
|
|
23
|
+
// read feed entries here from db or markdown files
|
|
24
|
+
const entries = [{
|
|
25
|
+
title: "Test",
|
|
26
|
+
id: new URL("blog/test/", baseUrl),
|
|
27
|
+
updated: new Date("2025-01-01"),
|
|
28
|
+
content: html`Less: <em> < </em>`,
|
|
29
|
+
}];
|
|
30
|
+
|
|
31
|
+
return atomResponse({
|
|
32
|
+
title: "My Blog",
|
|
33
|
+
id: new URL(baseUrl),
|
|
34
|
+
linkSelf: new URL("feed.xml", baseUrl),
|
|
35
|
+
updated: new Date("2025-01-01"),
|
|
36
|
+
author: { name: "me" },
|
|
37
|
+
entries,
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
And don't forget to advertise your feed from your HTML page. For example:
|
|
43
|
+
|
|
44
|
+
```html
|
|
45
|
+
<link rel="alternate" type="application/atom+xml" href="/feed.xml">
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
To learn more, see the `@mastrojs/feed` [API docs](https://jsr.io/@mastrojs/feed/doc).
|
package/dist/feed.d.ts
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Module with helper function to create an Atom feed.
|
|
3
|
+
*
|
|
4
|
+
* It’s not intended as a complete solution to all your feed generation needs,
|
|
5
|
+
* but as a minimal yet practical default that works well with most feed readers.
|
|
6
|
+
* @module
|
|
7
|
+
*/
|
|
8
|
+
import { type Html } from "@mastrojs/mastro";
|
|
9
|
+
/**
|
|
10
|
+
* An Atom feed with an array of entries.
|
|
11
|
+
*/
|
|
12
|
+
export interface AtomFeed {
|
|
13
|
+
author?: AtomAuthor;
|
|
14
|
+
entries: AtomEntry[];
|
|
15
|
+
/** Link to the feed itself. */
|
|
16
|
+
linkSelf?: URL;
|
|
17
|
+
/** Link to an alternate representation of the feed, e.g. the HTML web page. */
|
|
18
|
+
linkWebsite?: URL;
|
|
19
|
+
/** Small image for the feed. Should be square. */
|
|
20
|
+
icon?: URL;
|
|
21
|
+
/** Universally unique and permanent URI identifying the feed. */
|
|
22
|
+
id: URL;
|
|
23
|
+
/** Large image for the feed. Should be twice as wide as it's tall. */
|
|
24
|
+
logo?: URL;
|
|
25
|
+
/** Human-readable description or subtitle for the feed. */
|
|
26
|
+
subtitle?: string;
|
|
27
|
+
/** Human-readable title of the feed. */
|
|
28
|
+
title: string;
|
|
29
|
+
/** Last time the feed was modified in a significant way. */
|
|
30
|
+
updated: Date;
|
|
31
|
+
}
|
|
32
|
+
interface AtomEntry {
|
|
33
|
+
author?: AtomAuthor;
|
|
34
|
+
content?: Html;
|
|
35
|
+
/** Universally unique and permanent URI identifying the entry. */
|
|
36
|
+
id: URL;
|
|
37
|
+
/** Link to an alternate representation of the entry, e.g. the HTML web page. */
|
|
38
|
+
link?: URL;
|
|
39
|
+
/** Short summary, abstract, or excerpt of the entry. */
|
|
40
|
+
summary?: string;
|
|
41
|
+
/** Human-readable title of the entry. */
|
|
42
|
+
title: string;
|
|
43
|
+
/** Last time the entry was modified in a significant way. */
|
|
44
|
+
updated: Date;
|
|
45
|
+
}
|
|
46
|
+
interface AtomAuthor {
|
|
47
|
+
email?: string;
|
|
48
|
+
name: string;
|
|
49
|
+
uri?: URL;
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Create a standard Response object containing an [Atom feed](https://validator.w3.org/feed/docs/atom.html).
|
|
53
|
+
*
|
|
54
|
+
* Validate your output with the [feed validator](https://validator.w3.org/feed/).
|
|
55
|
+
*/
|
|
56
|
+
export declare const atomResponse: (feed: AtomFeed) => Response;
|
|
57
|
+
export {};
|
|
58
|
+
//# sourceMappingURL=feed.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"feed.d.ts","sourceRoot":"","sources":["../src/feed.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,EAAkB,KAAK,IAAI,EAAwB,MAAM,kBAAkB,CAAC;AAEnF;;GAEG;AACH,MAAM,WAAW,QAAQ;IACvB,MAAM,CAAC,EAAE,UAAU,CAAC;IACpB,OAAO,EAAE,SAAS,EAAE,CAAC;IACrB,+BAA+B;IAC/B,QAAQ,CAAC,EAAE,GAAG,CAAC;IACf,+EAA+E;IAC/E,WAAW,CAAC,EAAE,GAAG,CAAC;IAClB,kDAAkD;IAClD,IAAI,CAAC,EAAE,GAAG,CAAC;IACX,iEAAiE;IACjE,EAAE,EAAE,GAAG,CAAC;IACR,sEAAsE;IACtE,IAAI,CAAC,EAAE,GAAG,CAAC;IACX,2DAA2D;IAC3D,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,wCAAwC;IACxC,KAAK,EAAE,MAAM,CAAC;IACd,4DAA4D;IAC5D,OAAO,EAAE,IAAI,CAAC;CACf;AAED,UAAU,SAAS;IACjB,MAAM,CAAC,EAAE,UAAU,CAAC;IACpB,OAAO,CAAC,EAAE,IAAI,CAAC;IACf,kEAAkE;IAClE,EAAE,EAAE,GAAG,CAAC;IACR,gFAAgF;IAChF,IAAI,CAAC,EAAE,GAAG,CAAC;IACX,wDAAwD;IACxD,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,yCAAyC;IACzC,KAAK,EAAE,MAAM,CAAC;IACd,6DAA6D;IAC7D,OAAO,EAAE,IAAI,CAAC;CACf;AAED,UAAU,UAAU;IAClB,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,IAAI,EAAE,MAAM,CAAC;IACb,GAAG,CAAC,EAAE,GAAG,CAAC;CACX;AAED;;;;GAIG;AACH,eAAO,MAAM,YAAY,SAAU,QAAQ,KAAG,QAkB7C,CAAA"}
|
package/dist/feed.js
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Module with helper function to create an Atom feed.
|
|
3
|
+
*
|
|
4
|
+
* It’s not intended as a complete solution to all your feed generation needs,
|
|
5
|
+
* but as a minimal yet practical default that works well with most feed readers.
|
|
6
|
+
* @module
|
|
7
|
+
*/
|
|
8
|
+
import { htmlToResponse, html, renderToString } from "@mastrojs/mastro";
|
|
9
|
+
/**
|
|
10
|
+
* Create a standard Response object containing an [Atom feed](https://validator.w3.org/feed/docs/atom.html).
|
|
11
|
+
*
|
|
12
|
+
* Validate your output with the [feed validator](https://validator.w3.org/feed/).
|
|
13
|
+
*/
|
|
14
|
+
export const atomResponse = (feed) => {
|
|
15
|
+
const node = html `<?xml version="1.0" encoding="utf-8"?>
|
|
16
|
+
<feed xmlns="http://www.w3.org/2005/Atom">
|
|
17
|
+
<title>${feed.title}</title>
|
|
18
|
+
${feed.subtitle ? html `<subtitle>${feed.subtitle}</subtitle>` : ""}
|
|
19
|
+
${feed.linkSelf ? html `<link rel="self" href=${feed.linkSelf.toString()} />` : ""}
|
|
20
|
+
${feed.linkWebsite ? html `<link rel="alternate" href=${feed.linkWebsite.toString()} />` : ""}
|
|
21
|
+
<updated>${feed.updated.toISOString()}</updated>
|
|
22
|
+
${feed.author ? renderAuthor(feed.author) : ""}
|
|
23
|
+
<id>${feed.id.toString()}</id>
|
|
24
|
+
${feed.icon ? html `<icon>${feed.icon.toString()}</icon>` : ""}
|
|
25
|
+
${feed.logo ? html `<logo>${feed.logo.toString()}</logo>` : ""}
|
|
26
|
+
${feed.entries.map(renderEntry)}
|
|
27
|
+
</feed>
|
|
28
|
+
`;
|
|
29
|
+
const res = htmlToResponse(node);
|
|
30
|
+
res.headers.set("Content-Type", "application/atom+xml");
|
|
31
|
+
return res;
|
|
32
|
+
};
|
|
33
|
+
const renderAuthor = (a) => html `
|
|
34
|
+
<author>
|
|
35
|
+
<name>${a.name}</name>
|
|
36
|
+
${a.email ? html `<email>${a.email}</email>` : ""}
|
|
37
|
+
${a.uri ? html `<uri>${a.uri.toString()}</uri>` : ""}
|
|
38
|
+
</author>
|
|
39
|
+
`;
|
|
40
|
+
const renderEntry = (e) => html `
|
|
41
|
+
<entry>
|
|
42
|
+
<id>${e.id.toString()}</id>
|
|
43
|
+
<title>${e.title}</title>
|
|
44
|
+
<updated>${e.updated.toISOString()}</updated>
|
|
45
|
+
${e.author ? renderAuthor(e.author) : ""}
|
|
46
|
+
${e.link
|
|
47
|
+
? html `<link rel="alternate" href=${e.link.toString()} />`
|
|
48
|
+
: ""}
|
|
49
|
+
${e.summary ? html `<summary>${e.summary}</summary>` : ""}
|
|
50
|
+
${e.content
|
|
51
|
+
? html `<content type="html">${renderToString(e.content)}</content>`
|
|
52
|
+
: ""}
|
|
53
|
+
</entry>
|
|
54
|
+
`;
|
package/package.json
CHANGED
|
@@ -1,6 +1,39 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mastrojs/feed",
|
|
3
|
-
"version": "0.0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "0.0.2",
|
|
4
|
+
"description": "Generate Atom feeds with Mastro.",
|
|
5
|
+
"homepage": "https://github.com/mastrojs/mastro/tree/main/libs/feed",
|
|
6
|
+
"bugs": "https://github.com/mastrojs/mastro/issues",
|
|
7
|
+
"license": "MIT",
|
|
8
|
+
"repository": {
|
|
9
|
+
"type": "git",
|
|
10
|
+
"url": "git+https://github.com/mastrojs/mastro.git",
|
|
11
|
+
"directory": "libs/feed"
|
|
12
|
+
},
|
|
13
|
+
"files": [
|
|
14
|
+
"README.md",
|
|
15
|
+
"dist",
|
|
16
|
+
"src",
|
|
17
|
+
"!**/*.test.*",
|
|
18
|
+
"!**/*.bench.*"
|
|
19
|
+
],
|
|
20
|
+
"type": "module",
|
|
21
|
+
"exports": {
|
|
22
|
+
".": {
|
|
23
|
+
"types": "./dist/feed.d.ts",
|
|
24
|
+
"default": "./dist/feed.js"
|
|
25
|
+
}
|
|
26
|
+
},
|
|
27
|
+
"publishConfig": {
|
|
28
|
+
"access": "public",
|
|
29
|
+
"directory": "dist",
|
|
30
|
+
"linkDirectory": false
|
|
31
|
+
},
|
|
32
|
+
"dependencies": {
|
|
33
|
+
"@mastrojs/mastro": "^0"
|
|
34
|
+
},
|
|
35
|
+
"scripts": {
|
|
36
|
+
"check": "tsc --noEmit",
|
|
37
|
+
"test": "node --test 'src/**/*.test.ts'"
|
|
38
|
+
}
|
|
6
39
|
}
|
package/src/feed.ts
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Module with helper function to create an Atom feed.
|
|
3
|
+
*
|
|
4
|
+
* It’s not intended as a complete solution to all your feed generation needs,
|
|
5
|
+
* but as a minimal yet practical default that works well with most feed readers.
|
|
6
|
+
* @module
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { htmlToResponse, type Html, html, renderToString } from "@mastrojs/mastro";
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* An Atom feed with an array of entries.
|
|
13
|
+
*/
|
|
14
|
+
export interface AtomFeed {
|
|
15
|
+
author?: AtomAuthor;
|
|
16
|
+
entries: AtomEntry[];
|
|
17
|
+
/** Link to the feed itself. */
|
|
18
|
+
linkSelf?: URL;
|
|
19
|
+
/** Link to an alternate representation of the feed, e.g. the HTML web page. */
|
|
20
|
+
linkWebsite?: URL;
|
|
21
|
+
/** Small image for the feed. Should be square. */
|
|
22
|
+
icon?: URL;
|
|
23
|
+
/** Universally unique and permanent URI identifying the feed. */
|
|
24
|
+
id: URL;
|
|
25
|
+
/** Large image for the feed. Should be twice as wide as it's tall. */
|
|
26
|
+
logo?: URL;
|
|
27
|
+
/** Human-readable description or subtitle for the feed. */
|
|
28
|
+
subtitle?: string;
|
|
29
|
+
/** Human-readable title of the feed. */
|
|
30
|
+
title: string;
|
|
31
|
+
/** Last time the feed was modified in a significant way. */
|
|
32
|
+
updated: Date;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
interface AtomEntry {
|
|
36
|
+
author?: AtomAuthor;
|
|
37
|
+
content?: Html;
|
|
38
|
+
/** Universally unique and permanent URI identifying the entry. */
|
|
39
|
+
id: URL;
|
|
40
|
+
/** Link to an alternate representation of the entry, e.g. the HTML web page. */
|
|
41
|
+
link?: URL;
|
|
42
|
+
/** Short summary, abstract, or excerpt of the entry. */
|
|
43
|
+
summary?: string;
|
|
44
|
+
/** Human-readable title of the entry. */
|
|
45
|
+
title: string;
|
|
46
|
+
/** Last time the entry was modified in a significant way. */
|
|
47
|
+
updated: Date;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
interface AtomAuthor {
|
|
51
|
+
email?: string;
|
|
52
|
+
name: string;
|
|
53
|
+
uri?: URL;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Create a standard Response object containing an [Atom feed](https://validator.w3.org/feed/docs/atom.html).
|
|
58
|
+
*
|
|
59
|
+
* Validate your output with the [feed validator](https://validator.w3.org/feed/).
|
|
60
|
+
*/
|
|
61
|
+
export const atomResponse = (feed: AtomFeed): Response => {
|
|
62
|
+
const node = html`<?xml version="1.0" encoding="utf-8"?>
|
|
63
|
+
<feed xmlns="http://www.w3.org/2005/Atom">
|
|
64
|
+
<title>${feed.title}</title>
|
|
65
|
+
${feed.subtitle ? html`<subtitle>${feed.subtitle}</subtitle>` : ""}
|
|
66
|
+
${feed.linkSelf ? html`<link rel="self" href=${feed.linkSelf.toString()} />` : "" }
|
|
67
|
+
${feed.linkWebsite ? html`<link rel="alternate" href=${feed.linkWebsite.toString()} />` : "" }
|
|
68
|
+
<updated>${feed.updated.toISOString()}</updated>
|
|
69
|
+
${feed.author ? renderAuthor(feed.author) : ""}
|
|
70
|
+
<id>${feed.id.toString()}</id>
|
|
71
|
+
${feed.icon ? html`<icon>${feed.icon.toString()}</icon>` : ""}
|
|
72
|
+
${feed.logo ? html`<logo>${feed.logo.toString()}</logo>` : ""}
|
|
73
|
+
${feed.entries.map(renderEntry)}
|
|
74
|
+
</feed>
|
|
75
|
+
`;
|
|
76
|
+
const res = htmlToResponse(node);
|
|
77
|
+
res.headers.set("Content-Type", "application/atom+xml");
|
|
78
|
+
return res;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
const renderAuthor = (a: AtomAuthor) =>
|
|
82
|
+
html`
|
|
83
|
+
<author>
|
|
84
|
+
<name>${a.name}</name>
|
|
85
|
+
${a.email ? html`<email>${a.email}</email>` : ""}
|
|
86
|
+
${a.uri ? html`<uri>${a.uri.toString()}</uri>` : ""}
|
|
87
|
+
</author>
|
|
88
|
+
`;
|
|
89
|
+
|
|
90
|
+
const renderEntry = (e: AtomEntry) =>
|
|
91
|
+
html`
|
|
92
|
+
<entry>
|
|
93
|
+
<id>${e.id.toString()}</id>
|
|
94
|
+
<title>${e.title}</title>
|
|
95
|
+
<updated>${e.updated.toISOString()}</updated>
|
|
96
|
+
${e.author ? renderAuthor(e.author) : ""}
|
|
97
|
+
${e.link
|
|
98
|
+
? html`<link rel="alternate" href=${e.link.toString()} />`
|
|
99
|
+
: "" }
|
|
100
|
+
${e.summary ? html`<summary>${e.summary}</summary>` : ""}
|
|
101
|
+
${e.content
|
|
102
|
+
? html`<content type="html">${renderToString(e.content)}</content>`
|
|
103
|
+
: ""}
|
|
104
|
+
</entry>
|
|
105
|
+
`;
|