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
| 1 | import { githubEmojiAliases } from "@t3tools/client-runtime/pull-request-markdown"; |
| 2 | import type { ReferenceAutolink } from "@t3tools/client-runtime/source-control-clients"; |
| 3 | import remarkGfm from "remark-gfm"; |
| 4 | import remarkParse from "remark-parse"; |
| 5 | import { unified } from "unified"; |
| 6 | |
| 7 | type MarkdownNode = { |
| 8 | readonly type: string; |
| 9 | readonly position?: |
| 10 | | { |
| 11 | readonly start: { readonly offset?: number | undefined }; |
| 12 | readonly end: { readonly offset?: number | undefined }; |
| 13 | } |
| 14 | | undefined; |
| 15 | readonly children?: readonly MarkdownNode[] | undefined; |
| 16 | }; |
| 17 | |
| 18 | const parser = unified().use(remarkParse).use(remarkGfm); |
| 19 | const UNLINKED_TYPES = new Set([ |
| 20 | "link", |
| 21 | "linkReference", |
| 22 | "image", |
| 23 | "imageReference", |
| 24 | "code", |
| 25 | "inlineCode", |
| 26 | "html", |
| 27 | "definition", |
| 28 | ]); |
| 29 | |
| 30 | const unchanged = (offset: number) => offset; |
| 31 | |
| 32 | /** |
| 33 | * Rewrites the references `autolinks` finds in the markdown's prose as markdown links, and GitHub's |
| 34 | * `:alias:` emoji as the emoji, because the native renderer takes markdown text. Text is searched |
| 35 | * as written, escapes included. `sourceOffset` maps an offset in the result back to `markdown`. |
| 36 | */ |
| 37 | export function nativePullRequestMarkdown( |
| 38 | markdown: string, |
| 39 | autolinks: ((text: string) => ReadonlyArray<ReferenceAutolink>) | null, |
| 40 | githubEmoji: boolean, |
| 41 | ): { readonly markdown: string; readonly sourceOffset: (offset: number) => number } { |
| 42 | if (autolinks === null && !githubEmoji) return { markdown, sourceOffset: unchanged }; |
| 43 | const edits: { start: number; end: number; value: string }[] = []; |
| 44 | const visit = (node: MarkdownNode, inLink: boolean) => { |
| 45 | // Only a bracketed label is prose; an autolink's text is its destination. |
| 46 | const labelled = markdown[node.position?.start.offset ?? -1] === "["; |
| 47 | if (node.type === "link" && labelled && !inLink) { |
| 48 | node.children?.forEach((child) => visit(child, true)); |
| 49 | return; |
| 50 | } |
| 51 | if (UNLINKED_TYPES.has(node.type)) return; |
| 52 | if (node.type === "text") { |
| 53 | const start = node.position?.start.offset; |
| 54 | const end = node.position?.end.offset; |
| 55 | if (start === undefined || end === undefined) return; |
| 56 | const text = markdown.slice(start, end); |
| 57 | const found: { start: number; end: number; value: string }[] = []; |
| 58 | for (const link of inLink ? [] : (autolinks?.(text) ?? [])) { |
| 59 | found.push({ |
| 60 | start: start + link.index, |
| 61 | end: start + link.index + link.text.length, |
| 62 | value: `[${link.label}](${link.url})`, |
| 63 | }); |
| 64 | } |
| 65 | for (const alias of githubEmoji ? githubEmojiAliases(text) : []) { |
| 66 | // Keycap emoji start with `*`, which the native parser would read as emphasis. |
| 67 | const value = alias.emoji.replaceAll("*", "\\*"); |
| 68 | found.push({ |
| 69 | start: start + alias.index, |
| 70 | end: start + alias.index + alias.text.length, |
| 71 | value, |
| 72 | }); |
| 73 | } |
| 74 | found.sort((left, right) => left.start - right.start); |
| 75 | for (const edit of found) { |
| 76 | if (edit.start >= (edits.at(-1)?.end ?? 0)) edits.push(edit); |
| 77 | } |
| 78 | } else if (node.children !== undefined) { |
| 79 | let protectedHtmlDepth = 0; |
| 80 | for (const child of node.children) { |
| 81 | if (child.type === "html") { |
| 82 | const start = child.position?.start.offset; |
| 83 | const end = child.position?.end.offset; |
| 84 | const tag = start === undefined || end === undefined ? "" : markdown.slice(start, end); |
| 85 | for (const match of tag.matchAll(/<(\/)?(?:a|code|pre|script|style)(?=[\s>])[^>]*>/giu)) { |
| 86 | protectedHtmlDepth = Math.max(0, protectedHtmlDepth + (match[1] ? -1 : 1)); |
| 87 | } |
| 88 | } else if (protectedHtmlDepth === 0) visit(child, inLink); |
| 89 | } |
| 90 | } |
| 91 | }; |
| 92 | visit(parser.parse(markdown), false); |
| 93 | if (edits.length === 0) return { markdown, sourceOffset: unchanged }; |
| 94 | let rewritten = ""; |
| 95 | let cursor = 0; |
| 96 | // Where each edit ends in the result, and how far `markdown` is ahead from there on. |
| 97 | const shifts: { end: number; delta: number }[] = []; |
| 98 | for (const edit of edits) { |
| 99 | rewritten += markdown.slice(cursor, edit.start) + edit.value; |
| 100 | cursor = edit.end; |
| 101 | shifts.push({ end: rewritten.length, delta: edit.end - rewritten.length }); |
| 102 | } |
| 103 | rewritten += markdown.slice(cursor); |
| 104 | return { |
| 105 | markdown: rewritten, |
| 106 | sourceOffset(offset) { |
| 107 | let delta = 0; |
| 108 | for (const shift of shifts) { |
| 109 | if (shift.end > offset) break; |
| 110 | delta = shift.delta; |
| 111 | } |
| 112 | return offset + delta; |
| 113 | }, |
| 114 | }; |
| 115 | } |