|
1 | 1 | import { readFile, readdir } from 'node:fs/promises' |
2 | 2 | import { join } from 'node:path' |
3 | 3 | import { marked } from 'marked' |
| 4 | +import { notFound } from 'next/navigation' |
4 | 5 |
|
5 | | -const LAB_DIR = join(process.cwd(), 'content/lab') |
| 6 | +export type LegalDoc = 'privacy' | 'terms' |
6 | 7 |
|
7 | | -// GitHub Pages serves /lab/x/privacy from privacy.html, but the dev server only accepts clean URLs. |
8 | | -const toUrlPath = (file: string): string[] => |
9 | | - process.env.NODE_ENV === 'development' |
10 | | - ? file.replace(/(\/index)?\.md$/, '').split('/') |
11 | | - : file.replace(/\.md$/, '.html').split('/') |
| 8 | +const LEGAL_DIR = join(process.cwd(), 'content/legal') |
12 | 9 |
|
13 | | -export const listLabPagePaths = async (): Promise<string[][]> => { |
14 | | - const files = await readdir(LAB_DIR, { recursive: true }) |
15 | | - return files.filter((file) => file.endsWith('.md')).map(toUrlPath) |
| 10 | +const INDEX_TITLES: Record<LegalDoc, string> = { |
| 11 | + privacy: 'Privacy Policies', |
| 12 | + terms: 'Terms of Service', |
16 | 13 | } |
17 | 14 |
|
18 | | -export const readLabPage = ([project, page = 'index']: string[]): Promise<string | null> => |
19 | | - readFile(join(LAB_DIR, project, `${page.replace(/\.html$/, '')}.md`), 'utf8').catch(() => null) |
| 15 | +// GitHub Pages serves /x from x.html and /x/ from x/index.html, but the dev server only accepts clean URLs. |
| 16 | +const toStaticPath = (project?: string): string[] => { |
| 17 | + if (process.env.NODE_ENV === 'development') return project ? [project] : [] |
| 18 | + return [`${project ?? 'index'}.html`] |
| 19 | +} |
| 20 | + |
| 21 | +const projectFromPath = (path: string[] = []): string | undefined => { |
| 22 | + const page = path[0]?.replace(/\.html$/, '') |
| 23 | + return page === 'index' ? undefined : page |
| 24 | +} |
| 25 | + |
| 26 | +const readDoc = (project: string, doc: LegalDoc): Promise<string | null> => |
| 27 | + readFile(join(LEGAL_DIR, project, `${doc}.md`), 'utf8').catch(() => null) |
| 28 | + |
| 29 | +const listProjects = async (doc: LegalDoc): Promise<string[]> => { |
| 30 | + const files = await readdir(LEGAL_DIR, { recursive: true }) |
| 31 | + return files.filter((file) => file.endsWith(`/${doc}.md`)).map((file) => file.split('/')[0]) |
| 32 | +} |
| 33 | + |
| 34 | +const titleOf = (markdown: string): string => markdown.match(/^# (.+)$/m)?.[1] ?? '' |
| 35 | + |
| 36 | +const buildIndex = async (doc: LegalDoc): Promise<string> => { |
| 37 | + const projects = await listProjects(doc) |
| 38 | + const links = await Promise.all( |
| 39 | + projects.map(async (project) => { |
| 40 | + const markdown = await readDoc(project, doc) |
| 41 | + return `- [${titleOf(markdown ?? '')}](/${doc}/${project})` |
| 42 | + }) |
| 43 | + ) |
| 44 | + return `# ${INDEX_TITLES[doc]}\n\n${links.join('\n')}\n` |
| 45 | +} |
| 46 | + |
| 47 | +export const listLegalPagePaths = async (doc: LegalDoc): Promise<string[][]> => { |
| 48 | + const projects = await listProjects(doc) |
| 49 | + return [toStaticPath(), ...projects.map(toStaticPath)] |
| 50 | +} |
| 51 | + |
| 52 | +export const readLegalPage = (doc: LegalDoc, path?: string[]): Promise<string | null> => { |
| 53 | + const project = projectFromPath(path) |
| 54 | + return project ? readDoc(project, doc) : buildIndex(doc) |
| 55 | +} |
20 | 56 |
|
21 | | -export const renderLabPage = (markdown: string): string => { |
22 | | - const title = markdown.match(/^# (.+)$/m)?.[1] ?? '' |
| 57 | +const renderLegalPage = (markdown: string): string => { |
23 | 58 | const body = marked.parse(markdown, { async: false }) |
24 | 59 | return `<!doctype html> |
25 | 60 | <html lang="en"> |
26 | 61 | <head> |
27 | 62 | <meta charset="utf-8"> |
28 | 63 | <meta name="viewport" content="width=device-width, initial-scale=1"> |
29 | | -<title>${title}</title> |
| 64 | +<title>${titleOf(markdown)}</title> |
30 | 65 | <link rel="icon" href="/logo.png"> |
31 | 66 | </head> |
32 | 67 | <body> |
33 | 68 | ${body}</body> |
34 | 69 | </html> |
35 | 70 | ` |
36 | 71 | } |
| 72 | + |
| 73 | +// A route handler skips the root layout, so the page has no site shell. |
| 74 | +export const legalPageResponse = (markdown: string | null): Response => { |
| 75 | + if (markdown === null) notFound() |
| 76 | + |
| 77 | + return new Response(renderLegalPage(markdown), { |
| 78 | + headers: { 'Content-Type': 'text/html; charset=utf-8' }, |
| 79 | + }) |
| 80 | +} |
0 commit comments