SPB Git

Repository Public

Markdown.tsx 2.3 KB 43 lines tsx

1import "katex/dist/katex.min.css";2import ReactMarkdown from "react-markdown";3import rehypeHighlight from "rehype-highlight";4import rehypeKatex from "rehype-katex";5import remarkGfm from "remark-gfm";6import remarkMath from "remark-math";78/**9 * Renders course Markdown (lab instructions, narration script — imported or written in the studio) as inert10 * HTML. The same component draws the public page and the studio's live preview.11 *12 *  - raw HTML in the source is never interpreted (react-markdown builds React elements; `skipHtml` drops the rest);13 *  - links are limited to http(s), open in a new tab and carry no referrer; images are dropped;14 *  - code blocks are highlighted by class names only (highlight.js grammars, no script in the output);15 *  - mathematics is typeset by KaTeX from `$$ … $$` (inline or on its own lines). A single dollar sign stays a16 *    dollar sign, so prices in a text are left alone. KaTeX runs with `trust` off: no \href, no \includegraphics.17 *  - tables scroll inside a focusable, labelled region.18 */19export function Markdown({ source, headingOffset = 1 }: { source: string; headingOffset?: 0 | 1 | 2 }) {20  const heading = (level: number) => {21    const Tag = `h${Math.min(6, level + headingOffset)}` as "h2";22    return function Heading({ children }: { children?: React.ReactNode }) { return <Tag>{children}</Tag>; };23  };24  return (25    <div className="prose">26      <ReactMarkdown27        remarkPlugins={[remarkGfm, [remarkMath, { singleDollarTextMath: false }]]}28        rehypePlugins={[[rehypeKatex, { strict: "ignore", throwOnError: false, trust: false, maxExpand: 200, maxSize: 20 }], [rehypeHighlight, { detect: false, ignoreMissing: true }]]}29        skipHtml30        components={{31          h1: heading(1), h2: heading(2), h3: heading(3), h4: heading(4),32          a: ({ href, children }) => (/^https?:\/\//.test(href ?? "") ? <a href={href} target="_blank" rel="noopener noreferrer">{children}</a> : <span>{children}</span>),33          table: ({ children }) => <div className="table-wrap" tabIndex={0} role="group" aria-label="Tableau : faites défiler horizontalement si nécessaire"><table>{children}</table></div>,34          pre: ({ children }) => <pre tabIndex={0}>{children}</pre>,35          img: () => null,36        }}37      >38        {source}39      </ReactMarkdown>40    </div>41  );42}43
Files main
Menu

Theme