import HorizontalLine from "components/HorizontalLine"; import Img, { getAssetURL, ImageQuality } from "components/Img"; import InsetBox from "components/InsetBox"; import LightBox from "components/LightBox"; import ToolTip from "components/ToolTip"; import { useAppLayout } from "contexts/AppLayoutContext"; import Markdown from "markdown-to-jsx"; import { slugify } from "queries/helpers"; import React, { useState } from "react"; type ScenBreakProps = { className?: string; text: string; }; export default function Markdawn(props: ScenBreakProps): JSX.Element { const appLayout = useAppLayout(); const text = preprocessMarkDawn(props.text); const [lightboxOpen, setLightboxOpen] = useState(false); const [lightboxImages, setLightboxImages] = useState([""]); const [lightboxIndex, setLightboxIndex] = useState(0); if (text) { return ( <> { return (

{props.children}

); }, }, h2: { component: (props: { id: string; style: React.CSSProperties; children: React.ReactNode; }) => { return (

{props.children}

); }, }, h3: { component: (props: { id: string; style: React.CSSProperties; children: React.ReactNode; }) => { return (

{props.children}

); }, }, h4: { component: (props: { id: string; style: React.CSSProperties; children: React.ReactNode; }) => { return (

{props.children}

); }, }, h5: { component: (props: { id: string; style: React.CSSProperties; children: React.ReactNode; }) => { return (
{props.children}
); }, }, h6: { component: (props: { id: string; style: React.CSSProperties; children: React.ReactNode; }) => { return (
{props.children}
); }, }, Sep: { component: () => { return
; }, }, SceneBreak: { component: (props: { id: string }) => { return (
* * *
); }, }, player: { component: () => { return ( {appLayout.playerName ? appLayout.playerName : ""} ); }, }, Transcript: { component: (props) => { return (
{props.children}
); }, }, Line: { component: (props) => { return ( <> {props.name}

{props.children}

); }, }, InsetBox: { component: (props) => { return ( {props.children} ); }, }, li: { component: (props: { children: React.ReactNode }) => { return (
  • 100 ? "my-4" : "" } > {props.children}
  • ); }, }, Highlight: { component: (props: { children: React.ReactNode }) => { return {props.children}; }, }, footer: { component: (props: { children: React.ReactNode }) => { return ( <>
    {props.children}
    ); }, }, img: { component: (props: { alt: string; src: string; width?: number; height?: number; caption?: string; name?: string; }) => { return (
    { setLightboxOpen(true); setLightboxImages([ props.src.startsWith("/uploads/") ? getAssetURL(props.src, ImageQuality.Large) : props.src, ]); setLightboxIndex(0); }} > {props.src.startsWith("/uploads/") ? (
    ) : (
    )}
    ); }, }, }, }} > {text}
    ); } return <>; } function HeaderToolTip(props: { id: string }) { return ( { navigator.clipboard.writeText( process.env.NEXT_PUBLIC_URL_SELF + window.location.pathname + "#" + props.id ); }} > link ); } export function preprocessMarkDawn(text: string): string { let scenebreakIndex = 0; const visitedSlugs: string[] = []; const result = text.split("\n").map((line) => { if (line === "* * *" || line === "---") { scenebreakIndex++; return ``; } if (line.startsWith("# ")) { return markdawnHeadersParser(headerLevels.h1, line, visitedSlugs); } if (line.startsWith("## ")) { return markdawnHeadersParser(headerLevels.h2, line, visitedSlugs); } if (line.startsWith("### ")) { return markdawnHeadersParser(headerLevels.h3, line, visitedSlugs); } if (line.startsWith("#### ")) { return markdawnHeadersParser(headerLevels.h4, line, visitedSlugs); } if (line.startsWith("##### ")) { return markdawnHeadersParser(headerLevels.h5, line, visitedSlugs); } if (line.startsWith("###### ")) { return markdawnHeadersParser(headerLevels.h6, line, visitedSlugs); } return line; }); return result.join("\n"); } enum headerLevels { h1 = 1, h2 = 2, h3 = 3, h4 = 4, h5 = 5, h6 = 6, } function markdawnHeadersParser( headerLevel: headerLevels, line: string, visitedSlugs: string[] ): string { const lineText = line.slice(headerLevel + 1); let slug = slugify(lineText); let newSlug = slug; let index = 2; while (visitedSlugs.includes(newSlug)) { newSlug = `${slug}-${index}`; index++; } visitedSlugs.push(newSlug); return `<${headerLevels[headerLevel]} id="${newSlug}">${lineText}`; } function getAssetUrl(): React.SetStateAction { throw new Error("Function not implemented."); }