webring 0.0.2 → 0.3.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 CHANGED
@@ -5,6 +5,13 @@ All notable changes to this project will be documented in this file.
5
5
  The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
6
6
  and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
7
 
8
+ ## [0.3.0](https://github.com/shepherdjerred/webring/compare/v0.2.0...v0.3.0) (2024-06-03)
9
+
10
+
11
+ ### Features
12
+
13
+ * allow filename to be configurable ([cc7bb5f](https://github.com/shepherdjerred/webring/commit/cc7bb5f3139f306952d03568fc63cc9fcbfaad5e))
14
+
8
15
  ## [Unreleased]
9
16
 
10
17
  * Initial release
package/README.md CHANGED
@@ -11,7 +11,7 @@ Inspired by:
11
11
 
12
12
  ## Installation
13
13
 
14
- ```
14
+ ```bash
15
15
  npm i webring
16
16
  ```
17
17
 
@@ -19,14 +19,35 @@ npm i webring
19
19
 
20
20
  This library is meant to be used with static site generators. It is framework agnostic.
21
21
 
22
- ### Astro
23
-
24
- ### Command Line
25
-
26
- You can use this library to generate static HTML which can be included into your site using it's include mechanisms
27
-
28
- ## Configuration
29
-
30
22
  ```typescript
31
-
23
+ import { type Configuration, run } from "webring";
24
+
25
+ // create a configuration object
26
+ const config: Configuration = {
27
+ sources: [
28
+ {
29
+ url: "https://drewdevault.com/blog/index.xml",
30
+ title: "Drew DeVault",
31
+ },
32
+ {
33
+ url: "https://danluu.com/atom.xml",
34
+ title: "Dan Luu",
35
+ },
36
+ {
37
+ url: "https://jakelazaroff.com/rss.xml",
38
+ title: "Jake Lazaroff",
39
+ },
40
+ ],
41
+ number: 3,
42
+ cache_duration_minutes: 60,
43
+ truncate: 300,
44
+ };
45
+
46
+ // run the application
47
+ const result = await run(config);
48
+
49
+ // do something with the results
50
+ result.map((entry) => {
51
+ console.log(entry);
52
+ });
32
53
  ```
package/package.json CHANGED
@@ -1,11 +1,12 @@
1
1
  {
2
2
  "name": "webring",
3
3
  "type": "module",
4
- "version": "0.0.2",
4
+ "version": "0.3.0",
5
5
  "scripts": {
6
6
  "lint": "eslint .",
7
7
  "build": "tsc",
8
- "test": ""
8
+ "test": "",
9
+ "prepare": "husky"
9
10
  },
10
11
  "main": "dist/index.js",
11
12
  "types": "dist/index.d.ts",
@@ -17,11 +18,13 @@
17
18
  "zod": "^3.23.8"
18
19
  },
19
20
  "devDependencies": {
20
- "@eslint/js": "^9.3.0",
21
+ "@commitlint/cli": "^19.3.0",
22
+ "@commitlint/config-conventional": "^19.2.2",
23
+ "@eslint/js": "^9.4.0",
21
24
  "@tsconfig/node20": "^20.1.4",
22
25
  "@tsconfig/strictest": "^2.0.5",
23
26
  "@types/eslint__js": "^8.42.3",
24
- "@types/node": "^20.12.13",
27
+ "@types/node": "^20.13.0",
25
28
  "@types/sanitize-html": "^2.11.0",
26
29
  "@typescript-eslint/eslint-plugin": "^7.11.0",
27
30
  "@typescript-eslint/parser": "^7.11.0",
@@ -33,7 +36,7 @@
33
36
  "typescript-eslint": "^7.11.0"
34
37
  },
35
38
  "lint-staged": {
36
- "*.{js,jsx,ts,tsx}": "eslint --cache --fix",
39
+ "*.{ts,tsx}": "eslint --cache --fix",
37
40
  "*": "prettier --ignore-unknown --write"
38
41
  },
39
42
  "files": [
package/dist/cache.d.ts DELETED
@@ -1,3 +0,0 @@
1
- import type { Configuration, Cache, Result, ResultEntry, Source } from "./types.js";
2
- export declare function runWithCache(config: Configuration, cache: Cache): Promise<[Result, Cache]>;
3
- export declare function fetchWithCache(source: Source, cache: Cache, config: Configuration): Promise<ResultEntry | undefined>;
package/dist/cache.js DELETED
@@ -1,24 +0,0 @@
1
- import * as R from "remeda";
2
- export async function runWithCache(config, cache) {
3
- const promises = R.pipe(config.sources, R.map((source) => fetchWithCache(source, cache, config)), R.filter((result) => result !== undefined));
4
- const results = await Promise.all(promises);
5
- const definedResults = results.filter((result) => result !== undefined);
6
- const updatedCache = R.pipe(definedResults, R.map((result) => [result.source.url, { timestamp: new Date(), data: result }]), R.fromEntries());
7
- const topResults = R.pipe(definedResults, R.sortBy((result) => result.date.getTime()), R.reverse(), R.take(config.number));
8
- return [topResults, updatedCache];
9
- }
10
- export async function fetchWithCache(source, cache, config) {
11
- const cacheEntry = cache[source.url];
12
- if (cacheEntry) {
13
- const now = new Date();
14
- if (now.getTime() - cacheEntry.timestamp.getTime() < config.cache_duration_minutes * 60 * 1000) {
15
- console.log(`Cache entry found for ${source.url}`);
16
- return Promise.resolve(cacheEntry.data);
17
- }
18
- else {
19
- console.log(`Cache entry for ${source.url} is too old`);
20
- }
21
- }
22
- console.log(`No cache entry for ${source.url}`);
23
- return fetch(source, config.truncate);
24
- }
package/dist/fetch.d.ts DELETED
@@ -1,2 +0,0 @@
1
- import { type Source, type ResultEntry } from "./types.js";
2
- export declare function fetch(source: Source, length: number): Promise<ResultEntry | undefined>;
package/dist/fetch.js DELETED
@@ -1,27 +0,0 @@
1
- import Parser from "rss-parser";
2
- import sanitizeHtml from "sanitize-html";
3
- import truncate from "truncate-html";
4
- import { FeedEntrySchema } from "./types.js";
5
- import * as R from "remeda";
6
- export async function fetch(source, length) {
7
- const parser = new Parser();
8
- try {
9
- const feed = await parser.parseURL(source.url);
10
- const firstItem = R.pipe(feed.items, R.map((item) => FeedEntrySchema.parse(item)), R.sortBy((item) => new Date(item.date).getTime()), R.reverse(), R.first());
11
- if (!firstItem) {
12
- throw new Error("no items found in feed");
13
- }
14
- const preview = firstItem.contentSnippet ?? firstItem.content ?? firstItem.description ?? firstItem["content:encoded"];
15
- return {
16
- title: firstItem.title,
17
- url: firstItem.link,
18
- date: new Date(firstItem.date),
19
- source,
20
- preview: preview ? truncate(sanitizeHtml(preview), length) : undefined,
21
- };
22
- }
23
- catch (e) {
24
- console.error(`Error fetching ${source.url}: ${e}`);
25
- return undefined;
26
- }
27
- }
package/dist/index.d.ts DELETED
@@ -1,2 +0,0 @@
1
- import { type Configuration, type Result } from "./types.js";
2
- export declare function run(config: Configuration): Promise<Result>;
package/dist/index.js DELETED
@@ -1,21 +0,0 @@
1
- import { runWithCache } from "./cache.js";
2
- import { CacheSchema } from "./types.js";
3
- import fs from "fs/promises";
4
- export async function run(config) {
5
- const cacheFilename = "cache.json";
6
- const currentDir = process.cwd();
7
- const fullFilename = `${currentDir}/${cacheFilename}`;
8
- let cacheObject = {};
9
- try {
10
- const cacheFile = await fs.readFile(fullFilename);
11
- cacheObject = CacheSchema.parse(JSON.parse(cacheFile.toString()));
12
- }
13
- catch (e) {
14
- console.error("Error reading cache file:", e);
15
- throw e;
16
- }
17
- const [result, updatedCache] = await runWithCache(config, cacheObject);
18
- // write the updated cache to cache.json
19
- await fs.writeFile(fullFilename, JSON.stringify(updatedCache));
20
- return result;
21
- }
package/dist/types.d.ts DELETED
@@ -1,284 +0,0 @@
1
- import { z } from "zod";
2
- export type Source = z.infer<typeof SourceSchema>;
3
- declare const SourceSchema: z.ZodObject<{
4
- url: z.ZodString;
5
- title: z.ZodString;
6
- }, "strip", z.ZodTypeAny, {
7
- url: string;
8
- title: string;
9
- }, {
10
- url: string;
11
- title: string;
12
- }>;
13
- export type Configuration = z.infer<typeof ConfigurationSchema>;
14
- declare const ConfigurationSchema: z.ZodObject<{
15
- sources: z.ZodArray<z.ZodObject<{
16
- url: z.ZodString;
17
- title: z.ZodString;
18
- }, "strip", z.ZodTypeAny, {
19
- url: string;
20
- title: string;
21
- }, {
22
- url: string;
23
- title: string;
24
- }>, "many">;
25
- number: z.ZodNumber;
26
- cache_duration_minutes: z.ZodNumber;
27
- truncate: z.ZodNumber;
28
- }, "strip", z.ZodTypeAny, {
29
- number: number;
30
- sources: {
31
- url: string;
32
- title: string;
33
- }[];
34
- cache_duration_minutes: number;
35
- truncate: number;
36
- }, {
37
- number: number;
38
- sources: {
39
- url: string;
40
- title: string;
41
- }[];
42
- cache_duration_minutes: number;
43
- truncate: number;
44
- }>;
45
- export type ResultEntry = z.infer<typeof ResultEntrySchema>;
46
- declare const ResultEntrySchema: z.ZodObject<{
47
- title: z.ZodString;
48
- url: z.ZodString;
49
- date: z.ZodDate;
50
- source: z.ZodObject<{
51
- url: z.ZodString;
52
- title: z.ZodString;
53
- }, "strip", z.ZodTypeAny, {
54
- url: string;
55
- title: string;
56
- }, {
57
- url: string;
58
- title: string;
59
- }>;
60
- preview: z.ZodOptional<z.ZodString>;
61
- }, "strip", z.ZodTypeAny, {
62
- url: string;
63
- title: string;
64
- date: Date;
65
- source: {
66
- url: string;
67
- title: string;
68
- };
69
- preview?: string | undefined;
70
- }, {
71
- url: string;
72
- title: string;
73
- date: Date;
74
- source: {
75
- url: string;
76
- title: string;
77
- };
78
- preview?: string | undefined;
79
- }>;
80
- export type Result = z.infer<typeof ResultSchema>;
81
- declare const ResultSchema: z.ZodArray<z.ZodObject<{
82
- title: z.ZodString;
83
- url: z.ZodString;
84
- date: z.ZodDate;
85
- source: z.ZodObject<{
86
- url: z.ZodString;
87
- title: z.ZodString;
88
- }, "strip", z.ZodTypeAny, {
89
- url: string;
90
- title: string;
91
- }, {
92
- url: string;
93
- title: string;
94
- }>;
95
- preview: z.ZodOptional<z.ZodString>;
96
- }, "strip", z.ZodTypeAny, {
97
- url: string;
98
- title: string;
99
- date: Date;
100
- source: {
101
- url: string;
102
- title: string;
103
- };
104
- preview?: string | undefined;
105
- }, {
106
- url: string;
107
- title: string;
108
- date: Date;
109
- source: {
110
- url: string;
111
- title: string;
112
- };
113
- preview?: string | undefined;
114
- }>, "many">;
115
- export type CacheEntry = z.infer<typeof CacheEntrySchema>;
116
- export declare const CacheEntrySchema: z.ZodObject<{
117
- timestamp: z.ZodDate;
118
- data: z.ZodObject<{
119
- title: z.ZodString;
120
- url: z.ZodString;
121
- date: z.ZodDate;
122
- source: z.ZodObject<{
123
- url: z.ZodString;
124
- title: z.ZodString;
125
- }, "strip", z.ZodTypeAny, {
126
- url: string;
127
- title: string;
128
- }, {
129
- url: string;
130
- title: string;
131
- }>;
132
- preview: z.ZodOptional<z.ZodString>;
133
- }, "strip", z.ZodTypeAny, {
134
- url: string;
135
- title: string;
136
- date: Date;
137
- source: {
138
- url: string;
139
- title: string;
140
- };
141
- preview?: string | undefined;
142
- }, {
143
- url: string;
144
- title: string;
145
- date: Date;
146
- source: {
147
- url: string;
148
- title: string;
149
- };
150
- preview?: string | undefined;
151
- }>;
152
- }, "strip", z.ZodTypeAny, {
153
- timestamp: Date;
154
- data: {
155
- url: string;
156
- title: string;
157
- date: Date;
158
- source: {
159
- url: string;
160
- title: string;
161
- };
162
- preview?: string | undefined;
163
- };
164
- }, {
165
- timestamp: Date;
166
- data: {
167
- url: string;
168
- title: string;
169
- date: Date;
170
- source: {
171
- url: string;
172
- title: string;
173
- };
174
- preview?: string | undefined;
175
- };
176
- }>;
177
- export type Cache = z.infer<typeof CacheSchema>;
178
- export declare const CacheSchema: z.ZodRecord<z.ZodString, z.ZodObject<{
179
- timestamp: z.ZodDate;
180
- data: z.ZodObject<{
181
- title: z.ZodString;
182
- url: z.ZodString;
183
- date: z.ZodDate;
184
- source: z.ZodObject<{
185
- url: z.ZodString;
186
- title: z.ZodString;
187
- }, "strip", z.ZodTypeAny, {
188
- url: string;
189
- title: string;
190
- }, {
191
- url: string;
192
- title: string;
193
- }>;
194
- preview: z.ZodOptional<z.ZodString>;
195
- }, "strip", z.ZodTypeAny, {
196
- url: string;
197
- title: string;
198
- date: Date;
199
- source: {
200
- url: string;
201
- title: string;
202
- };
203
- preview?: string | undefined;
204
- }, {
205
- url: string;
206
- title: string;
207
- date: Date;
208
- source: {
209
- url: string;
210
- title: string;
211
- };
212
- preview?: string | undefined;
213
- }>;
214
- }, "strip", z.ZodTypeAny, {
215
- timestamp: Date;
216
- data: {
217
- url: string;
218
- title: string;
219
- date: Date;
220
- source: {
221
- url: string;
222
- title: string;
223
- };
224
- preview?: string | undefined;
225
- };
226
- }, {
227
- timestamp: Date;
228
- data: {
229
- url: string;
230
- title: string;
231
- date: Date;
232
- source: {
233
- url: string;
234
- title: string;
235
- };
236
- preview?: string | undefined;
237
- };
238
- }>>;
239
- export declare const FeedEntrySchema: z.ZodEffects<z.ZodObject<{
240
- title: z.ZodString;
241
- link: z.ZodString;
242
- isoDate: z.ZodOptional<z.ZodDate>;
243
- pubDate: z.ZodOptional<z.ZodDate>;
244
- content: z.ZodOptional<z.ZodString>;
245
- contentSnippet: z.ZodOptional<z.ZodString>;
246
- "content:encoded": z.ZodOptional<z.ZodString>;
247
- description: z.ZodOptional<z.ZodString>;
248
- }, "strip", z.ZodTypeAny, {
249
- title: string;
250
- link: string;
251
- isoDate?: Date | undefined;
252
- pubDate?: Date | undefined;
253
- content?: string | undefined;
254
- contentSnippet?: string | undefined;
255
- "content:encoded"?: string | undefined;
256
- description?: string | undefined;
257
- }, {
258
- title: string;
259
- link: string;
260
- isoDate?: Date | undefined;
261
- pubDate?: Date | undefined;
262
- content?: string | undefined;
263
- contentSnippet?: string | undefined;
264
- "content:encoded"?: string | undefined;
265
- description?: string | undefined;
266
- }>, {
267
- title: string;
268
- link: string;
269
- date: Date;
270
- content: string | undefined;
271
- contentSnippet: string | undefined;
272
- description: string | undefined;
273
- "content:encoded": string | undefined;
274
- }, {
275
- title: string;
276
- link: string;
277
- isoDate?: Date | undefined;
278
- pubDate?: Date | undefined;
279
- content?: string | undefined;
280
- contentSnippet?: string | undefined;
281
- "content:encoded"?: string | undefined;
282
- description?: string | undefined;
283
- }>;
284
- export {};
package/dist/types.js DELETED
@@ -1,50 +0,0 @@
1
- import { z } from "zod";
2
- const SourceSchema = z.object({
3
- url: z.string(),
4
- title: z.string(),
5
- });
6
- const ConfigurationSchema = z.object({
7
- sources: SourceSchema.array(),
8
- number: z.number(),
9
- cache_duration_minutes: z.number(),
10
- truncate: z.number(),
11
- });
12
- const ResultEntrySchema = z.object({
13
- title: z.string(),
14
- url: z.string(),
15
- date: z.coerce.date(),
16
- source: SourceSchema,
17
- preview: z.string().optional(),
18
- });
19
- const ResultSchema = z.array(ResultEntrySchema);
20
- export const CacheEntrySchema = z.object({
21
- timestamp: z.coerce.date(),
22
- data: ResultEntrySchema,
23
- });
24
- export const CacheSchema = z.record(CacheEntrySchema);
25
- export const FeedEntrySchema = z
26
- .object({
27
- title: z.string(),
28
- link: z.string(),
29
- isoDate: z.coerce.date().optional(),
30
- pubDate: z.coerce.date().optional(),
31
- content: z.string().optional(),
32
- contentSnippet: z.string().optional(),
33
- "content:encoded": z.string().optional(),
34
- description: z.string().optional(),
35
- })
36
- .transform((entry) => {
37
- const date = entry.isoDate ?? entry.pubDate;
38
- if (!date) {
39
- throw new Error("no date found in feed entry");
40
- }
41
- return {
42
- title: entry.title,
43
- link: entry.link,
44
- date,
45
- content: entry.content,
46
- contentSnippet: entry.contentSnippet,
47
- description: entry.description,
48
- "content:encoded": entry["content:encoded"],
49
- };
50
- });