api/routes/single-page.ts

159a9802ac8da62e21c8272cbedff668d4a41512
wotaku/api/routes/single-page.ts history · blame · raw

101 lines · 3158 bytes

  1/**
  2 *  Copyright (c) 2024 taskylizard
  3 *
  4 *  Licensed under the Apache License, Version 2.0 (the "License");
  5 *  you may not use this file except in compliance with the License.
  6 *  You may obtain a copy of the License at
  7 *
  8 *  http://www.apache.org/licenses/LICENSE-2.0
  9 *
 10 *  Unless required by applicable law or agreed to in writing, software
 11 *  distributed under the License is distributed on an "AS IS" BASIS,
 12 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13 *  See the License for the specific language governing permissions and
 14 *  limitations under the License.
 15 */
 16/**
 17 *  Copyright (c) taskylizard. All rights reserved.
 18 *
 19 *  Licensed under the Apache License, Version 2.0 (the "License");
 20 *  you may not use this file except in compliance with the License.
 21 *  You may obtain a copy of the License at
 22 *
 23 *  http://www.apache.org/licenses/LICENSE-2.0
 24 *
 25 *  Unless required by applicable law or agreed to in writing, software
 26 *  distributed under the License is distributed on an "AS IS" BASIS,
 27 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 28 *  See the License for the specific language governing permissions and
 29 *  limitations under the License.
 30 */
 31
 32import { fetcher } from 'itty-fetcher'
 33
 34// Look inside tbe docs directory
 35const GITHUB_REPO =
 36  'https://api.github.com/repos/wotakumoe/wotaku/contents/docs/'
 37const EXCLUDE_FILES = ['README.md', 'index.md', 'credits.md', 'faq.md']
 38const EXCLUDE_DIRECTORIES = ['glossary/', 'guides/', 'torrenting/']
 39
 40interface File {
 41  name: string
 42  path: string
 43  sha: string
 44  size: number
 45  url: string
 46  html_url: string
 47  git_url: string
 48  download_url: string | null
 49  type: string
 50  _links: {
 51    self: string
 52    git: string
 53    html: string
 54  }
 55}
 56
 57export default defineEventHandler(async (event) => {
 58  let body = '<!-- This is autogenerated content, do not edit manually. -->\n'
 59  const f = fetcher({
 60    headers: {
 61      'User-Agent': 'taskylizard'
 62    }
 63  })
 64
 65  try {
 66    // Fetch the list of files in the repository
 67    const files = await f.get<File[]>(GITHUB_REPO)
 68
 69    // Filter out the excluded files and non-markdown files
 70    const markdownFiles = files.filter((file: File) => {
 71      const isExcludedFile = EXCLUDE_FILES.includes(file.name)
 72      const isInExcludedDirectory = EXCLUDE_DIRECTORIES.some((dir) =>
 73        file.path.startsWith(dir)
 74      )
 75      const isMarkdownFile = file.name.endsWith('.md')
 76
 77      return isMarkdownFile && !isExcludedFile && !isInExcludedDirectory
 78    })
 79
 80    // console.info(markdownFiles.map((f) => f.name))
 81
 82    // Fetch and concatenate the contents of the markdown files
 83    const contents = await Promise.all(
 84      markdownFiles.map(async (file: File) => {
 85        const content = await f.get<string>(file.download_url)
 86        return content
 87      })
 88    )
 89
 90    body += contents.join('\n\n')
 91  } catch (error) {
 92    return {
 93      status: 500,
 94      body: `Error fetching markdown files: ${error.message}`
 95    }
 96  }
 97
 98  // biome-ignore lint/correctness/noUndeclaredVariables: <explanation>
 99  appendResponseHeader(event, 'content-type', 'text/markdown;charset=utf-8')
100  return body
101})