"use client";
/**
* A small, purpose-built Markdown renderer for the reader's text view.
*
* It is not a general Markdown engine: it renders exactly the constructs
* extracted web/EPUB content actually produces (bold, italic, inline code,
* links, blockquotes, bullet lists, horizontal rules) and nothing that would
* require a multi-line block model (tables, nested lists).
*
* The one rule every branch below must hold: the DOM `textContent` of what it
* renders, read start to end, must equal the original source line exactly.
* Recogito's TextPosition selectors and the server's verbatim-quote check
* both resolve against that text, so a highlighted "**bold**" that visually
* shows only "bold" still has to leave the two `**` where a selection can
* still see them — just visually collapsed to nothing. `HiddenMark` is that
* collapse: present in the DOM, `aria-hidden`, zero-size, so `Range.toString()`
* still walks through it but nothing is painted.
*/
import { Fragment, type ReactNode } from "react";
function HiddenMark({ text }: { text: string }) {
if (!text) return null;
return (
{text}
);
}
// Alternatives are tried in order at each position, so `**bold**` is claimed
// by the first branch before the single-`*` italic branch ever sees it.
const INLINE_PATTERN =
/(\*\*|__)([^\n]+?)\1|`([^`\n]+)`|\[([^\]\n]+)\]\(([^)\s]+)\)|(\*|_)([^\s*_][^\n]*?)\6(?!\w)/g;
/**
* An embedded-picture ingest marker line — ``[图片 3: image-03.png]``, the
* exact fixed format the extractor emits. Matching the whole line keeps the
* figure at the picture's original position between paragraphs; anything
* else falls back to literal text.
*/
const IMAGE_MARKER_PATTERN = /^\s{0,3}\[图片 \d+: ([^\]\s]+)\]\s*$/;
/**
* Names of embedded pictures whose marker lines appear in *text*, so a caller
* can tell which media items the inline renderer claimed and which still need
* the fallback strip below the article.
*/
export function imageMarkerNames(text: string): Set {
const names = new Set();
for (const line of text.split("\n")) {
const match = IMAGE_MARKER_PATTERN.exec(line);
if (match) names.add(match[1]);
}
return names;
}
/** Bold, italic, inline code, and links within one line of plain text. */
export function InlineMarkdown({ text }: { text: string }): ReactNode {
const nodes: ReactNode[] = [];
let lastIndex = 0;
let key = 0;
// `matchAll` never mutates the shared pattern's `lastIndex` — it is
// specified to operate on an internal copy — so this stays safe to call
// with a module-level regex from a component body.
for (const match of text.matchAll(INLINE_PATTERN)) {
if (match.index > lastIndex) {
nodes.push(text.slice(lastIndex, match.index));
}
const [
whole,
boldDelim,
boldContent,
codeContent,
linkLabel,
linkUrl,
italicDelim,
italicContent,
] = match;
if (boldDelim) {
nodes.push(
{boldContent}
,
);
} else if (codeContent !== undefined) {
nodes.push(
{codeContent}
,
);
} else if (linkLabel !== undefined) {
nodes.push(
{linkLabel}
,
);
} else if (italicDelim) {
nodes.push(
{italicContent}
,
);
}
lastIndex = (match.index ?? lastIndex) + whole.length;
}
if (lastIndex < text.length) nodes.push(text.slice(lastIndex));
return <>{nodes}>;
}
const RULE_PATTERN = /^\s{0,3}(?:(-\s*){3,}|(\*\s*){3,}|(_\s*){3,})$/;
const BLOCKQUOTE_PATTERN = /^(\s{0,3}>\s?)([\s\S]*)$/;
const BULLET_PATTERN = /^(\s{0,3}[-*+]\s+)(\S[\s\S]*)$/;
/**
* One rendered line of body text: block-level markers first (a line is at
* most one of rule / blockquote / bullet), then inline formatting on
* whatever text remains. An embedded-picture marker line goes to
* *renderImage* first — returning null keeps the literal marker text.
*/
export function MarkdownLine({
text,
renderImage,
}: {
text: string;
renderImage?: (name: string, markerText: string) => ReactNode | null;
}): ReactNode {
if (renderImage) {
const image = IMAGE_MARKER_PATTERN.exec(text);
if (image) {
const figure = renderImage(image[1], text);
if (figure !== null && figure !== undefined) return figure;
}
}
if (RULE_PATTERN.test(text) && text.trim().length >= 3) {
return (
<>
>
);
}
const blockquote = BLOCKQUOTE_PATTERN.exec(text);
if (blockquote) {
const [, marker, rest] = blockquote;
return (
);
}
const bullet = BULLET_PATTERN.exec(text);
if (bullet) {
const [, marker, rest] = bullet;
return (
);
}
return ;
}