このコミットが含まれているのは:
@@ -8,13 +8,15 @@ import { useUITranslation } from '@/lib/useUITranslation'
|
||||
import PrefetchLink from '@/components/PrefetchLink'
|
||||
import { wikiKeys } from '@/lib/queryKeys'
|
||||
import remarkWikiAutoLink from '@/lib/remark-wiki-autolink'
|
||||
import { fetchWikiPages } from '@/lib/wiki'
|
||||
import { fetchWikiAutolinkCandidates } from '@/lib/wiki'
|
||||
import { ainuMDXComponents, LocalisedDocument } from '@/mdx-components'
|
||||
|
||||
import type { FC } from 'react'
|
||||
import type { Components } from 'react-markdown'
|
||||
|
||||
type Props = { title: string
|
||||
body?: string }
|
||||
body?: string
|
||||
language?: string }
|
||||
|
||||
const mdComponents = { a: (({ href, children }) => (
|
||||
['/', '.'].some (e => href?.startsWith (e))
|
||||
@@ -27,23 +29,32 @@ const mdComponents = { a: (({ href, children }) => (
|
||||
</a>))) } as const satisfies Components
|
||||
|
||||
|
||||
const WikiBody: FC<Props> = ({ title, body }) => {
|
||||
const WikiBody: FC<Props> = ({ title, body, language = 'und' }) => {
|
||||
const { message } = useUITranslation ('wiki')
|
||||
const locale = useLocale ()
|
||||
const missingPage = message ('missingPage', {
|
||||
url: `/wiki/new?title=${ encodeURIComponent (title) }` })
|
||||
const contentLanguage = body ? language : missingPage.language
|
||||
const matchLanguage = contentLanguage === 'ain-Kana' ? 'ain' : contentLanguage
|
||||
const { data } = useQuery ({
|
||||
enabled: Boolean (body),
|
||||
queryKey: wikiKeys.index ({ }, locale),
|
||||
queryFn: () => fetchWikiPages ({ }, locale) })
|
||||
const pageNames = (data ?? []).map (page => page.title).sort ((a, b) => b.length - a.length)
|
||||
queryKey: wikiKeys.autolinkCandidates (matchLanguage, locale),
|
||||
queryFn: () => fetchWikiAutolinkCandidates (matchLanguage, locale) })
|
||||
|
||||
const remarkPlugins = useMemo (
|
||||
() => [() => remarkWikiAutoLink (pageNames), remarkGFM], [pageNames])
|
||||
() => [() => remarkWikiAutoLink (data ?? [], matchLanguage), remarkGFM],
|
||||
[data, matchLanguage])
|
||||
const components = {
|
||||
...((contentLanguage === 'ain' || contentLanguage === 'ain-Kana')
|
||||
? ainuMDXComponents : { }),
|
||||
...mdComponents }
|
||||
|
||||
return (
|
||||
<ReactMarkdown components={mdComponents} remarkPlugins={remarkPlugins}>
|
||||
{body || message ('missingPage', {
|
||||
url: `/wiki/new?title=${ encodeURIComponent (title) }` }).text}
|
||||
</ReactMarkdown>)
|
||||
<LocalisedDocument language={contentLanguage}>
|
||||
<ReactMarkdown components={components} remarkPlugins={remarkPlugins}>
|
||||
{body || missingPage.text}
|
||||
</ReactMarkdown>
|
||||
</LocalisedDocument>)
|
||||
}
|
||||
|
||||
export default WikiBody
|
||||
|
||||
新しいイシューから参照
ユーザーをブロックする