another out-of-diff comment
', + 'x
another out-of-diff comment
' + ); +}); + +test("leaves bodies without a permalink untouched", () => { + expect(stripOutOfDiffPermalink("plain")).toBe("plain"); + expect(stripOutOfDiffPermalinkHtml("plain
")).toBe("plain
"); +}); diff --git a/src/shared/out-of-diff.ts b/src/shared/out-of-diff.ts new file mode 100644 index 0000000..489ea53 --- /dev/null +++ b/src/shared/out-of-diff.ts @@ -0,0 +1,69 @@ +export const OUT_OF_DIFF_MARKER = "/; + +export function parseOutOfDiffMarker(body: string): OutOfDiffInfo | null { + const match = body.match(MARKER_RE); + if (!match) return null; + return { + line: parseInt(match[2], 10), + startLine: match[3] ? parseInt(match[3], 10) : undefined, + side: match[4] as "LEFT" | "RIGHT", + ...(match[1] ? { sha: match[1] } : {}), + }; +} + +export function isOutOfDiffComment(body?: string | null): boolean { + return !!body?.includes(OUT_OF_DIFF_MARKER); +} + +export function buildOutOfDiffMarker( + line: number, + startLine: number | undefined, + side: "LEFT" | "RIGHT", + sha?: string +): string { + const withSha = sha ? ` sha=${sha}` : ""; + const start = startLine !== undefined ? ` start_line=${startLine}` : ""; + return ``; +} + +/** The blob permalink appended to out-of-diff comments, matched anywhere in + * a body (raw markdown or pre-rendered HTML). */ +const PERMALINK_RE = + /(?:^|\n|