api/routes/single-page.ts
77 lines · 2212 bytes
1/**
2* All Rights Reserved
3*
4* Copyright (c) 2025 taskylizard
5*
6* All rights reserved. This code and its associated files may not be copied, modified, distributed, sublicensed, or used in any form, in whole or in part, without prior written permission from the copyright holder.
7*/
8import { fetcher } from 'itty-fetcher'
9
10// Look inside tbe docs directory
11const GITHUB_REPO =
12 'https://api.github.com/repos/wotakumoe/wotaku/contents/docs/'
13const EXCLUDE_FILES = ['README.md', 'index.md', 'credits.md', 'faq.md']
14const EXCLUDE_DIRECTORIES = ['glossary/', 'guides/', 'torrenting/']
15
16interface File {
17 name: string
18 path: string
19 sha: string
20 size: number
21 url: string
22 html_url: string
23 git_url: string
24 download_url: string | null
25 type: string
26 _links: {
27 self: string
28 git: string
29 html: string
30 }
31}
32
33export default defineEventHandler(async (event) => {
34 let body = '<!-- This is autogenerated content, do not edit manually. -->\n'
35 const f = fetcher({
36 headers: {
37 'User-Agent': 'taskylizard'
38 }
39 })
40
41 try {
42 // Fetch the list of files in the repository
43 const files = await f.get<File[]>(GITHUB_REPO)
44
45 // Filter out the excluded files and non-markdown files
46 const markdownFiles = files.filter((file: File) => {
47 const isExcludedFile = EXCLUDE_FILES.includes(file.name)
48 const isInExcludedDirectory = EXCLUDE_DIRECTORIES.some((dir) =>
49 file.path.startsWith(dir)
50 )
51 const isMarkdownFile = file.name.endsWith('.md')
52
53 return isMarkdownFile && !isExcludedFile && !isInExcludedDirectory
54 })
55
56 // console.info(markdownFiles.map((f) => f.name))
57
58 // Fetch and concatenate the contents of the markdown files
59 const contents = await Promise.all(
60 markdownFiles.map(async (file: File) => {
61 const content = await f.get<string>(file.download_url)
62 return content
63 })
64 )
65
66 body += contents.join('\n\n')
67 } catch (error) {
68 return {
69 status: 500,
70 body: `Error fetching markdown files: ${error.message}`
71 }
72 }
73
74 // biome-ignore lint/correctness/noUndeclaredVariables: <explanation>
75 appendResponseHeader(event, 'content-type', 'text/markdown;charset=utf-8')
76 return body
77})