From ff3688e3aa6055a9acf39ca85c853dc74a24a717 Mon Sep 17 00:00:00 2001 From: taskylizard <75871323+taskylizard@users.noreply.github.com> Date: Sat, 27 Jul 2024 07:30:30 +0000 Subject: [PATCH] feat(api): implement single-page --- .vitepress/routes/single-page.ts | 67 ++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 .vitepress/routes/single-page.ts diff --git a/.vitepress/routes/single-page.ts b/.vitepress/routes/single-page.ts new file mode 100644 index 000000000..335b97de9 --- /dev/null +++ b/.vitepress/routes/single-page.ts @@ -0,0 +1,67 @@ +import { fetcher } from 'itty-fetcher' + +const GITHUB_REPO = 'https://api.github.com/repos/fmhy/FMHYEdit/contents/' +const EXCLUDE_FILES = ['README.md', 'index.md', 'feedback.md', 'posts.md'] +const EXCLUDE_DIRECTORIES = ['posts/'] + +interface File { + name: string + path: string + sha: string + size: number + url: string + html_url: string + git_url: string + download_url: string | null + type: string + _links: { + self: string + git: string + html: string + } +} + +export default defineEventHandler(async () => { + let body = '' + try { + // event.waitUntil(async () => { + const f = fetcher() + + // Fetch the list of files in the repository + const files = await f.get(GITHUB_REPO) + + // Filter out the excluded files and non-markdown files + const markdownFiles = files.filter((file: File) => { + const isExcludedFile = EXCLUDE_FILES.includes(file.name) + const isInExcludedDirectory = EXCLUDE_DIRECTORIES.some((dir) => + file.path.startsWith(dir) + ) + const isMarkdownFile = file.name.endsWith('.md') + + return isMarkdownFile && !isExcludedFile && !isInExcludedDirectory + }) + + // console.info(markdownFiles.map((f) => f.name)) + + // Fetch and concatenate the contents of the markdown files + const contents = await Promise.all( + markdownFiles.map(async (file: File) => { + const content = await f.get(file.download_url) + return content + }) + ) + + body += contents.join('\n\n') + // }) + } catch (error) { + return { + status: 500, + body: `Error fetching markdown files: ${error.message}` + } + } + + return { + status: 200, + body + } +})