|
1 | | -import { JSX } from "react"; |
| 1 | +"use client"; |
| 2 | + |
| 3 | +import { createContext, JSX, ReactNode, useContext } from "react"; |
2 | 4 | import { ExtraProps } from "react-markdown"; |
3 | 5 | import { onlyText } from "react-children-utilities"; |
| 6 | +import { LangId, TermDefinition } from "@/lib/docs"; |
| 7 | +import Link from "next/link"; |
| 8 | +import { StyledMarkdown } from "./markdown"; |
| 9 | + |
| 10 | +const TermDefinitionContext = createContext<{ |
| 11 | + lang: LangId; |
| 12 | + termDefinitions: TermDefinition[]; |
| 13 | +} | null>(null); |
| 14 | +export function TermDefinitionProvider({ |
| 15 | + lang, |
| 16 | + termDefinitions, |
| 17 | + children, |
| 18 | +}: { |
| 19 | + lang: LangId; |
| 20 | + termDefinitions: TermDefinition[]; |
| 21 | + children: ReactNode; |
| 22 | +}) { |
| 23 | + return ( |
| 24 | + <TermDefinitionContext.Provider value={{ lang, termDefinitions }}> |
| 25 | + {children} |
| 26 | + </TermDefinitionContext.Provider> |
| 27 | + ); |
| 28 | +} |
4 | 29 |
|
5 | 30 | /** |
6 | 31 | * https://github.com/ut-code/utcode-learn/blob/main/src/components/Term/index.tsx をもとに独自実装 |
7 | 32 | * Copyright (c) 2023 ut.code(); |
8 | 33 | */ |
9 | 34 | export default function Term(props: JSX.IntrinsicElements["q"] & ExtraProps) { |
10 | | - // 動作確認用 |
11 | | - return <>[{props.children} → term:{onlyText(props.children)}]</>; |
| 35 | + // termDefinitionの取得がasync関数であり、clientコンポーネントから直接取得できないので、 |
| 36 | + // @docs/lang/pageId/page.tsx で取得したものをcontextに渡してそれを取得する |
| 37 | + const { lang, termDefinitions } = useContext(TermDefinitionContext) ?? {}; |
| 38 | + |
| 39 | + if (!termDefinitions) { |
| 40 | + return props.children; |
| 41 | + } |
12 | 42 |
|
13 | | - // const term = props.id |
14 | | - // ? terms.find((term) => term.id === props.id) |
15 | | - // : terms.find( |
16 | | - // (term) => |
17 | | - // term.name === onlyText(props.children) || |
18 | | - // term.aliases.includes(onlyText(props.children)), |
19 | | - // ); |
20 | | - // if (!term) |
21 | | - // throw new Error( |
22 | | - // `${props.id ? props.id : onlyText(props.children)}という用語は定義されていません`, |
23 | | - // ); |
| 43 | + const termText = onlyText(props.children); |
| 44 | + const term = termDefinitions.find((t) => t.term.includes(termText)); |
| 45 | + if (!term) { |
| 46 | + console.error(`'${termText}'という用語は定義されていません`); |
| 47 | + return props.children; |
| 48 | + } |
24 | 49 |
|
25 | | - // const wrap = (content: JSX.Element) => { |
| 50 | + // [{props.children} → term:{onlyText(props.children)}] |
26 | 51 |
|
27 | | - // return ( |
28 | | - // <Tippy |
29 | | - // theme="material" |
30 | | - // interactive={shouldLinkToReferencePage} |
31 | | - // appendTo={window.document.body} |
32 | | - // content={ |
33 | | - // <div className={styles.tooltipContent}> |
34 | | - // <header className={styles.tooltipContentHeader}>{term.name}</header> |
35 | | - // <div>{term.definition}</div> |
36 | | - // {shouldLinkToReferencePage && ( |
37 | | - // <Link className={styles.tooltipLink} to={term.referencePage}> |
38 | | - // <span>{referencePageTitle} へ</span> |
39 | | - // <MdArrowForward size="1.2rem" /> |
40 | | - // </Link> |
41 | | - // )} |
42 | | - // </div> |
43 | | - // } |
44 | | - // > |
45 | | - // {content} |
46 | | - // </Tippy> |
47 | | - // ); |
48 | | - // }; |
| 52 | + return ( |
| 53 | + <span className="tooltip tooltip-primary"> |
| 54 | + <span className="tooltip-content bg-primary-content/60 border border-primary text-base-content backdrop-blur-xs"> |
| 55 | + <span className="breadcrumbs text-info text-center text-base wrap-none mb-4"> |
| 56 | + <ul className=""> |
| 57 | + <li>{term.page}</li> |
| 58 | + <li>{term.title}</li> |
| 59 | + </ul> |
| 60 | + </span> |
| 61 | + <StyledMarkdown content={term.rawContentWithoutCode} /> |
| 62 | + </span> |
| 63 | + <Link |
| 64 | + href={`/${lang}/${term.page}#${term.id}`} |
| 65 | + className="link link-info decoration-dotted underline-offset-[0.2rem]" |
| 66 | + > |
| 67 | + {props.children} |
| 68 | + </Link> |
| 69 | + </span> |
| 70 | + ); |
49 | 71 | } |
0 commit comments