@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

main
ts 626 lines 24.4 KB
Raw
1 import type { Root, RootContent } from "mdast";
2 import { unified, type Processor } from "unified";
3 import remarkParse from "remark-parse";
4 import remarkGfm from "remark-gfm";
5 import { remarkKeepWindowsProsePaths } from "@t3tools/shared/markdownWindowsPaths";
6 import {
7 absoluteFilePathMatches,
8 inlineCodeFilePathCandidate,
9 parseMarkdownFileLink,
10 } from "@t3tools/shared/markdownLinks";
11 import { isWindowsAbsolutePath } from "@t3tools/shared/path";
12 import { hasRightToLeftLetter, textDirection } from "@t3tools/shared/textDirection";
13
14 const parser = unified().use(remarkParse).use(remarkGfm).use(remarkKeepWindowsProsePaths);
15 export type MarkdownNode = Root | RootContent;
16
17 interface MarkdownEdit {
18 readonly start: number;
19 readonly end: number;
20 readonly content: string;
21 }
22
23 function applyMarkdownEdits(markdown: string, edits: MarkdownEdit[]): string {
24 if (edits.length === 0) return markdown;
25 const parts: string[] = [];
26 let offset = 0;
27 // AST traversal emits disjoint edits in source order.
28 for (const edit of edits) {
29 parts.push(markdown.slice(offset, edit.start), edit.content);
30 offset = edit.end;
31 }
32 parts.push(markdown.slice(offset));
33 return parts.join("");
34 }
35
36 function decodeCodePoint(codePoint: number, entity: string): string {
37 if (!Number.isInteger(codePoint) || codePoint < 0 || codePoint > 0x10ffff) {
38 return entity;
39 }
40 return String.fromCodePoint(codePoint);
41 }
42
43 export function decodeHtmlEntities(value: string): string {
44 return value.replace(
45 /&(?:#(\d+)|#x([0-9a-f]+)|amp|apos|gt|lt|nbsp|quot);/gi,
46 (entity, decimal: string | undefined, hexadecimal: string | undefined) => {
47 if (decimal) {
48 return decodeCodePoint(Number.parseInt(decimal, 10), entity);
49 }
50 if (hexadecimal) {
51 return decodeCodePoint(Number.parseInt(hexadecimal, 16), entity);
52 }
53 switch (entity.toLowerCase()) {
54 case "&amp;":
55 return "&";
56 case "&apos;":
57 return "'";
58 case "&gt;":
59 return ">";
60 case "&lt;":
61 return "<";
62 case "&nbsp;":
63 return "\u00a0";
64 case "&quot;":
65 return '"';
66 default:
67 return entity;
68 }
69 },
70 );
71 }
72
73 const HTML_TAG = /<!--[\s\S]*?-->|<\/?[a-z](?:[^>"']|"[^"]*"|'[^']*')*>/gi;
74
75 function attribute(tag: string, name: string): string | null {
76 const attributes = /([\w:-]+)\s*=\s*(?:"([^"]*)"|'([^']*)'|([^\s>]+))/g;
77 for (const match of tag.matchAll(attributes)) {
78 if (match[1]?.toLowerCase() === name) {
79 return decodeHtmlEntities(match[2] ?? match[3] ?? match[4] ?? "");
80 }
81 }
82 return null;
83 }
84
85 function markdownDestination(url: string): string {
86 return `<${url.replace(/&/g, "\\&").replace(/</g, "%3C").replace(/>/g, "%3E").replace(/\|/g, "%7C").replace(/\n/g, "")}>`;
87 }
88
89 const ANCHOR_TAG = /^<\/?a[\s/>]/i;
90
91 /** Replacements for the anchor tags still ahead, and how many emitted links are open. */
92 type AnchorState = { readonly pending: string[]; open: number };
93
94 // Code spans keep their text; an odd trailing backslash would escape the closing "]".
95 function escapeLinkLabel(text: string): string {
96 return text
97 .replace(/(`+)[\s\S]*?\1|\\*[[\]]/g, (match) =>
98 match.startsWith("`") || match.length % 2 === 0
99 ? match
100 : `${match.slice(0, -1)}\\${match.slice(-1)}`,
101 )
102 .replace(/\\+$/, (slashes) => (slashes.length % 2 ? `${slashes}\\` : slashes));
103 }
104
105 // An unescaped "!" before the inserted "[" would turn the link into an image.
106 function bangBefore(text: string, index: number): boolean {
107 if (text[index - 1] !== "!") return false;
108 let backslashes = 0;
109 while (text[index - 2 - backslashes] === "\\") backslashes += 1;
110 return backslashes % 2 === 0;
111 }
112
113 /**
114 * Pairs `<a>` tags in order. Only a closed anchor with an http(s) or mailto href becomes a
115 * Markdown link; any other anchor keeps its text unlinked.
116 */
117 function anchorMarkdown(tags: readonly string[]): AnchorState {
118 const replacements = tags.map(() => "");
119 const open: Array<{ index: number; href: string | undefined }> = [];
120 tags.forEach((tag, index) => {
121 if (tag[1] !== "/") {
122 const href = attribute(tag, "href")?.trim();
123 open.push({ index, href: href && /^(?:https?|mailto):/i.test(href) ? href : undefined });
124 return;
125 }
126 const opening = open.pop();
127 if (!opening?.href) return;
128 replacements[opening.index] = "[";
129 replacements[index] = `](${markdownDestination(opening.href)})`;
130 });
131 return { pending: replacements, open: 0 };
132 }
133
134 export function markdownHtmlTags(content: string): RegExpExecArray[] {
135 // An HTML block such as <details> can also contain Markdown fences and code spans.
136 const codeRanges: Array<{ start: number; end: number }> = [];
137 const tags = [...content.matchAll(HTML_TAG)];
138 let tagIndex = 0;
139 const delimiters = [...content.matchAll(/`+|~{3,}/g)].filter((delimiter) => {
140 while (tags[tagIndex] && tags[tagIndex]!.index + tags[tagIndex]![0].length <= delimiter.index)
141 tagIndex += 1;
142 const tag = tags[tagIndex];
143 return !tag || delimiter.index < tag.index;
144 });
145 for (let index = 0; index < delimiters.length; index += 1) {
146 const opening = delimiters[index]!;
147 const lineStart = content.lastIndexOf("\n", opening.index - 1) + 1;
148 const fenced =
149 opening[0].length >= 3 && /^ {0,3}$/.test(content.slice(lineStart, opening.index));
150 if (opening[0][0] === "~" && !fenced) continue;
151 let closingIndex = -1;
152 for (let candidate = index + 1; candidate < delimiters.length; candidate += 1) {
153 const closing = delimiters[candidate]!;
154 if (!fenced && closing[0] === opening[0]) {
155 closingIndex = candidate;
156 break;
157 }
158 if (!fenced || closing[0][0] !== opening[0][0] || closing[0].length < opening[0].length)
159 continue;
160 const start = content.lastIndexOf("\n", closing.index - 1) + 1;
161 const nextLine = content.indexOf("\n", closing.index);
162 const end = nextLine === -1 ? content.length : nextLine;
163 if (
164 /^ {0,3}$/.test(content.slice(start, closing.index)) &&
165 /^\s*$/.test(content.slice(closing.index + closing[0].length, end))
166 ) {
167 closingIndex = candidate;
168 break;
169 }
170 }
171 if (closingIndex === -1) {
172 if (!fenced) continue;
173 // An unclosed fence runs to the end of the content, so no later opener matters.
174 codeRanges.push({ start: opening.index, end: content.length });
175 break;
176 }
177 const closing = delimiters[closingIndex]!;
178 codeRanges.push({ start: opening.index, end: closing.index + closing[0].length });
179 index = closingIndex;
180 }
181 return tags.filter(
182 (tag) => !codeRanges.some((range) => tag.index >= range.start && tag.index < range.end),
183 );
184 }
185
186 /**
187 * Called only for parsed HTML nodes, so Markdown code examples keep their literal tags.
188 * `anchors` is shared across sibling inline HTML nodes because each of those carries a single tag.
189 */
190 export function markdownHtmlContent(
191 content: string,
192 options: { readonly anchors?: AnchorState; readonly inTableCell?: boolean } = {},
193 ): string {
194 const tags = markdownHtmlTags(content);
195 const anchors =
196 options.anchors ??
197 anchorMarkdown(tags.map((tag) => tag[0]).filter((tag) => ANCHOR_TAG.test(tag)));
198 let result = "";
199 let end = 0;
200 for (const match of tags) {
201 const text = content.slice(end, match.index);
202 result += anchors.open > 0 ? escapeLinkLabel(text) : text;
203 const replacement = htmlTagMarkdown(match[0], anchors, options);
204 if (replacement === "[" && bangBefore(result, result.length))
205 result = `${result.slice(0, -1)}\\!`;
206 result +=
207 anchors.open > 0 && /[\s[]/.test(result.at(-1) ?? "")
208 ? replacement.replace(/^ /, "")
209 : replacement;
210 end = match.index + match[0].length;
211 }
212 const text = content.slice(end);
213 return result + (anchors.open > 0 ? escapeLinkLabel(text) : text);
214 }
215
216 function htmlTagMarkdown(
217 tag: string,
218 anchors: AnchorState,
219 options: { readonly inTableCell?: boolean },
220 ): string {
221 if (tag.startsWith("<!--")) return "";
222 if (ANCHOR_TAG.test(tag)) {
223 const replacement = anchors.pending.shift() ?? "";
224 if (replacement === "[") anchors.open += 1;
225 else if (replacement) anchors.open -= 1;
226 return replacement;
227 }
228 if (/^<img\b/i.test(tag)) {
229 const src = attribute(tag, "src");
230 if (!src) return "";
231 const alt = (attribute(tag, "alt") ?? "").replace(/[\\[\]&]/g, "\\$&");
232 return `![${alt}](${markdownDestination(src)})`;
233 }
234 // A raw newline would end the table row; both parsers decode this reference to one inside the cell.
235 const cellBreak = "&#10;";
236 // A blank line inside an open link label would end the paragraph before its "]".
237 const blockBreak = anchors.open > 0 ? " " : options.inTableCell ? cellBreak : "\n\n";
238 if (/^<summary\b/i.test(tag)) return `${blockBreak}**`;
239 if (/^<\/summary\s*>/i.test(tag)) return `**${blockBreak}`;
240 if (/^<br\b/i.test(tag)) return options.inTableCell ? cellBreak : " \n";
241 if (/^<\/?(?:p|div|details)\b/i.test(tag)) return blockBreak;
242 return "";
243 }
244
245 export function markdownWithHtml(markdown: string): string {
246 if (!/<(?:!--|\/?[a-z])/i.test(markdown)) return markdown;
247 const edits: Array<{ start: number; end: number; content: string }> = [];
248 const visit = (node: MarkdownNode, inTableCell: boolean, inLink: boolean) => {
249 if (!("children" in node)) return;
250 inTableCell ||= node.type === "tableCell";
251 const children: readonly MarkdownNode[] = node.children;
252 // Inline HTML arrives one tag per node, so anchors pair across phrasing siblings.
253 const anchors =
254 node.type === "root" || node.type === "blockquote" || node.type === "listItem"
255 ? undefined
256 : anchorMarkdown(
257 children.flatMap((child) =>
258 child.type === "html"
259 ? markdownHtmlTags(child.value)
260 .map((tag) => tag[0])
261 .filter((tag) => ANCHOR_TAG.test(tag))
262 : [],
263 ),
264 );
265 for (const child of children) {
266 const start = child.position?.start.offset;
267 const end = child.position?.end.offset;
268 const linkLabel = inLink || (anchors?.open ?? 0) > 0;
269 if (child.type === "text") {
270 if (linkLabel && start !== undefined && end !== undefined) {
271 const content = escapeLinkLabel(markdown.slice(start, end));
272 if (content !== markdown.slice(start, end)) edits.push({ start, end, content });
273 }
274 continue;
275 }
276 if (child.type !== "html") {
277 visit(child, inTableCell, linkLabel);
278 continue;
279 }
280 const content = markdownHtmlContent(child.value, { anchors, inTableCell });
281 if (start === undefined || end === undefined) continue;
282 // Text before an outermost link is never edited, so this edit cannot overlap another.
283 if (content.startsWith("[") && !linkLabel && bangBefore(markdown, start))
284 edits.push({ start: start - 1, end: start, content: "\\!" });
285 edits.push({ start, end, content });
286 }
287 };
288 visit(parser.parse(markdown), false, false);
289 return applyMarkdownEdits(markdown, edits);
290 }
291
292 /** Reference definitions as authored, one line each, so a Windows destination keeps its backslashes. */
293 export function markdownReferenceDefinitions(markdown: string): string[] {
294 if (!markdown.includes("]:")) return [];
295 // Flattened as the renderer does it: a raw HTML block hides the definitions inside it.
296 const flattened = markdownWithHtml(markdown);
297 const definitions: string[] = [];
298 const visit = (node: MarkdownNode, quotes: number) => {
299 if (node.type === "definition") {
300 // A wrapped definition drops its indent and the enclosing quotes' markers, not a literal ">".
301 const wrap = new RegExp(`(?:\\r\\n|\\r|\\n)(?:[ \\t]*>){0,${quotes}}[ \\t]*`, "g");
302 definitions.push(
303 flattened.slice(node.position?.start.offset, node.position?.end.offset).replace(wrap, " "),
304 );
305 }
306 if (!("children" in node)) return;
307 for (const child of node.children) visit(child, quotes + (node.type === "blockquote" ? 1 : 0));
308 };
309 visit(parser.parse(flattened), 0);
310 return definitions;
311 }
312
313 /**
314 * A reply split into folds and cards renders each fragment as its own document, so each one
315 * carries the reply's definitions. The first definition of a label wins, so the fragment's own
316 * lead: the renderer then prefers them by its own label matching, with none reimplemented here.
317 */
318 export function withReferenceDefinitions(
319 fragment: string,
320 replyDefinitions: ReadonlyArray<string>,
321 ): string {
322 if (replyDefinitions.length === 0 || !fragment.includes("[")) return fragment;
323 const definitions = new Set([...markdownReferenceDefinitions(fragment), ...replyDefinitions]);
324 return `${[...definitions].join("\n")}\n\n${fragment}`;
325 }
326
327 // Start only at the beginning of a backtick run to avoid retrying every suffix.
328 const INLINE_CODE_SPAN = /(?<!`)`+([^`\r\n]+)`+/g;
329
330 // The native regex stops at whitespace; the shared classifier decides paths with spaces.
331 function isInlineCodeFileLink(code: string, fileLink: RegExp): boolean {
332 return fileLink.test(code) || (/\s/.test(code) && inlineCodeFilePathCandidate(code) !== null);
333 }
334
335 export function markdownWithInlineFileLinks(markdown: string, fileLink: RegExp): string {
336 return transformNativeMarkdown(markdown, fileLink, false);
337 }
338
339 function transformNativeMarkdown(
340 markdown: string,
341 fileLink: RegExp | undefined,
342 tableBreaks: boolean,
343 ): string {
344 const breakTables = tableBreaks && mayHaveTableUnderText(markdown);
345 let linkFiles = false;
346 if (fileLink) {
347 for (const [, code] of markdown.matchAll(INLINE_CODE_SPAN)) {
348 if (isInlineCodeFileLink(code!.trim(), fileLink)) {
349 linkFiles = true;
350 break;
351 }
352 }
353 }
354 const linkProseFiles = fileLink !== undefined && absoluteFilePathMatches(markdown).length > 0;
355 if (!linkFiles && !linkProseFiles && !breakTables) return markdown;
356 const edits: MarkdownEdit[] = [];
357 const fileEdits: MarkdownEdit[] = [];
358 let rewritesPipe = false;
359 const visit = (node: MarkdownNode) => {
360 if (linkFiles && node.type === "inlineCode") {
361 const start = node.position?.start.offset;
362 const end = node.position?.end.offset;
363 const escaped = node.value.replace(/[\\[\]&]/g, "\\$&");
364 if (
365 start !== undefined &&
366 end !== undefined &&
367 fileLink !== undefined &&
368 isInlineCodeFileLink(node.value, fileLink) &&
369 parseMarkdownFileLink(node.value) !== null &&
370 !/[<>]/.test(node.value)
371 ) {
372 const edit = {
373 start,
374 end,
375 content: `[${escaped}](<${escaped}>)`,
376 };
377 edits.push(edit);
378 fileEdits.push(edit);
379 if (node.value.includes("|")) rewritesPipe = true;
380 }
381 } else if (
382 linkProseFiles &&
383 node.type === "text" &&
384 node.position?.start.offset !== undefined
385 ) {
386 const start = node.position.start.offset;
387 const raw = markdown.slice(start, node.position.end.offset);
388 for (const { index, path } of absoluteFilePathMatches(raw)) {
389 // Source offsets stay valid for paths whose own text needs no Markdown decoding.
390 if (raw !== node.value && /[\\&]/.test(path)) continue;
391 const escaped = path.replace(/[\\[\]&]/g, "\\$&");
392 const edit = {
393 start: start + index,
394 end: start + index + path.length,
395 content: `[${escaped}](<${escaped}>)`,
396 };
397 edits.push(edit);
398 fileEdits.push(edit);
399 }
400 } else if (node.type !== "link" && node.type !== "linkReference" && "children" in node)
401 node.children.forEach((child, index) => {
402 const previous = node.children[index - 1];
403 const start = child.position?.start;
404 if (
405 breakTables &&
406 child.type === "table" &&
407 previous?.type === "paragraph" &&
408 start?.offset !== undefined &&
409 previous.position?.end.line === start.line - 1
410 ) {
411 let lineStart = start.offset;
412 while (lineStart > 0 && !/[\r\n]/.test(markdown[lineStart - 1]!)) lineStart -= 1;
413 // Replace only the container prefix, leaving cell edits disjoint from the table break.
414 const [, header = "", , ending = "\n", markers = ""] =
415 /^([ \t>]*)([^\r\n]*)(\r\n|\r|\n)([ \t>]*)/.exec(markdown.slice(lineStart)) ?? [];
416 edits.push({
417 start: lineStart,
418 end: lineStart + header.length,
419 content: `${markers.trimEnd()}${ending}${markers}`,
420 });
421 }
422 visit(child);
423 });
424 };
425 visit(parser.parse(markdown));
426 // Rewritten pipes can change table structure, so preserve the original pass ordering.
427 if (breakTables && rewritesPipe)
428 return markdownWithTableBreaks(applyMarkdownEdits(markdown, fileEdits));
429 return applyMarkdownEdits(markdown, edits);
430 }
431
432 // A delimiter row such as `|---|:--:|`, possibly inside a blockquote or list item.
433 function isTableDelimiterRow(line: string): boolean {
434 if (!line.includes("-")) return false;
435 const row = line.replace(/^[ \t>]+/, "").trim();
436 return row
437 .replace(/^\||\|$/g, "")
438 .split("|")
439 .every((cell) => /^:?-+:?$/.test(cell.trim()));
440 }
441
442 function mayHaveTableUnderText(markdown: string): boolean {
443 if (!markdown.includes("|")) return false;
444 const lines = markdown.split(/\r\n|\r|\n/);
445 for (let index = 2; index < lines.length; index += 1) {
446 if (isTableDelimiterRow(lines[index]!) && /[^\s>]/.test(lines[index - 2]!)) return true;
447 }
448 return false;
449 }
450
451 /** md4c starts tables only at paragraph starts; insert breaks to match GFM. */
452 export function markdownWithTableBreaks(markdown: string): string {
453 return transformNativeMarkdown(markdown, undefined, true);
454 }
455
456 // `>` covers a destination continued on a blockquote line.
457 const WINDOWS_DESTINATION = /\](?:\(|:)[\s>]*<?(?:[A-Za-z]:[\\/]|\\\\)/;
458
459 interface DestinationToken {
460 readonly start: { readonly offset: number };
461 readonly end: { readonly offset: number };
462 }
463
464 /**
465 * Markdown reads `C:\work\.t3` as `C:\work.t3`. Web keeps authored Windows destinations
466 * literally (see `remarkKeepWindowsPaths` in shared), so escape their backslashes to give
467 * both parsers here the same path. `\&` stays an escape, as `markdownWithHtml` emits it.
468 * Expects authored Markdown: already escaped output would be escaped twice.
469 */
470 function markdownWithWindowsPathDestinations(markdown: string): string {
471 // Skips the extra parse for the common message that has no Windows destination.
472 if (!WINDOWS_DESTINATION.test(markdown)) return markdown;
473 const edits: MarkdownEdit[] = [];
474 function record(
475 this: { resume(): string; sliceSerialize(token: DestinationToken): string },
476 token: DestinationToken,
477 ) {
478 this.resume();
479 const authored = this.sliceSerialize(token);
480 if (!isWindowsAbsolutePath(authored) || !authored.includes("\\") || /[<>]/.test(authored))
481 return;
482 const start = token.start.offset;
483 const end = token.end.offset;
484 const escaped = authored.replace(/\\(?!&)/g, "\\\\");
485 // A bare destination ends at an unescaped `)`, so wrap it to keep `\)` inside.
486 const angled = markdown[start - 1] === "<";
487 edits.push({ start, end, content: angled ? escaped : `<${escaped}>` });
488 }
489 unified()
490 .use(remarkParse)
491 .use(remarkGfm)
492 .use(function (this: Processor) {
493 (this.data().fromMarkdownExtensions ??= []).push({
494 exit: { resourceDestinationString: record, definitionDestinationString: record },
495 });
496 })
497 .parse(markdown);
498 return applyMarkdownEdits(markdown, edits);
499 }
500
501 // HTML wrappers can expose Markdown that wasn't in the first AST, so normalize them first.
502 export function markdownForNativeParsing(markdown: string): string {
503 return markdownWithWindowsPathDestinations(markdownWithHtml(markdown));
504 }
505
506 export function parseMarkdownDocument(markdown: string): Root {
507 return parser.parse(markdownForNativeParsing(markdown));
508 }
509
510 export function markdownForNativeRendering(markdown: string, fileLink: RegExp): string {
511 return transformNativeMarkdown(markdownForNativeParsing(markdown), fileLink, true);
512 }
513
514 /** Native media slots only cover standalone images; nested file embeds must become links. */
515 export function markdownWithFileEmbeds(
516 markdown: string,
517 label: (href: string, standalone: boolean) => string | null,
518 ): { markdown: string; fileHrefs: ReadonlySet<string> } {
519 const fileHrefs = new Set<string>();
520 if (!markdown.includes("![")) return { markdown, fileHrefs };
521 const root = parser.parse(markdown);
522 const topLevelBlocks = new Set<MarkdownNode>(root.children);
523 const definitions = new Map<string, string>();
524 const collect = (node: MarkdownNode) => {
525 if (node.type === "definition") {
526 const identifier = node.identifier.toUpperCase();
527 if (!definitions.has(identifier)) definitions.set(identifier, node.url);
528 }
529 if ("children" in node) node.children.forEach(collect);
530 };
531 collect(root);
532 const edits: Array<{ start: number; end: number; content: string }> = [];
533 const visit = (node: MarkdownNode, standalone = false, insideLink = false) => {
534 if (node.type === "image" || node.type === "imageReference") {
535 const href =
536 node.type === "image" ? node.url : definitions.get(node.identifier.toUpperCase());
537 const name = href === undefined ? null : label(href, standalone);
538 const start = node.position?.start.offset;
539 const end = node.position?.end.offset;
540 if (href !== undefined && name !== null && start !== undefined && end !== undefined) {
541 const text = name.replace(/[\\[\]`*_~|&<>]/g, "\\$&");
542 const url = href.replace(/[\\&]/g, "\\$&").replace(/</g, "%3C").replace(/>/g, "%3E");
543 edits.push({ start, end, content: insideLink ? text : `[${text}](<${url}>)` });
544 fileHrefs.add(href);
545 }
546 }
547 if ("children" in node)
548 node.children.forEach((child) =>
549 visit(
550 child,
551 node.type === "paragraph" && node.children.length === 1 && topLevelBlocks.has(node),
552 insideLink || node.type === "link" || node.type === "linkReference",
553 ),
554 );
555 };
556 visit(root);
557 const parts: string[] = [];
558 let offset = 0;
559 for (const edit of edits) {
560 parts.push(markdown.slice(offset, edit.start), edit.content);
561 offset = edit.end;
562 }
563 parts.push(markdown.slice(offset));
564 return { markdown: parts.join(""), fileHrefs };
565 }
566
567 const DIRECTION_MARK = { ltr: "‎", rtl: "‏" } as const;
568
569 function proseText(node: MarkdownNode): string {
570 if (node.type === "text") return node.value;
571 if (node.type === "inlineCode" || node.type === "code") return " ";
572 if (!("children" in node)) return "";
573 const text = node.children.map(proseText).join("");
574 return node.type === "paragraph" || node.type === "heading" || node.type === "tableCell"
575 ? ` ${text} `
576 : text;
577 }
578
579 // The native renderer scans inline code too when it picks a paragraph's direction.
580 function renderedText(node: MarkdownNode): string {
581 if (node.type === "text" || node.type === "inlineCode") return node.value;
582 if (!("children" in node)) return "";
583 return node.children.map(renderedText).join("");
584 }
585
586 // A mark inside `[label]` or `<url>` would break the reference or autolink.
587 function firstLeafOffset(node: MarkdownNode): number | undefined {
588 if (
589 !("children" in node) ||
590 node.children.length === 0 ||
591 node.type === "link" ||
592 node.type === "linkReference"
593 ) {
594 return node.position?.start.offset;
595 }
596 return firstLeafOffset(node.children[0]!);
597 }
598
599 /**
600 * The native renderer gives each paragraph the direction of its first letter, so an Arabic
601 * line that opens with an English name renders left to right. Like the web's
602 * `rehypeTextDirection`, each top-level block follows the majority of its words, and a
603 * paragraph whose first letter disagrees gets a direction mark that the renderer reads first.
604 */
605 export function markdownWithTextDirection(markdown: string): string {
606 if (!hasRightToLeftLetter(markdown)) return markdown;
607 const edits: MarkdownEdit[] = [];
608 for (const block of parser.parse(markdown).children) {
609 if (block.type === "code" || block.type === "html") continue;
610 const direction = textDirection(proseText(block));
611 if (!direction) continue;
612 const mark = (node: MarkdownNode) => {
613 if (node.type === "paragraph" || node.type === "heading" || node.type === "tableCell") {
614 const offset = firstLeafOffset(node);
615 const first = renderedText(node).match(/\p{L}/u)?.[0];
616 if (offset !== undefined && first && textDirection(first) !== direction) {
617 edits.push({ start: offset, end: offset, content: DIRECTION_MARK[direction] });
618 }
619 return;
620 }
621 if ("children" in node) node.children.forEach(mark);
622 };
623 mark(block);
624 }
625 return applyMarkdownEdits(markdown, edits);
626 }