astro-relatinator 1.0.3 → 1.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/README.md CHANGED
@@ -3,3 +3,52 @@
3
3
  An Astro integration for the [humble library for finding related posts and content](https://github.com/DBozhinovski/relatinator/tree/master/packages/relatinator).
4
4
 
5
5
  ## Installation
6
+
7
+ Assuming you're running Astro v3 or later, you can install this integration by running:
8
+
9
+ ```
10
+ npx astro add astro-relatinator
11
+ ```
12
+
13
+ OR
14
+
15
+ ```
16
+ npm install astro-relatinator
17
+ ```
18
+
19
+ ## Configuration
20
+
21
+ The integration takes a single input argument, `paths`, which is an array of paths to the `.md` or `.mdx` files you want to use for training the model. If you're using the [Content Collections API](https://docs.astro.build/en/guides/content-collections/), you can find these at `./src/collections/<collection-name>/`.
22
+
23
+ ### Example:
24
+
25
+ ```ts
26
+ export default defineConfig({
27
+ // ...
28
+ integrations: [
29
+ // ...
30
+ relatinatorIntegration({
31
+ paths: ["./src/content/posts"],
32
+ }),
33
+ // ...
34
+ ],
35
+ });
36
+ ```
37
+
38
+ ## Usage
39
+
40
+ The integration hooks into the Astro build (or dev) process, and will traing the tf-idf model based on the files you've provided. You can then use the `find` function from the package to find related content.
41
+
42
+ ### Example:
43
+
44
+ ```astro
45
+ ---
46
+ import { find } from "astro-relatinator";
47
+
48
+ ...
49
+
50
+ const related = find(id, frontmatter.title, frontmatter.tags, frontmatter.description, body, 3)
51
+
52
+ ---
53
+
54
+ ```
package/integration.ts CHANGED
@@ -7,7 +7,19 @@ import fs from "node:fs/promises";
7
7
  import { TfIdfDocument } from "natural";
8
8
  import { Document } from "relatinator";
9
9
 
10
- async function trainModel(paths: string[]) {
10
+ const stringifyData = (data: any): string => {
11
+ if (typeof data === 'object') {
12
+ if (Array.isArray(data)) {
13
+ return data.map(stringifyData).join(' ');
14
+ } else {
15
+ return Object.values(data).map(stringifyData).join(' ');
16
+ }
17
+ } else {
18
+ return String(data);
19
+ }
20
+ }
21
+
22
+ async function trainModel(paths: string[], schema: string[]) {
11
23
  const documents: Document[] = [];
12
24
 
13
25
  if (tfIdf.documents) {
@@ -26,11 +38,17 @@ async function trainModel(paths: string[]) {
26
38
  const idArr = file.split("/");
27
39
  const id = idArr[idArr.length - 1];
28
40
 
41
+ let documentContent = '';
42
+ for (const key of schema) {
43
+ if (parsed.data[key]) {
44
+ documentContent += ' ' + stringifyData(parsed.data[key]);
45
+ }
46
+ }
47
+ documentContent += ' ' + parsed.content;
48
+
29
49
  documents.push({
30
50
  id,
31
- content: `${parsed.data.title} ${
32
- parsed.data.description
33
- } ${parsed.data.tags.join(" ")} ${parsed.content}`,
51
+ content: documentContent,
34
52
  });
35
53
  }
36
54
  }
@@ -40,8 +58,10 @@ async function trainModel(paths: string[]) {
40
58
 
41
59
  const relatinatorIntegration = ({
42
60
  paths,
61
+ schema,
43
62
  }: {
44
63
  paths: string[];
64
+ schema: string[];
45
65
  }): AstroIntegration => {
46
66
  return {
47
67
  name: "astro-relatinator",
@@ -50,7 +70,7 @@ const relatinatorIntegration = ({
50
70
  logger.info("relatinator server:setup");
51
71
 
52
72
  logger.info("Training TF-IDF on existing data...");
53
- await trainModel(paths);
73
+ await trainModel(paths, schema);
54
74
  logger.info("Initial training done.");
55
75
 
56
76
  server.watcher.on("all", async (eventName, filePath) => {
@@ -62,12 +82,19 @@ const relatinatorIntegration = ({
62
82
  logger.info(
63
83
  `Markdown file changed: ${filePath}; Re-training TF-IDF...`
64
84
  );
65
- await trainModel(paths);
85
+ await trainModel(paths, schema);
66
86
  } else {
67
87
  return;
68
88
  }
69
89
  });
70
90
  },
91
+ "astro:build:setup": async ({ logger }) => {
92
+ logger.info("relatinator build:setup");
93
+
94
+ logger.info("Training TF-IDF on existing data...");
95
+ await trainModel(paths, schema);
96
+ logger.info("Training done.");
97
+ }
71
98
  },
72
99
  };
73
100
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "astro-relatinator",
3
- "version": "1.0.3",
3
+ "version": "1.1.0",
4
4
  "description": "Find related content in your Astro website or app",
5
5
  "author": "Darko Bozhinovski <dbozhinovski@gmail.com>",
6
6
  "license": "MIT",
@@ -13,7 +13,8 @@
13
13
  "words",
14
14
  "posts",
15
15
  "articles",
16
- "static"
16
+ "static",
17
+ "astro-integration"
17
18
  ],
18
19
  "files": [
19
20
  "integration.ts"