|
| 1 | +import { parseFragment, type DefaultTreeAdapterTypes } from "parse5"; |
| 2 | +import remarkGfm from "remark-gfm"; |
| 3 | +import remarkParse from "remark-parse"; |
| 4 | +import { unified } from "unified"; |
| 5 | + |
| 6 | +const markdownParser = unified().use(remarkParse).use(remarkGfm).freeze(); |
| 7 | +const mentionPattern = /@\{([^{}\s]+)\}/gu; |
| 8 | +const ignoredMarkdownNodes = new Set([ |
| 9 | + "code", |
| 10 | + "inlineCode", |
| 11 | + "html", |
| 12 | + "link", |
| 13 | + "linkReference", |
| 14 | + "image", |
| 15 | + "imageReference", |
| 16 | +]); |
| 17 | + |
| 18 | +type MarkdownNode = { |
| 19 | + type: string; |
| 20 | + position?: |
| 21 | + | { start: { offset?: number | undefined }; end: { offset?: number | undefined } } |
| 22 | + | undefined; |
| 23 | + children?: MarkdownNode[] | undefined; |
| 24 | +}; |
| 25 | + |
| 26 | +function htmlContentRanges(body: string, tree: MarkdownNode) { |
| 27 | + const fragments: string[] = []; |
| 28 | + let cursor = 0; |
| 29 | + const collect = (node: MarkdownNode) => { |
| 30 | + const start = node.position?.start.offset; |
| 31 | + const end = node.position?.end.offset; |
| 32 | + if (node.type === "html" && start !== undefined && end !== undefined) { |
| 33 | + fragments.push(" ".repeat(start - cursor), body.slice(start, end)); |
| 34 | + cursor = end; |
| 35 | + } else { |
| 36 | + node.children?.forEach(collect); |
| 37 | + } |
| 38 | + }; |
| 39 | + collect(tree); |
| 40 | + const ranges: { start: number; end: number }[] = []; |
| 41 | + if (fragments.length === 0) return ranges; |
| 42 | + fragments.push(" ".repeat(body.length - cursor)); |
| 43 | + const visit = (node: DefaultTreeAdapterTypes.Node) => { |
| 44 | + const location = "tagName" in node ? node.sourceCodeLocation : undefined; |
| 45 | + const start = location?.startTag?.endOffset; |
| 46 | + const end = location?.endTag?.startOffset ?? location?.endOffset; |
| 47 | + if (start !== undefined && end !== undefined && end > start) { |
| 48 | + ranges.push({ start, end }); |
| 49 | + return; |
| 50 | + } |
| 51 | + if ("childNodes" in node) node.childNodes.forEach(visit); |
| 52 | + }; |
| 53 | + visit(parseFragment(fragments.join(""), { sourceCodeLocationInfo: true })); |
| 54 | + return ranges; |
| 55 | +} |
| 56 | + |
| 57 | +function mentionLabels(html: string) { |
| 58 | + const labels = new Map<string, string>(); |
| 59 | + const conflicts = new Set<string>(); |
| 60 | + const text = (node: DefaultTreeAdapterTypes.Node): string => |
| 61 | + "value" in node ? node.value : "childNodes" in node ? node.childNodes.map(text).join("") : ""; |
| 62 | + const visit = (node: DefaultTreeAdapterTypes.Node) => { |
| 63 | + if ("tagName" in node) { |
| 64 | + if (["pre", "code", "script", "style"].includes(node.tagName)) return; |
| 65 | + const attribute = (name: string) => node.attrs.find((entry) => entry.name === name)?.value; |
| 66 | + const id = attribute("data-atlassian-id"); |
| 67 | + if (id && attribute("class")?.split(/\s+/u).includes("ap-mention")) { |
| 68 | + const label = text(node).replace(/\s+/gu, " ").trim(); |
| 69 | + if (label.startsWith("@") && label.length > 1) { |
| 70 | + if (labels.has(id) && labels.get(id) !== label) conflicts.add(id); |
| 71 | + labels.set(id, label); |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | + if ("childNodes" in node) node.childNodes.forEach(visit); |
| 76 | + }; |
| 77 | + visit(parseFragment(html)); |
| 78 | + conflicts.forEach((id) => labels.delete(id)); |
| 79 | + return labels; |
| 80 | +} |
| 81 | + |
| 82 | +/** Resolves prose mentions for display without changing the Markdown used for edits. */ |
| 83 | +export function bitbucketMentionDisplayBody(body: string, html: unknown) { |
| 84 | + if (typeof html !== "string" || !html || !body.includes("@{")) return undefined; |
| 85 | + const labels = mentionLabels(html); |
| 86 | + if (labels.size === 0) return undefined; |
| 87 | + const tree = markdownParser.parse(body); |
| 88 | + const htmlRanges = htmlContentRanges(body, tree); |
| 89 | + const replacements: { start: number; end: number; label: string }[] = []; |
| 90 | + const visit = (node: MarkdownNode) => { |
| 91 | + if (ignoredMarkdownNodes.has(node.type)) return; |
| 92 | + const start = node.position?.start.offset; |
| 93 | + const end = node.position?.end.offset; |
| 94 | + if (node.type === "text" && start !== undefined && end !== undefined) { |
| 95 | + const source = body.slice(start, end); |
| 96 | + for (const match of source.matchAll(mentionPattern)) { |
| 97 | + if ( |
| 98 | + htmlRanges.some( |
| 99 | + (range) => start + match.index >= range.start && start + match.index < range.end, |
| 100 | + ) |
| 101 | + ) |
| 102 | + continue; |
| 103 | + let escapes = 0; |
| 104 | + for (let index = match.index - 1; index >= 0 && source[index] === "\\"; index--) escapes++; |
| 105 | + if (escapes % 2 !== 0) continue; |
| 106 | + const label = labels.get(match[1]!); |
| 107 | + if (label === undefined) continue; |
| 108 | + replacements.push({ |
| 109 | + start: start + match.index, |
| 110 | + end: start + match.index + match[0].length, |
| 111 | + label: label.replace(/[\\`*_{}[\]()<>!#|~&]/gu, "\\$&"), |
| 112 | + }); |
| 113 | + } |
| 114 | + } |
| 115 | + node.children?.forEach(visit); |
| 116 | + }; |
| 117 | + visit(tree); |
| 118 | + if (replacements.length === 0) return undefined; |
| 119 | + let displayBody = body; |
| 120 | + for (const replacement of replacements.toReversed()) { |
| 121 | + displayBody = |
| 122 | + displayBody.slice(0, replacement.start) + |
| 123 | + replacement.label + |
| 124 | + displayBody.slice(replacement.end); |
| 125 | + } |
| 126 | + return displayBody === body ? undefined : displayBody; |
| 127 | +} |
0 commit comments