import { useEffect, useMemo, useState } from 'react' import ReactMarkdown from 'react-markdown' import remarkGFM from 'remark-gfm' import PrefetchLink from '@/components/PrefetchLink' import SectionTitle from '@/components/common/SectionTitle' import SubsectionTitle from '@/components/common/SubsectionTitle' import { apiGet } from '@/lib/api' import remarkWikiAutoLink from '@/lib/remark-wiki-autolink' import type { FC } from 'react' import type { Components } from 'react-markdown' import type { WikiPage } from '@/types' type Props = { title: string body?: string } const mdComponents = { h1: ({ children }) => {children}, h2: ({ children }) => {children}, ol: ({ children }) =>
    {children}
, ul: ({ children }) => , a: (({ href, children }) => ( ['/', '.'].some (e => href?.startsWith (e)) ? {children} : ( {children} ))) } as const satisfies Components export default (({ title, body }: Props) => { const [pageNames, setPageNames] = useState ([]) const remarkPlugins = useMemo ( () => [() => remarkWikiAutoLink (pageNames), remarkGFM], [pageNames]) useEffect (() => { void (async () => { try { const data = await apiGet ('/wiki') setPageNames (data.map (page => page.title).sort ((a, b) => b.length - a.length)) } catch { setPageNames ([]) } }) () }, []) return ( {body || `このページは存在しません。[新規作成してください](/wiki/new?title=${ encodeURIComponent (title) })。`} ) }) satisfies FC