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 type { MouseEvent as ReactMouseEvent } from "react"; |
| 2 | |
| 3 | import { hastPlainTextDeep } from "./markdownHast.logic"; |
| 4 | |
| 5 | const SANITIZED_FRAGMENT_PREFIX = "user-content-"; |
| 6 | |
| 7 | function decodeMarkdownFragmentId(href: string): string { |
| 8 | const encodedId = href.slice(1); |
| 9 | try { |
| 10 | return decodeURIComponent(encodedId); |
| 11 | } catch { |
| 12 | return encodedId; |
| 13 | } |
| 14 | } |
| 15 | |
| 16 | function normalizeSanitizedFragmentId(id: string): string { |
| 17 | let normalizedId = id; |
| 18 | while (normalizedId.startsWith(SANITIZED_FRAGMENT_PREFIX)) { |
| 19 | normalizedId = normalizedId.slice(SANITIZED_FRAGMENT_PREFIX.length); |
| 20 | } |
| 21 | return normalizedId; |
| 22 | } |
| 23 | |
| 24 | function findMarkdownFragmentTarget(anchor: HTMLAnchorElement, href: string): HTMLElement | null { |
| 25 | const decodedId = decodeMarkdownFragmentId(href); |
| 26 | const normalizedId = normalizeSanitizedFragmentId(decodedId); |
| 27 | const matchesFragment = (element: HTMLElement) => |
| 28 | element.id === decodedId || normalizeSanitizedFragmentId(element.id) === normalizedId; |
| 29 | const markdownRoot = anchor.closest<HTMLElement>(".chat-markdown"); |
| 30 | if (markdownRoot) { |
| 31 | const localTargets = Array.from(markdownRoot.querySelectorAll<HTMLElement>("[id]")); |
| 32 | const localTarget = localTargets.find(matchesFragment); |
| 33 | if (localTarget) return localTarget; |
| 34 | } |
| 35 | |
| 36 | return ( |
| 37 | document.getElementById(decodedId) ?? |
| 38 | Array.from(document.querySelectorAll<HTMLElement>("[id]")).find(matchesFragment) ?? |
| 39 | null |
| 40 | ); |
| 41 | } |
| 42 | |
| 43 | export function handleMarkdownFragmentClick( |
| 44 | event: ReactMouseEvent<HTMLAnchorElement>, |
| 45 | href: string, |
| 46 | ) { |
| 47 | if ( |
| 48 | event.defaultPrevented || |
| 49 | event.button !== 0 || |
| 50 | event.metaKey || |
| 51 | event.ctrlKey || |
| 52 | event.shiftKey || |
| 53 | event.altKey |
| 54 | ) { |
| 55 | return; |
| 56 | } |
| 57 | |
| 58 | // Never let the browser follow the fragment or write it to the URL: desktop keeps |
| 59 | // its route in the hash, so replacing the hash navigates away from the thread. |
| 60 | event.preventDefault(); |
| 61 | findMarkdownFragmentTarget(event.currentTarget, href)?.scrollIntoView({ block: "start" }); |
| 62 | } |
| 63 | |
| 64 | type HeadingHastNode = { |
| 65 | type?: string; |
| 66 | tagName?: string; |
| 67 | properties?: Record<string, unknown>; |
| 68 | children?: HeadingHastNode[]; |
| 69 | }; |
| 70 | |
| 71 | export function githubHeadingSlug(text: string): string { |
| 72 | return text |
| 73 | .trim() |
| 74 | .toLowerCase() |
| 75 | .replace(/[^\p{L}\p{M}\p{N}\p{Pc} -]/gu, "") |
| 76 | .replace(/ /g, "-"); |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Like the sanitizer's ids, they carry the `user-content-` prefix so they cannot clobber app |
| 81 | * element ids; fragment lookup strips it. |
| 82 | */ |
| 83 | export function rehypeHeadingIds() { |
| 84 | return (tree: HeadingHastNode) => { |
| 85 | const taken = new Set<string>(); |
| 86 | const collect = (node: HeadingHastNode) => { |
| 87 | const id = node.properties?.id; |
| 88 | if (typeof id === "string") taken.add(id); |
| 89 | node.children?.forEach(collect); |
| 90 | }; |
| 91 | collect(tree); |
| 92 | const nextSuffix = new Map<string, number>(); |
| 93 | const visit = (node: HeadingHastNode) => { |
| 94 | if (node.type === "element" && node.tagName && /^h[1-6]$/.test(node.tagName)) { |
| 95 | const slug = githubHeadingSlug(hastPlainTextDeep(node)); |
| 96 | if (node.properties?.id === undefined && slug) { |
| 97 | let count = nextSuffix.get(slug) ?? 0; |
| 98 | let id = `${SANITIZED_FRAGMENT_PREFIX}${slug}`; |
| 99 | while (taken.has(id)) { |
| 100 | count += 1; |
| 101 | id = `${SANITIZED_FRAGMENT_PREFIX}${slug}-${count}`; |
| 102 | } |
| 103 | nextSuffix.set(slug, count); |
| 104 | taken.add(id); |
| 105 | node.properties = { ...node.properties, id }; |
| 106 | } |
| 107 | return; |
| 108 | } |
| 109 | node.children?.forEach(visit); |
| 110 | }; |
| 111 | visit(tree); |
| 112 | }; |
| 113 | } |