astro-relatinator 1.0.4 → 1.2.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 +69 -0
- package/integration.ts +27 -7
- package/package.json +3 -2
package/README.md
CHANGED
|
@@ -3,3 +3,72 @@
|
|
|
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"], // path yo your content files
|
|
32
|
+
schema: ["title", "descriptions", "tags"], // frontmatter fields to use for training
|
|
33
|
+
}),
|
|
34
|
+
// ...
|
|
35
|
+
],
|
|
36
|
+
});
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
__Note:__ the fields you specify in the `schema` array must be present in the frontmatter of the files you're training on. If they're not, the integration will throw an error or lead to unexpected results.
|
|
40
|
+
|
|
41
|
+
## Usage
|
|
42
|
+
|
|
43
|
+
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.
|
|
44
|
+
|
|
45
|
+
### Example:
|
|
46
|
+
|
|
47
|
+
```astro
|
|
48
|
+
---
|
|
49
|
+
import { getCollection } from "astro:content";
|
|
50
|
+
import { find } from "astro-relatinator";
|
|
51
|
+
|
|
52
|
+
const blogEntries = await getCollection("posts"); // assuming your collection is called posts
|
|
53
|
+
// let's say we want to compare against the first blog entry
|
|
54
|
+
const first = blogEntries[0];
|
|
55
|
+
|
|
56
|
+
// Concatenate the same fields we used for training and declare how many related posts we want to find
|
|
57
|
+
const related = find(`${first.frontmatter.title} ${first.frontmatter.description} ${first.frontmatter.tags.join(' ')} ${first.body}`, 3)
|
|
58
|
+
|
|
59
|
+
---
|
|
60
|
+
|
|
61
|
+
<div>
|
|
62
|
+
<!-- render your related posts here -->
|
|
63
|
+
{ related.map((id) => {
|
|
64
|
+
const post = posts.find((post) => post.id === id);
|
|
65
|
+
return (
|
|
66
|
+
<a href={post.slug}>
|
|
67
|
+
<h2>{post.frontmatter.title}</h2>
|
|
68
|
+
<p>{post.frontmatter.description}</p>
|
|
69
|
+
</a>
|
|
70
|
+
);
|
|
71
|
+
}) }
|
|
72
|
+
</div>
|
|
73
|
+
|
|
74
|
+
```
|
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
|
-
|
|
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:
|
|
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,7 +82,7 @@ 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
|
}
|
|
@@ -72,7 +92,7 @@ const relatinatorIntegration = ({
|
|
|
72
92
|
logger.info("relatinator build:setup");
|
|
73
93
|
|
|
74
94
|
logger.info("Training TF-IDF on existing data...");
|
|
75
|
-
await trainModel(paths);
|
|
95
|
+
await trainModel(paths, schema);
|
|
76
96
|
logger.info("Training done.");
|
|
77
97
|
}
|
|
78
98
|
},
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "astro-relatinator",
|
|
3
|
-
"version": "1.0
|
|
3
|
+
"version": "1.2.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"
|