Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add related articles #207

Draft
wants to merge 12 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
105 changes: 105 additions & 0 deletions astro.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,115 @@ import tailwind from "@astrojs/tailwind";
import sectionize from "@hbsnow/rehype-sectionize";
import icon from "astro-icon";
import { defineConfig } from "astro/config";
import fs from "fs";
import path from "path";

const articleFiles = new Map();

const articleObjs = [];

export var articleMatches = [];

function readDirectory(directory, articleDir) {
fs.readdirSync(directory).forEach((file) => {
const Absolute = path.join(directory, file);
let fileName = file.toString();

if (fileName.indexOf(".") >= 0) {
fileName = fileName.substring(0, fileName.indexOf("."));
}

if (fs.statSync(Absolute).isDirectory())
return readDirectory(Absolute, fileName);
else if (file.endsWith(".mdx") || file.endsWith(".md"))
articleFiles.set(fileName == "index" ? articleDir : fileName, Absolute);
});
}

function buildArticleJson() {
articleFiles.forEach((value, key) => {
const obj = {
slug: key,
content: fs.readFileSync(path.join(value), "utf8").substring(0, 100),
};
articleObjs.push(obj);
});

console.log(`Read ${articleObjs.length} articles.`);
}

// Custom function you want to run
async function addEmbeddedArticles() {
console.log("Vectorizing articles...");
// Add your logic here
// For example, generating data files, cleaning directories, etc.

const directory = "./src/content/articles"; // Replace with your directory path

readDirectory(directory, null);
buildArticleJson();

try {
const externalResponse = await fetch(
"https://related-articles.andrei-023.workers.dev/add_articles",
{
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ articles: articleObjs }),
},
);

console.log("Uploaded articles");
} catch (error) {
console.error("Error sending files:", error);
}
}

async function getArticleMatches() {
try {
const res = await fetch(
"https://related-articles.andrei-023.workers.dev/match_articles",
{
method: "GET",
headers: { "Content-Type": "application/json" },
},
);

const body = await res.json();

fs.writeFile(
"src/data/matchedArticles.json",
JSON.stringify(body),
(err) => {
if (err) {
console.error(err);
return;
}
console.log("JSON data saved to data.json");
},
);
} catch (error) {
console.error("Error sending files:", error);
}
}

// Custom Astro integration
function buildStart() {
return {
name: "my-build-start",
hooks: {
"astro:build:start": async () => {
await addEmbeddedArticles();
await getArticleMatches();
},
},
};
}

export default defineConfig({
site: "https://rockthejvm.com",
integrations: [
buildStart(),
icon({
include: {
"fa6-brands": [
Expand Down
21 changes: 21 additions & 0 deletions components.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"$schema": "https://ui.shadcn.com/schema.json",
"style": "default",
"rsc": false,
"tsx": true,
"tailwind": {
"config": "tailwind.config.js",
"css": "src/styles/base.css",
"baseColor": "neutral",
"cssVariables": true,
"prefix": ""
},
"aliases": {
"components": "@assets/components",
"utils": "@assets/lib/utils",
"ui": "@assets/components/ui",
"lib": "@assets/lib",
"hooks": "@assets/hooks"
},
"iconLibrary": "lucide"
}
9 changes: 8 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,28 @@
"@iconify-json/fa6-brands": "^1.2.0",
"@iconify-json/fa6-solid": "^1.2.0",
"@iconify-json/heroicons": "^1.2.0",
"@radix-ui/react-slot": "^1.1.1",
"@tailwindcss/aspect-ratio": "^0.4.2",
"@tailwindcss/forms": "^0.5.9",
"@types/react-dom": "^18.3.0",
"astro": "^4.15.6",
"astro-embed": "^0.7.2",
"astro-icon": "^1.1.1",
"class-variance-authority": "^0.7.1",
"clsx": "^2.1.1",
"effect": "^3.10.13",
"embla-carousel-react": "^8.5.1",
"fp-ts": "^2.16.9",
"lucide-react": "^0.469.0",
"mdast-util-to-string": "^4.0.0",
"react": "^18.3.1",
"react-dom": "^18.3.1",
"react-share": "^5.1.0",
"reading-time": "^1.5.0",
"sharp": "^0.33.5",
"tailwindcss": "^3.4.11"
"tailwind-merge": "^2.6.0",
"tailwindcss": "^3.4.11",
"tailwindcss-animate": "^1.0.7"
},
"devDependencies": {
"@cloudflare/workers-types": "^4.20241022.0",
Expand Down
107 changes: 107 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading