webring 0.0.1 → 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/CHANGELOG.md ADDED
@@ -0,0 +1,10 @@
1
+ # Changelog
2
+
3
+ All notable changes to this project will be documented in this file.
4
+
5
+ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
6
+ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
+
8
+ ## [Unreleased]
9
+
10
+ * Initial release
package/README.md CHANGED
@@ -1,5 +1,7 @@
1
1
  # webring
2
2
 
3
+ [![webring](https://img.shields.io/npm/v/webring.svg)](https://www.npmjs.com/package/webring)
4
+
3
5
  `webring` gathers the latest posts from your favorite RSS feeds so that you can embed them on your site.
4
6
 
5
7
  Inspired by:
@@ -7,6 +9,12 @@ Inspired by:
7
9
  - https://github.com/lukehsiao/openring-rs
8
10
  - https://git.sr.ht/~sircmpwn/openring
9
11
 
12
+ ## Installation
13
+
14
+ ```
15
+ npm i webring
16
+ ```
17
+
10
18
  ## Quick Start
11
19
 
12
20
  This library is meant to be used with static site generators. It is framework agnostic.
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "webring",
3
3
  "type": "module",
4
- "version": "0.0.1",
4
+ "version": "0.0.2",
5
5
  "scripts": {
6
6
  "lint": "eslint .",
7
7
  "build": "tsc",
@@ -35,5 +35,13 @@
35
35
  "lint-staged": {
36
36
  "*.{js,jsx,ts,tsx}": "eslint --cache --fix",
37
37
  "*": "prettier --ignore-unknown --write"
38
- }
38
+ },
39
+ "files": [
40
+ "dist",
41
+ "package.json",
42
+ "README.md",
43
+ "LICENSE",
44
+ "package-lock.json",
45
+ "CHANGELOG.md"
46
+ ]
39
47
  }
@@ -1,12 +0,0 @@
1
- version: 2
2
- updates:
3
- - package-ecosystem: "github-actions"
4
- directory: "/"
5
- schedule:
6
- interval: "monthly"
7
- day: "sunday"
8
- - package-ecosystem: "npm"
9
- directory: "/"
10
- schedule:
11
- interval: "monthly"
12
- day: "sunday"
package/.prettierrc.json DELETED
@@ -1,3 +0,0 @@
1
- {
2
- "printWidth": 120
3
- }
@@ -1,3 +0,0 @@
1
- {
2
- "recommendations": ["dbaeumer.vscode-eslint", "esbenp.prettier-vscode"]
3
- }
package/Earthfile DELETED
@@ -1 +0,0 @@
1
- VERSION 0.9
package/eslint.config.js DELETED
@@ -1,17 +0,0 @@
1
- import eslint from "@eslint/js";
2
- import tseslint from "typescript-eslint";
3
-
4
- export default tseslint.config(
5
- eslint.configs.recommended,
6
- ...tseslint.configs.strictTypeChecked,
7
- ...tseslint.configs.stylisticTypeChecked,
8
- {
9
- languageOptions: {
10
- parserOptions: {
11
- project: true,
12
- tsconfigRootDir: import.meta.dirname,
13
- },
14
- },
15
- ignores: ["*.js"],
16
- },
17
- );
package/src/cache.ts DELETED
@@ -1,48 +0,0 @@
1
- import * as R from "remeda";
2
- import type { Configuration, Cache, Result, ResultEntry, CacheEntry, Source } from "./types.js";
3
-
4
- export async function runWithCache(config: Configuration, cache: Cache): Promise<[Result, Cache]> {
5
- const promises = R.pipe(
6
- config.sources,
7
- R.map((source) => fetchWithCache(source, cache, config)),
8
- R.filter((result) => result !== undefined),
9
- );
10
- const results = await Promise.all(promises);
11
-
12
- const definedResults = results.filter((result) => result !== undefined) as ResultEntry[];
13
-
14
- const updatedCache: Cache = R.pipe(
15
- definedResults,
16
- R.map((result): [string, CacheEntry] => [result.source.url, { timestamp: new Date(), data: result }]),
17
- R.fromEntries(),
18
- );
19
-
20
- const topResults = R.pipe(
21
- definedResults,
22
- R.sortBy((result) => result.date.getTime()),
23
- R.reverse(),
24
- R.take(config.number),
25
- );
26
-
27
- return [topResults, updatedCache];
28
- }
29
-
30
- export async function fetchWithCache(
31
- source: Source,
32
- cache: Cache,
33
- config: Configuration,
34
- ): Promise<ResultEntry | undefined> {
35
- const cacheEntry = cache[source.url];
36
- if (cacheEntry) {
37
- const now = new Date();
38
- if (now.getTime() - cacheEntry.timestamp.getTime() < config.cache_duration_minutes * 60 * 1000) {
39
- console.log(`Cache entry found for ${source.url}`);
40
- return Promise.resolve(cacheEntry.data);
41
- } else {
42
- console.log(`Cache entry for ${source.url} is too old`);
43
- }
44
- }
45
-
46
- console.log(`No cache entry for ${source.url}`);
47
- return fetch(source, config.truncate);
48
- }
package/src/fetch.ts DELETED
@@ -1,38 +0,0 @@
1
- import Parser from "rss-parser";
2
- import sanitizeHtml from "sanitize-html";
3
- import truncate from "truncate-html";
4
- import { type Source, type ResultEntry, FeedEntrySchema } from "./types.js";
5
- import * as R from "remeda";
6
-
7
- export async function fetch(source: Source, length: number): Promise<ResultEntry | undefined> {
8
- const parser = new Parser();
9
- try {
10
- const feed = await parser.parseURL(source.url);
11
-
12
- const firstItem = R.pipe(
13
- feed.items,
14
- R.map((item) => FeedEntrySchema.parse(item)),
15
- R.sortBy((item) => new Date(item.date).getTime()),
16
- R.reverse(),
17
- R.first(),
18
- );
19
-
20
- if (!firstItem) {
21
- throw new Error("no items found in feed");
22
- }
23
-
24
- const preview =
25
- firstItem.contentSnippet ?? firstItem.content ?? firstItem.description ?? firstItem["content:encoded"];
26
-
27
- return {
28
- title: firstItem.title,
29
- url: firstItem.link,
30
- date: new Date(firstItem.date),
31
- source,
32
- preview: preview ? truncate(sanitizeHtml(preview), length) : undefined,
33
- };
34
- } catch (e) {
35
- console.error(`Error fetching ${source.url}: ${e as string}`);
36
- return undefined;
37
- }
38
- }
package/src/index.ts DELETED
@@ -1,26 +0,0 @@
1
- import { runWithCache } from "./cache.js";
2
- import { type Configuration, type Result, type Cache, CacheSchema } from "./types.js";
3
- import fs from "fs/promises";
4
-
5
- export async function run(config: Configuration): Promise<Result> {
6
- const cacheFilename = "cache.json";
7
- const currentDir = process.cwd();
8
- const fullFilename = `${currentDir}/${cacheFilename}`;
9
-
10
- let cacheObject: Cache = {};
11
-
12
- try {
13
- const cacheFile = await fs.readFile(fullFilename);
14
- cacheObject = CacheSchema.parse(JSON.parse(cacheFile.toString()));
15
- } catch (e) {
16
- console.error("Error reading cache file:", e);
17
- throw e;
18
- }
19
-
20
- const [result, updatedCache] = await runWithCache(config, cacheObject);
21
-
22
- // write the updated cache to cache.json
23
- await fs.writeFile(fullFilename, JSON.stringify(updatedCache));
24
-
25
- return result;
26
- }
package/src/types.ts DELETED
@@ -1,63 +0,0 @@
1
- import { z } from "zod";
2
-
3
- export type Source = z.infer<typeof SourceSchema>;
4
- const SourceSchema = z.object({
5
- url: z.string(),
6
- title: z.string(),
7
- });
8
-
9
- export type Configuration = z.infer<typeof ConfigurationSchema>;
10
- const ConfigurationSchema = z.object({
11
- sources: SourceSchema.array(),
12
- number: z.number(),
13
- cache_duration_minutes: z.number(),
14
- truncate: z.number(),
15
- });
16
-
17
- export type ResultEntry = z.infer<typeof ResultEntrySchema>;
18
- const ResultEntrySchema = z.object({
19
- title: z.string(),
20
- url: z.string(),
21
- date: z.coerce.date(),
22
- source: SourceSchema,
23
- preview: z.string().optional(),
24
- });
25
-
26
- export type Result = z.infer<typeof ResultSchema>;
27
- const ResultSchema = z.array(ResultEntrySchema);
28
-
29
- export type CacheEntry = z.infer<typeof CacheEntrySchema>;
30
- export const CacheEntrySchema = z.object({
31
- timestamp: z.coerce.date(),
32
- data: ResultEntrySchema,
33
- });
34
-
35
- export type Cache = z.infer<typeof CacheSchema>;
36
- export const CacheSchema = z.record(CacheEntrySchema);
37
-
38
- export const FeedEntrySchema = z
39
- .object({
40
- title: z.string(),
41
- link: z.string(),
42
- isoDate: z.coerce.date().optional(),
43
- pubDate: z.coerce.date().optional(),
44
- content: z.string().optional(),
45
- contentSnippet: z.string().optional(),
46
- "content:encoded": z.string().optional(),
47
- description: z.string().optional(),
48
- })
49
- .transform((entry) => {
50
- const date = entry.isoDate ?? entry.pubDate;
51
- if (!date) {
52
- throw new Error("no date found in feed entry");
53
- }
54
- return {
55
- title: entry.title,
56
- link: entry.link,
57
- date,
58
- content: entry.content,
59
- contentSnippet: entry.contentSnippet,
60
- description: entry.description,
61
- "content:encoded": entry["content:encoded"],
62
- };
63
- });
package/tsconfig.json DELETED
@@ -1,8 +0,0 @@
1
- {
2
- "extends": ["@tsconfig/strictest/tsconfig.json", "@tsconfig/node20/tsconfig.json"],
3
- "exclude": ["eslint.config.js", "dist"],
4
- "compilerOptions": {
5
- "outDir": "dist",
6
- "declaration": true
7
- }
8
- }