diff --git a/src/components/Markdown/Markdawn.tsx b/src/components/Markdown/Markdawn.tsx index 18059d7..68b7b96 100644 --- a/src/components/Markdown/Markdawn.tsx +++ b/src/components/Markdown/Markdawn.tsx @@ -20,7 +20,8 @@ interface Props { export function Markdawn(props: Immutable): JSX.Element { const appLayout = useAppLayout(); - const text = preprocessMarkDawn(props.text); + // eslint-disable-next-line no-irregular-whitespace + const text = `${preprocessMarkDawn(props.text)}​`; const router = useRouter(); @@ -54,6 +55,7 @@ export function Markdawn(props: Immutable): JSX.Element { ); }, }, + h1: { component: (compProps: { id: string; @@ -66,6 +68,7 @@ export function Markdawn(props: Immutable): JSX.Element { ), }, + h2: { component: (compProps: { id: string; @@ -78,6 +81,7 @@ export function Markdawn(props: Immutable): JSX.Element { ), }, + h3: { component: (compProps: { id: string; @@ -90,6 +94,7 @@ export function Markdawn(props: Immutable): JSX.Element { ), }, + h4: { component: (compProps: { id: string; @@ -102,6 +107,7 @@ export function Markdawn(props: Immutable): JSX.Element { ), }, + h5: { component: (compProps: { id: string; @@ -114,6 +120,7 @@ export function Markdawn(props: Immutable): JSX.Element { ), }, + h6: { component: (compProps: { id: string; @@ -126,9 +133,7 @@ export function Markdawn(props: Immutable): JSX.Element { ), }, - Sep: { - component: () =>
, - }, + SceneBreak: { component: (compProps: { id: string }) => (
): JSX.Element {
), }, + IntraLink: { component: (compProps: { children: React.ReactNode; @@ -161,6 +167,7 @@ export function Markdawn(props: Immutable): JSX.Element { ); }, }, + player: { component: () => ( @@ -168,6 +175,7 @@ export function Markdawn(props: Immutable): JSX.Element { ), }, + Transcript: { component: (compProps) => (
@@ -175,6 +183,7 @@ export function Markdawn(props: Immutable): JSX.Element {
), }, + Line: { component: (compProps) => ( <> @@ -185,11 +194,13 @@ export function Markdawn(props: Immutable): JSX.Element { ), }, + InsetBox: { component: (compProps) => ( {compProps.children} ), }, + li: { component: (compProps: { children: React.ReactNode }) => (
  • ): JSX.Element {
  • ), }, + Highlight: { component: (compProps: { children: React.ReactNode }) => ( {compProps.children} ), }, + footer: { component: (compProps: { children: React.ReactNode }) => ( <> @@ -219,6 +232,7 @@ export function Markdawn(props: Immutable): JSX.Element { ), }, + blockquote: { component: (compProps: { children: React.ReactNode; @@ -236,6 +250,7 @@ export function Markdawn(props: Immutable): JSX.Element { ), }, + img: { component: (compProps: { alt: string; @@ -301,45 +316,64 @@ function HeaderToolTip(props: { id: string }) { ); } +function typographicRules(text: string): string { + let newText = text; + newText = newText.replace(/--/gu, "—"); + /* + * newText = newText.replace(/\.\.\./gu, "…"); + * newText = newText.replace(/(?:^|[\s{[(<'"\u2018\u201C])(")/gu, " “"); + * newText = newText.replace(/"/gu, "”"); + * newText = newText.replace(/(?:^|[\s{[(<'"\u2018\u201C])(')/gu, " ‘"); + * newText = newText.replace(/'/gu, "’"); + */ + return newText; +} + export function preprocessMarkDawn(text: string): string { if (!text) return ""; + let preprocessed = typographicRules(text); + let scenebreakIndex = 0; const visitedSlugs: string[] = []; - const result = text.split("\n").map((line) => { - if (line === "* * *" || line === "---") { - scenebreakIndex += 1; - return ``; - } + preprocessed = preprocessed + .split("\n") + .map((line) => { + if (line === "* * *" || line === "---") { + scenebreakIndex += 1; + return ``; + } - if (line.startsWith("# ")) { - return markdawnHeadersParser(HeaderLevels.H1, line, visitedSlugs); - } + if (line.startsWith("# ")) { + return markdawnHeadersParser(HeaderLevels.H1, line, visitedSlugs); + } - if (line.startsWith("## ")) { - return markdawnHeadersParser(HeaderLevels.H2, 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.H3, line, visitedSlugs); + } - if (line.startsWith("#### ")) { - return markdawnHeadersParser(HeaderLevels.H4, 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.H5, line, visitedSlugs); + } - if (line.startsWith("###### ")) { - return markdawnHeadersParser(HeaderLevels.H6, line, visitedSlugs); - } + if (line.startsWith("###### ")) { + return markdawnHeadersParser(HeaderLevels.H6, line, visitedSlugs); + } - return line; - }); - return result.join("\n"); + return line; + }) + .join("\n"); + + return preprocessed; } enum HeaderLevels { @@ -365,5 +399,5 @@ function markdawnHeadersParser( index += 1; } visitedSlugs.push(newSlug); - return `<${HeaderLevels[headerLevel]} id="${newSlug}">${lineText}`; + return `${lineText}`; }