From b51440fb4924146e996013c7ce289b44160f91b5 Mon Sep 17 00:00:00 2001 From: taskylizard <75871323+taskylizard@users.noreply.github.com> Date: Sat, 28 Oct 2023 17:34:09 +0530 Subject: [PATCH] add wip loader --- .vitepress/loaders/guides.data.ts | 43 +++++++++++++++++++++++++++++++ guides.md | 16 ++++++++++++ package.json | 1 + 3 files changed, 60 insertions(+) create mode 100644 .vitepress/loaders/guides.data.ts create mode 100644 guides.md diff --git a/.vitepress/loaders/guides.data.ts b/.vitepress/loaders/guides.data.ts new file mode 100644 index 000000000..64d73144b --- /dev/null +++ b/.vitepress/loaders/guides.data.ts @@ -0,0 +1,43 @@ +import { defineLoader } from "vitepress"; + +interface Data { + title?: string; + content?: string; + url?: string; +} +declare const data: Data; +export { data }; + +const page = "https://rentry.co/fmhy-guides/raw"; +const regex = /\* \[([^\]]+)\]\(([^)]+)\)/g; +const rentryRe = /(?<=rentry\.(co|org)).*/; +const guides = new Set(); + +const f = async (url: string) => { + const contents = await (await fetch(url)) + .text() + .catch((error: Error) => console.error(`Failed at ${url}`, error)); + return contents; +}; + +export default defineLoader({ + async load(): Promise { + const contents = await f(page); + let match: any[] | null; + while ((match = regex.exec(contents)) !== null) { + const title = match[1]; + const url = match[2]; + // Fetch rentry guides + if (url.match(rentryRe)) { + const content = await f(url + "/raw"); + guides.add({ title, content }); + } else { + // Everything else can be here + guides.add({ title, url }); + } + } + return Object.fromEntries( + [...guides.entries()].map((entry, index) => [index.toString(), entry]), + ); + }, +}); diff --git a/guides.md b/guides.md new file mode 100644 index 000000000..484a43cab --- /dev/null +++ b/guides.md @@ -0,0 +1,16 @@ +--- +title: Guides +--- + + + diff --git a/package.json b/package.json index dd540d3bf..65d01c205 100644 --- a/package.json +++ b/package.json @@ -1,5 +1,6 @@ { "name": "fmhy", + "type": "module", "scripts": { "docs:dev": "vitepress dev", "docs:build": "vitepress build",