|
1 | 1 | import * as React from 'react'; |
| 2 | +import { useContext } from 'react'; |
2 | 3 | import ReactMarkdown from 'react-markdown'; |
3 | 4 | import remarkMath from 'remark-math' |
4 | 5 | import rehypeKatex from 'rehype-katex' |
5 | 6 | import 'katex/dist/katex.min.css' // `rehype-katex` does not import the CSS for you |
6 | 7 | import gfm from "remark-gfm"; |
| 8 | +import { GameIdContext } from '../app'; |
7 | 9 |
|
8 | | -function Markdown(props) { |
9 | | - const newProps = { |
10 | | - ...props, |
11 | | - remarkPlugins: [...props.remarkPlugins ?? [], remarkMath, gfm], |
12 | | - rehypePlugins: [...props.remarkPlugins ?? [], rehypeKatex], |
13 | | - }; |
14 | | - return ( |
15 | | - <ReactMarkdown {...newProps} className="markdown" /> |
16 | | - ); |
17 | | -} |
| 10 | +export function Markdown(props: any) { |
| 11 | + const gameId = useContext(GameIdContext) |
| 12 | + // Prefix image URLs of the form `` with the game ID |
| 13 | + let children = props?.children |
| 14 | + |
| 15 | + if (typeof children === "string") { |
| 16 | + children = children.replace( |
| 17 | + /!\[([^\]]+)\]\((images\/[^\)]+)\)/g, |
| 18 | + (match, text, url) => `` |
| 19 | + ); |
| 20 | + } |
18 | 21 |
|
19 | | -export default Markdown |
| 22 | + const newProps = { |
| 23 | + ...props, |
| 24 | + children, |
| 25 | + remarkPlugins: [...props.remarkPlugins ?? [], remarkMath, gfm], |
| 26 | + rehypePlugins: [...props.remarkPlugins ?? [], rehypeKatex], |
| 27 | + }; |
| 28 | + return ( |
| 29 | + <ReactMarkdown {...newProps} className="markdown" /> |
| 30 | + ); |
| 31 | +} |
0 commit comments