|
9 | 9 | parseMarkdownFileLink, |
10 | 10 | } from "@onicode/shared/markdownLinks"; |
11 | 11 | import { isWindowsAbsolutePath } from "@onicode/shared/path"; |
12 | | -import { hasRightToLeftLetter, textDirection } from "@onicode/shared/textDirection"; |
13 | 12 |
|
14 | 13 | const parser = unified().use(remarkParse).use(remarkGfm).use(remarkKeepWindowsProsePaths); |
15 | 14 | export type MarkdownNode = Root | RootContent; |
@@ -563,64 +562,3 @@ export function markdownWithFileEmbeds( |
563 | 562 | parts.push(markdown.slice(offset)); |
564 | 563 | return { markdown: parts.join(""), fileHrefs }; |
565 | 564 | } |
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 | | -} |
0 commit comments