astro-relatinator 1.0.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/README.md ADDED
@@ -0,0 +1,5 @@
1
+ ## Astro Integration for Relatinator
2
+
3
+ An Astro integration for the [humble library for finding related posts and content](https://github.com/DBozhinovski/relatinator/tree/master/packages/relatinator).
4
+
5
+ ## Installation
package/integration.ts ADDED
@@ -0,0 +1,75 @@
1
+ import type { AstroIntegration } from "astro";
2
+ import path from "node:path";
3
+ import { train, tfIdf } from "relatinator";
4
+ import { glob } from "glob";
5
+ import matter from "gray-matter";
6
+ import fs from "node:fs/promises";
7
+ import { TfIdfDocument } from "natural";
8
+ import { Document } from "relatinator";
9
+
10
+ async function trainModel(paths: string[]) {
11
+ const documents: Document[] = [];
12
+
13
+ if (tfIdf.documents) {
14
+ if ((tfIdf.documents as unknown as TfIdfDocument[]).length > 0) {
15
+ (tfIdf.documents as unknown as TfIdfDocument[]) = [];
16
+ (tfIdf.documents as unknown as TfIdfDocument[]).length = -1;
17
+ }
18
+ }
19
+
20
+ for (const path of paths) {
21
+ const markdownFiles = await glob(`${path}/**/*.{md,mdx}`);
22
+
23
+ for (const file of markdownFiles) {
24
+ const content = await fs.readFile(file, "utf-8");
25
+ const parsed = matter(content);
26
+ const idArr = file.split("/");
27
+ const id = idArr[idArr.length - 1];
28
+
29
+ documents.push({
30
+ id,
31
+ content: `${parsed.data.title} ${
32
+ parsed.data.description
33
+ } ${parsed.data.tags.join(" ")} ${parsed.content}`,
34
+ });
35
+ }
36
+ }
37
+
38
+ train(documents);
39
+ }
40
+
41
+ const relatinatorIntegration = ({
42
+ paths,
43
+ }: {
44
+ paths: string[];
45
+ }): AstroIntegration => {
46
+ return {
47
+ name: "astro-relatinator",
48
+ hooks: {
49
+ "astro:server:setup": async ({ server, logger }) => {
50
+ logger.info("relatinator server:setup");
51
+
52
+ logger.info("Training TF-IDF on existing data...");
53
+ await trainModel(paths);
54
+ logger.info("Initial training done.");
55
+
56
+ server.watcher.on("all", async (eventName, filePath) => {
57
+ if (!["add", "change"].includes(eventName)) {
58
+ return;
59
+ }
60
+
61
+ if (path.extname(filePath).includes("md")) {
62
+ logger.info(
63
+ `Markdown file changed: ${filePath}; Re-training TF-IDF...`
64
+ );
65
+ await trainModel(paths);
66
+ } else {
67
+ return;
68
+ }
69
+ });
70
+ },
71
+ },
72
+ };
73
+ };
74
+
75
+ export default relatinatorIntegration;
package/package.json ADDED
@@ -0,0 +1,42 @@
1
+ {
2
+ "name": "astro-relatinator",
3
+ "version": "1.0.0",
4
+ "description": "Find related content in your Astro website or app",
5
+ "author": "Darko Bozhinovski <dbozhinovski@gmail.com>",
6
+ "license": "MIT",
7
+ "keywords": [
8
+ "astro",
9
+ "related-content",
10
+ "tfidf",
11
+ "relatinator",
12
+ "related",
13
+ "words",
14
+ "posts",
15
+ "articles",
16
+ "static"
17
+ ],
18
+ "files": [
19
+ "integration.ts"
20
+ ],
21
+ "exports": {
22
+ ".": "./integration.ts"
23
+ },
24
+ "repository": {
25
+ "type": "git",
26
+ "url": "https://github.com/DBozhinovski/relatinator"
27
+ },
28
+ "type": "module",
29
+ "devDependencies": {
30
+ "@types/node": "^20.10.4",
31
+ "astro": "3",
32
+ "husky": "^8.0.3",
33
+ "natural": "^6.10.3"
34
+ },
35
+ "dependencies": {
36
+ "glob": "^10.3.10",
37
+ "relatinator": "^1.1.0"
38
+ },
39
+ "peerDependencies": {
40
+ "astro": "3"
41
+ }
42
+ }