2023-02-06 06:03:33 -05:00
|
|
|
import Link from "next/link";
|
|
|
|
import ReactMarkdown from "react-markdown";
|
|
|
|
import rehypeHighlight from "rehype-highlight";
|
|
|
|
import rehypeRaw from "rehype-raw";
|
|
|
|
import rehypeSlug from "rehype-slug";
|
|
|
|
import remarkGfm from "remark-gfm";
|
2023-02-09 04:09:15 -05:00
|
|
|
import WikiPage from "../../interfaces/WikiPage";
|
2023-02-06 06:03:33 -05:00
|
|
|
|
|
|
|
interface RenderMarkdownProps {
|
|
|
|
children: string;
|
2023-02-09 04:09:15 -05:00
|
|
|
page: WikiPage;
|
2023-02-06 06:03:33 -05:00
|
|
|
}
|
|
|
|
|
2023-02-09 04:09:15 -05:00
|
|
|
export default function RenderMarkdown({
|
|
|
|
children,
|
|
|
|
page,
|
|
|
|
}: RenderMarkdownProps) {
|
2023-02-06 06:03:33 -05:00
|
|
|
return (
|
|
|
|
<ReactMarkdown
|
|
|
|
remarkPlugins={[remarkGfm]}
|
|
|
|
rehypePlugins={[rehypeRaw, rehypeHighlight, rehypeSlug]}
|
|
|
|
components={{
|
|
|
|
a: ({ node, ...props }) => {
|
2023-02-07 04:49:06 -05:00
|
|
|
// if the link is internal, reformat it; if it ends with a slash, do not apply this
|
|
|
|
let href = props.href as string;
|
|
|
|
if (!href.endsWith("/") && !href.startsWith("http")) {
|
|
|
|
if (href.startsWith("/wiki/")) {
|
2023-02-09 04:09:15 -05:00
|
|
|
href = `/wiki/${page.language}${href.slice(5)}`;
|
2023-02-07 04:49:06 -05:00
|
|
|
} else {
|
|
|
|
// if single relative
|
2023-02-09 04:09:15 -05:00
|
|
|
href = `/wiki/${page.language}/${page.path}/${href}`;
|
2023-02-07 04:49:06 -05:00
|
|
|
}
|
|
|
|
}
|
2023-02-06 06:03:33 -05:00
|
|
|
return (
|
2023-02-07 04:49:06 -05:00
|
|
|
<Link legacyBehavior href={href as string}>
|
|
|
|
<a>{props.children ? props.children[0] : href}</a>
|
2023-02-06 06:03:33 -05:00
|
|
|
</Link>
|
|
|
|
);
|
|
|
|
},
|
2023-02-07 04:49:06 -05:00
|
|
|
img: ({ node, ...props }) => {
|
|
|
|
// if image is internal (relative), prefix it with the current page's path
|
|
|
|
let src = props.src as string;
|
|
|
|
if (!src.startsWith("http") && !src.startsWith("/")) {
|
2023-02-09 04:09:15 -05:00
|
|
|
src = `/img/wiki/${page.path}/${src}`;
|
2023-02-07 04:49:06 -05:00
|
|
|
}
|
|
|
|
return (
|
|
|
|
<div className="flex w-full flex-col items-center justify-center">
|
|
|
|
<img
|
|
|
|
className="mb-2"
|
|
|
|
src={src}
|
|
|
|
alt={props.alt as string}
|
|
|
|
title={props.title as string}
|
|
|
|
/>
|
|
|
|
<p> {props.title as string} </p>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
},
|
2023-02-06 06:03:33 -05:00
|
|
|
}}
|
|
|
|
>
|
2023-02-09 04:09:15 -05:00
|
|
|
{children}
|
2023-02-06 06:03:33 -05:00
|
|
|
</ReactMarkdown>
|
|
|
|
);
|
|
|
|
}
|