fix(landing): bundle legal markdown content

This commit is contained in:
DIYgod
2026-06-22 20:57:58 +08:00
parent cbe4e06733
commit c782efa65e
6 changed files with 60 additions and 34 deletions
+2
View File
@@ -1,3 +1,5 @@
/// <reference types="vite/client" />
/* eslint-disable @typescript-eslint/no-empty-object-type */
/* eslint-disable @typescript-eslint/method-signature-style */
import type { FC, PropsWithChildren } from 'react'
@@ -1,6 +1,7 @@
import type { Metadata } from 'next/types'
import { getMarkdownContent, MarkdownContent } from '~/components/ui/markdown'
import { MarkdownContent } from '~/components/ui/markdown'
import { getMarkdownContent } from '~/components/ui/markdown/get-markdown-content'
import { siteInfo } from '~/constants/site'
export const metadata: Metadata = {
@@ -1,6 +1,7 @@
import type { Metadata } from 'next/types'
import { getMarkdownContent, MarkdownContent } from '~/components/ui/markdown'
import { MarkdownContent } from '~/components/ui/markdown'
import { getMarkdownContent } from '~/components/ui/markdown/get-markdown-content'
import { siteInfo } from '~/constants/site'
export const metadata: Metadata = {
@@ -0,0 +1,35 @@
import rehypeStringify from 'rehype-stringify'
import remarkParse from 'remark-parse'
import remarkRehype from 'remark-rehype'
import { unified } from 'unified'
import privacyPolicyMarkdown from '~/legal/privacy.md?raw'
import termsOfServiceMarkdown from '~/legal/tos.md?raw'
const markdownContentByPath = {
'legal/privacy.md': privacyPolicyMarkdown,
'legal/tos.md': termsOfServiceMarkdown,
} as const
export type MarkdownFilePath = keyof typeof markdownContentByPath
export async function getMarkdownContent(filePath: MarkdownFilePath) {
try {
const fileContents = markdownContentByPath[filePath]
const processedContent = await unified()
.use(remarkParse)
.use(remarkRehype, { allowDangerousHtml: true })
.use(rehypeStringify, { allowDangerousHtml: true })
.process(fileContents)
return {
content: processedContent.toString(),
}
} catch (error) {
console.error('Error reading markdown file:', error)
return {
content: '<p>Error loading content. Please try again later.</p>',
}
}
}
@@ -1,11 +1,3 @@
import fs from 'node:fs'
import { join } from 'pathe'
import rehypeStringify from 'rehype-stringify'
import remarkParse from 'remark-parse'
import remarkRehype from 'remark-rehype'
import { unified } from 'unified'
interface MarkdownContentProps {
content: string
}
@@ -25,27 +17,3 @@ export function MarkdownContent({ content }: MarkdownContentProps) {
</div>
)
}
// Utility function to read and process markdown files
export async function getMarkdownContent(filePath: string) {
try {
const fullPath = join(process.cwd(), 'src', filePath)
const fileContents = fs.readFileSync(fullPath, 'utf8')
// Process markdown to HTML using unified
const processedContent = await unified()
.use(remarkParse)
.use(remarkRehype, { allowDangerousHtml: true })
.use(rehypeStringify, { allowDangerousHtml: true })
.process(fileContents)
return {
content: processedContent.toString(),
}
} catch (error) {
console.error('Error reading markdown file:', error)
return {
content: '<p>Error loading content. Please try again later.</p>',
}
}
}
@@ -0,0 +1,19 @@
import { describe, expect, it } from 'vitest'
import { getMarkdownContent } from './get-markdown-content'
describe('getMarkdownContent', () => {
it('renders the privacy policy markdown content', async () => {
const { content } = await getMarkdownContent('legal/privacy.md')
expect(content).toContain('<h1>Privacy Policy</h1>')
expect(content).not.toContain('Error loading content')
})
it('renders the terms of service markdown content', async () => {
const { content } = await getMarkdownContent('legal/tos.md')
expect(content).toContain('<h1>Terms of Service</h1>')
expect(content).not.toContain('Error loading content')
})
})