ぼざクリタグ広場 https://hub.nizika.monster
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

49 lines
1.9 KiB

  1. import { useQuery } from '@tanstack/react-query'
  2. import { useMemo } from 'react'
  3. import ReactMarkdown from 'react-markdown'
  4. import remarkGFM from 'remark-gfm'
  5. import PrefetchLink from '@/components/PrefetchLink'
  6. import SectionTitle from '@/components/common/SectionTitle'
  7. import SubsectionTitle from '@/components/common/SubsectionTitle'
  8. import { wikiKeys } from '@/lib/queryKeys'
  9. import remarkWikiAutoLink from '@/lib/remark-wiki-autolink'
  10. import { fetchWikiPages } from '@/lib/wiki'
  11. import type { FC } from 'react'
  12. import type { Components } from 'react-markdown'
  13. type Props = { title: string
  14. body?: string }
  15. const mdComponents = { h1: ({ children }) => <SectionTitle>{children}</SectionTitle>,
  16. h2: ({ children }) => <SubsectionTitle>{children}</SubsectionTitle>,
  17. ol: ({ children }) => <ol className="list-decimal pl-6">{children}</ol>,
  18. ul: ({ children }) => <ul className="list-disc pl-6">{children}</ul>,
  19. a: (({ href, children }) => (
  20. ['/', '.'].some (e => href?.startsWith (e))
  21. ? <PrefetchLink to={href!}>{children}</PrefetchLink>
  22. : (
  23. <a href={href}
  24. target="_blank"
  25. rel="noopener noreferrer">
  26. {children}
  27. </a>))) } as const satisfies Components
  28. export default (({ title, body }: Props) => {
  29. const { data } = useQuery ({
  30. enabled: Boolean (body),
  31. queryKey: wikiKeys.index ({ }),
  32. queryFn: () => fetchWikiPages ({ }) })
  33. const pageNames = (data ?? []).map (page => page.title).sort ((a, b) => b.length - a.length)
  34. const remarkPlugins = useMemo (
  35. () => [() => remarkWikiAutoLink (pageNames), remarkGFM], [pageNames])
  36. return (
  37. <ReactMarkdown components={mdComponents} remarkPlugins={remarkPlugins}>
  38. {body || `このページは存在しません。[新規作成してください](/wiki/new?title=${ encodeURIComponent (title) })。`}
  39. </ReactMarkdown>)
  40. }) satisfies FC<Props>