feat(viewers): outlines for Markdown and gists, refined pull request page

Markdown: the "On this page" outline covers h1-h3, draws redaction slots
like the body, treats a lone opening h1 as the title, highlights the
section past the reading line, and shows its menu from 768 to 1199px too.

Gists: one card per file and an outline of the files (name, language,
lines) with Markdown headings nested under each file; sticky right rail
on wide screens, an "In this gist" menu on narrow ones.

Pull requests: header with state, anonymized author, branches and
commit count; Conversation, Commits and Files changed tabs; a file list
with +/- counts, per-file collapse, inline review threads, and redaction
slots in the title, body and diff. The server now stores the author,
branch names, commits (no SHAs) and review comments, grouped into
threads; older pull requests render without them.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Thomas DurieuxandClaude Opus 5.5 committed 2026-11-29 08:13:31 +00:00
1 parent 0640d741f1
commit 176bc857a1
22 files changed
+1515 -295

No files matched your search

+9 -2
View File
@@ -29,6 +29,13 @@ function harness(date) {
context.createListeners = () => (target, name, callback) => target.addEventListener(name, callback);
vm.runInNewContext(fs.readFileSync(path.join(__dirname, "../public/script/billing.js"), "utf8").replaceAll("export function", "function"), context);
context.createPageState = parent => Object.create(parent);
context.nextTick = fn => Promise.resolve().then(fn);
context.window.document = context.document;
// Modules the controllers import: outline helpers, the diff parser and the reviewer pages.
for (const name of ["outline.js", "formatters.js", "review-pages.js"]) {
const moduleSource = fs.readFileSync(path.join(__dirname, "../public/script", name), "utf8");
vm.runInNewContext(moduleSource.replace(/^import .*;$/gm, "").replace(/export function/g, "function").replace(/export const/g, "var"), context);
}
const viewerSource = fs.readFileSync(path.join(__dirname, "../public/script/file-viewer.js"), "utf8");
vm.runInNewContext(viewerSource.replace(/^import .*;$/gm, "").replace(/export const/g, "var"), context);
const names = [...source.matchAll(/export const (\w+)/g)].map(match => match[1]);
@@ -358,11 +365,11 @@ describe("frontend production regressions", function () {
request.resolve({ data: { contents: ["title", "MASK", "MASK comment"] } }); await h.flush();
expect(h.scope.anonymizePrContent("Alice")).to.equal("MASK");
});
it("selects the diff tab after asynchronous PR loading", async function () {
it("selects the files tab after asynchronous PR loading when there is no conversation", async function () {
const h = harness(); h.defs.pullRequestController(h.scope, h.http, {}, { pullRequestId: "pr" }, {});
h.requests[0].resolve({ data: {} }); await h.flush();
h.requests[1].resolve({ data: { diff: "patch" } }); await h.flush();
expect(h.scope.tabState.active).to.equal("diff");
expect(h.scope.tabState.active).to.equal("files");
const template = fs.readFileSync(path.join(__dirname, "../public/partials/pullRequest.htm"), "utf8");
expect(template).not.to.include('ng-init="tabState');
});
+107
View File
@@ -0,0 +1,107 @@
const { expect } = require("chai");
require("ts-node/register/transpile-only");
const { default: PullRequest, reviewThreads } = require("../src/core/PullRequest");
const at = (day) => new Date(`2026-03-${String(day).padStart(2, "0")}T10:00:00Z`);
function makePullRequest(options) {
return new PullRequest({
pullRequestId: "pr-test",
owner: "000000000000000000000001",
anonymizeDate: at(20),
source: { pullRequestId: 12, repositoryFullName: "alice/secret-project" },
options: { terms: ["Alice", "secret-project"], image: true, link: true, ...options },
pullRequest: {
title: "Fix by Alice",
body: "Body",
diff: "--- a/x\n+++ b/x\n@@ -1 +1 @@\n-Alice\n+Bob\n",
state: "open",
author: "Alice",
baseRef: "main",
headRef: "Alice/fix-secret-project",
creationDate: at(1),
updatedDate: at(2),
comments: [],
commits: [
{ message: "First commit by Alice\n\nDetails for secret-project", author: "Alice", date: at(3) },
{ message: "Second", author: "bob", date: at(4) },
],
reviewComments: [
{ commentId: 11, path: "src/Alice.py", line: 4, body: "Why Alice?", author: "bob", creationDate: at(5), updatedDate: at(5) },
{ commentId: 13, inReplyTo: 11, path: "src/Alice.py", line: 4, body: "Because", author: "Alice", creationDate: at(7), updatedDate: at(7) },
{ commentId: 12, path: "README.md", line: 1, body: "Typo", author: "carol", creationDate: at(6), updatedDate: at(6) },
],
},
});
}
const all = { title: true, body: true, comments: true, diff: true, username: true, date: true, origin: false };
describe("PullRequest.content() commits, branches and review threads", function () {
it("anonymizes the author and branch names", function () {
const out = makePullRequest(all).content();
expect(out.author).to.equal("XXXX-1");
expect(out.baseRef).to.equal("main");
expect(out.headRef).to.equal("XXXX-1/fix-XXXX-2");
});
it("hides the author without the username option", function () {
const out = makePullRequest({ ...all, username: false }).content();
expect(out.author).to.be.undefined;
expect(out.commits[0]).to.not.have.property("author");
expect(out.reviewComments[0].comments[0]).to.not.have.property("author");
});
it("lists commits with anonymized messages and no SHA", function () {
const out = makePullRequest(all).content();
expect(out.commits).to.have.length(2);
expect(out.commits[0].message).to.equal("First commit by XXXX-1\n\nDetails for XXXX-2");
expect(out.commits[0].author).to.equal("XXXX-1");
expect(out.commits[0].date).to.deep.equal(at(3));
expect(Object.keys(out.commits[0])).to.have.members(["message", "author", "date"]);
});
it("drops commits when the diff is hidden, and dates when dates are hidden", function () {
expect(makePullRequest({ ...all, diff: false }).content().commits).to.be.undefined;
expect(makePullRequest({ ...all, date: false }).content().commits[0]).to.not.have.property("date");
});
it("groups review comments into threads without GitHub ids", function () {
const out = makePullRequest(all).content();
expect(out.reviewComments).to.have.length(2);
const [first, second] = out.reviewComments;
expect(first.path).to.equal("src/XXXX-1.py");
expect(first.line).to.equal(4);
expect(first.comments.map((c) => c.body)).to.deep.equal(["Why XXXX-1?", "Because"]);
expect(first.comments[1].author).to.equal("XXXX-1");
expect(second.path).to.equal("README.md");
expect(JSON.stringify(out.reviewComments)).to.not.match(/commentId|inReplyTo|"1[123]"/);
});
it("omits review threads without the comments option", function () {
expect(makePullRequest({ ...all, comments: false }).content().reviewComments).to.be.undefined;
});
it("keeps pull requests stored before commits and review comments readable", function () {
const pr = makePullRequest(all);
delete pr.model.pullRequest.commits;
delete pr.model.pullRequest.reviewComments;
const out = pr.content();
expect(out.commits).to.be.undefined;
expect(out.reviewComments).to.be.undefined;
expect(out.diff).to.include("XXXX-1");
});
});
describe("reviewThreads()", function () {
it("attaches replies to their thread even when they arrive out of order", function () {
const threads = reviewThreads([
{ commentId: 3, inReplyTo: 1, path: "a", line: 2, body: "reply 2", author: "x", creationDate: at(9), updatedDate: at(9) },
{ commentId: 1, path: "a", line: 2, body: "root", author: "x", creationDate: at(1), updatedDate: at(1) },
{ commentId: 2, inReplyTo: 1, path: "a", line: 2, body: "reply 1", author: "y", creationDate: at(2), updatedDate: at(2) },
{ commentId: 4, inReplyTo: 99, path: "b", line: 5, body: "orphan", author: "z", creationDate: at(3), updatedDate: at(3) },
]);
expect(threads.map((t) => t.comments.map((c) => c.body))).to.deep.equal([["root", "reply 1", "reply 2"], ["orphan"]]);
expect(threads[1]).to.include({ path: "b", line: 5 });
});
});
+120
View File
@@ -0,0 +1,120 @@
const { expect } = require("chai");
const { JSDOM } = require("jsdom");
const path = require("path");
const { pathToFileURL } = require("url");
const load = (name) => import(pathToFileURL(path.join(__dirname, "../public/script", name)).href);
describe("Reviewer page outlines and diffs", function () {
let outline, formatters, pages, dom, vue;
// The pages register their scroll listeners in the controller's effect scope.
const inScope = (fn) => { const scope = vue.effectScope(); scope.run(fn); return scope; };
before(async () => {
dom = new JSDOM("<!doctype html><body></body>", { pretendToBeVisual: true });
global.window = dom.window;
outline = await load("outline.js");
formatters = await load("formatters.js");
pages = await load("review-pages.js");
vue = await import("vue");
});
after(() => { delete global.window; dom.window.close(); });
it("splits redaction masks into slot parts", () => {
expect(outline.redactParts("Results from XXXX-2 cluster")).to.deep.equal([{ t: "Results from " }, { n: "2" }, { t: " cluster" }]);
expect(outline.redactParts("")).to.deep.equal([]);
});
it("outlines h1-h3 with redaction slots read back from the chips", () => {
const html = '<h1 id="t">Guide</h1><h2 id="a">Setup</h2><h3 id="b">Data</h3><h4 id="c">Skip</h4>'
+ '<h2 id="r">Results from <mark class="redact-chip" data-term="XXXX-2"><i>2</i></mark> cluster</h2>';
const list = outline.headingOutline(html);
expect(list.map((h) => [h.id, h.level, h.depth])).to.deep.equal([["t", 1, 0], ["a", 2, 1], ["b", 3, 2], ["r", 2, 1]]);
expect(list[3].parts).to.deep.equal([{ t: "Results from " }, { n: "2" }, { t: " cluster" }]);
});
it("starts depth at the shallowest heading", () => {
const list = outline.headingOutline('<h2 id="a">A</h2><h3 id="b">B</h3>');
expect(list.map((h) => h.depth)).to.deep.equal([0, 1]);
});
it("highlights the last section above the reading line", () => {
const doc = dom.window.document;
doc.body.innerHTML = '<h2 id="one">1</h2><h2 id="two">2</h2><h2 id="three">3</h2>';
const tops = { one: -400, two: 120, three: 700 };
for (const id of Object.keys(tops)) doc.getElementById(id).getBoundingClientRect = () => ({ top: tops[id] });
const view = { innerHeight: 900, scrollY: 400 };
expect(outline.activeSection(["one", "two", "three"], { doc, view })).to.equal("two");
tops.two = 400;
expect(outline.activeSection(["one", "two", "three"], { doc, view })).to.equal("one");
tops.one = 50;
tops.two = 300; tops.three = 600;
expect(outline.activeSection(["one", "two", "three"], { doc, view: { innerHeight: 900, scrollY: 0 } })).to.equal("one");
});
it("parses a diff into files with status and counts, without a phantom last line", () => {
const files = formatters.parseDiff([
"diff --git a/a.py b/a.py", "--- a/a.py", "+++ b/a.py", "@@ -1,2 +1,2 @@", " keep", "-old", "+new",
"diff --git a/gone.sh b/gone.sh", "deleted file mode 100644", "--- a/gone.sh", "+++ /dev/null", "@@ -1,2 +0,0 @@", "-x", "-y",
"diff --git a/new.md b/new.md", "new file mode 100644", "--- /dev/null", "+++ b/new.md", "@@ -0,0 +1 @@", "+hi",
"\\ No newline at end of file", "",
].join("\n"));
expect(files.map((f) => [f.path, f.status, f.additions, f.deletions, f.lines.length])).to.deep.equal([
["a.py", "modified", 1, 1, 4], ["gone.sh", "deleted", 0, 2, 3], ["new.md", "added", 1, 0, 2],
]);
expect(files[0].lines[3]).to.include({ kind: "add", newNo: 2 });
});
it("renders the legacy diff HTML from the same parser", () => {
const html = formatters.diff("--- a/x\n+++ b/x\n@@ -1 +1 @@\n-a<b\n+b\n");
expect(html).to.include('diff-file-status-modified').and.include("a&lt;b");
expect((html.match(/<tr/g) || []).length).to.equal(3);
});
it("places review threads on their diff line and keeps the rest at the end of the file", () => {
const state = { details: {
diff: "--- a/src/a.py\n+++ b/src/a.py\n@@ -1,2 +1,3 @@\n one\n+two\n three\n",
reviewComments: [{ path: "src/a.py", line: 2, comments: [{ body: "here" }] }, { path: "src/a.py", line: 40, comments: [{ body: "elsewhere" }] }, { path: "other", line: 1, comments: [] }],
comments: [{ body: "c", creationDate: "2026-03-02" }],
commits: [{ message: "First\n\nBody", date: "2026-03-01" }, { message: "Second", date: "2026-03-01" }],
}, fmt: { date: (d) => d.slice(0, 10) } };
const scope = inScope(() => pages.setupPullRequestPage(state));
const [file] = state.prFiles();
expect(file).to.include({ path: "src/a.py", dir: "src/", name: "a.py", threads: 2, id: "diff-1" });
expect(file.lines.find((l) => l.threads)).to.include({ newNo: 2, text: "two" });
expect(file.looseThreads.map((t) => t.line)).to.deep.equal([40]);
expect(state.prStats()).to.deep.equal({ files: 1, additions: 1, deletions: 0 });
expect(state.diffBar(file)).to.deep.equal(["add", "add", "add", "add", "add"]);
expect(state.commitGroups()).to.deep.equal([{ day: "2026-03-01", commits: [
{ message: "First\n\nBody", date: "2026-03-01", index: 1, headline: "First", detail: "Body" },
{ message: "Second", date: "2026-03-01", index: 2, headline: "Second", detail: "" },
] }]);
expect(state.prTimeline().map((i) => i.kind)).to.deep.equal(["comment", "review", "review", "review"]);
state.toggleAllFiles();
expect(state.allCollapsed()).to.equal(true);
scope.stop();
});
it("opens a pull request on its conversation, or on the files without one", () => {
expect(pages.defaultPullRequestTab({ body: "x", diff: "d" })).to.equal("conversation");
expect(pages.defaultPullRequestTab({ diff: "d", comments: [] })).to.equal("files");
expect(pages.defaultPullRequestTab({ commits: [{}] })).to.equal("commits");
});
it("lists gist files with language, lines and nested Markdown headings", () => {
const state = { tabState: { active: "files" }, details: { files: [
{ filename: "NOTES-XXXX-1.md", language: "Markdown", content: "# A\n\n## B\n" },
{ filename: "run.sh", language: "Shell", content: "a\nb\nc\n" },
] } };
const scope = inScope(() => pages.setupGistPage(state));
state.setFileOutline(0, [{ id: "file-1-a", level: 1, depth: 0, parts: [{ t: "A" }] }]);
const toc = state.gistToc();
expect(toc.map((f) => [f.id, f.language, f.lines, f.headings.length])).to.deep.equal([["file-1", "Markdown", 3, 1], ["file-2", "Shell", 3, 0]]);
expect(toc[0].parts).to.deep.equal([{ t: "NOTES-" }, { n: "1" }, { t: ".md" }]);
expect(state.showGistToc()).to.equal(true);
state.activeAnchor = "file-1-a";
expect(state.activeFileIndex()).to.equal(0);
state.details.files.pop();
expect(state.showGistToc()).to.equal(false);
scope.stop();
});
});
+81
View File
@@ -2977,4 +2977,85 @@ describe("Vue 3 UI", function () {
}
expect(ui.app.router.currentRoute.value.path).to.equal("/");
});
it("renders the pull request header, threaded conversation, commits and collapsible files", async function () {
const diff = "diff --git a/src/a.py b/src/a.py\n--- a/src/a.py\n+++ b/src/a.py\n@@ -1,2 +1,3 @@\n one\n+two by XXXX-1\n three\n"
+ "diff --git a/old.sh b/old.sh\ndeleted file mode 100644\n--- a/old.sh\n+++ /dev/null\n@@ -1 +0,0 @@\n-gone\n";
ui = await browser("/pr/pr-1/", {
"/api/pr/pr-1/options": { url: null },
"/api/pr/pr-1/content": {
state: "open", title: "Fix XXXX-1 harness", author: "XXXX-2", baseRef: "main", headRef: "XXXX-2/fix", diff,
comments: [{ author: "XXXX-3", body: "Looks good", creationDate: "2026-03-03" }],
reviewComments: [{ path: "src/a.py", line: 2, comments: [{ author: "XXXX-3", body: "Why?", creationDate: "2026-03-02" }, { author: "XXXX-2", body: "Because", creationDate: "2026-03-04" }] }],
commits: [{ message: "First", author: "XXXX-2", date: "2026-03-01" }],
},
});
await delay(60);
const doc = ui.window.document;
expect(doc.querySelector(".pr-title").textContent.replace(/\s+/g, " ").trim()).to.equal("Fix 1 harness");
expect(doc.querySelectorAll(".pr-title .redact-chip")).to.have.length(1);
expect(doc.querySelector(".pr-state").textContent.trim()).to.equal("Open");
expect(doc.querySelector(".pr-summary").textContent.replace(/\s+/g, " ")).to.include("wants to merge 1 commit into main from 2/fix");
const tabs = [...doc.querySelectorAll(".pr-tabs [role=tab]")].map((t) => t.textContent.replace(/\s+/g, " ").trim());
expect(tabs).to.deep.equal(["Conversation2", "Commits1", "Files changed2"]);
expect(doc.querySelector(".pr-tabs [aria-selected=true]").id).to.equal("pr-conversation-tab");
expect(doc.querySelectorAll(".pr-timeline-item")).to.have.length(3);
expect(doc.querySelectorAll(".pr-timeline-item.is-review .pr-thread-comment.is-reply")).to.have.length(1);
doc.querySelector("#pr-commits-tab").click(); await delay(20);
expect(doc.querySelector(".pr-commit-headline").textContent).to.equal("First");
doc.querySelector("#pr-files-tab").click(); await delay(20);
const blocks = doc.querySelectorAll(".diff-file-block");
expect(blocks).to.have.length(2);
expect(doc.querySelectorAll(".pr-file-toc .toc-diff-file")).to.have.length(2);
expect(blocks[0].querySelector(".diff-file-stat").textContent).to.include("+1");
expect(blocks[0].querySelectorAll(".diff-thread-row")).to.have.length(1);
expect(blocks[0].querySelector(".diff-row-add .redact-chip")).to.not.equal(null);
expect(blocks[1].querySelector(".diff-file-status").textContent).to.equal("deleted");
blocks[0].querySelector(".diff-file-toggle").click(); await delay(20);
expect(blocks[0].querySelector(".diff-file-body")).to.equal(null);
expect(blocks[0].querySelector(".diff-file-toggle").getAttribute("aria-expanded")).to.equal("false");
expect(ui.errors).to.deep.equal([]);
});
it("outlines a gist's files on the files tab", async function () {
ui = await browser("/gist/g-1/", {
"/api/gist/g-1/options": { url: null },
"/api/gist/g-1/content": { description: "Notes", files: [
{ filename: "a-XXXX-1.py", language: "Python", content: "x = 1\ny = 2\n" },
{ filename: "b.json", language: "JSON", content: "{}\n" },
] },
});
await delay(60);
const doc = ui.window.document;
const items = [...doc.querySelectorAll(".page-toc .toc-file")];
expect(items.map((a) => a.getAttribute("href"))).to.deep.equal(["#file-1", "#file-2"]);
expect(items[0].querySelector(".toc-file-meta").textContent).to.equal("Python · 2 lines");
expect(items[0].querySelector(".redact-chip")).to.not.equal(null);
expect(items[0].classList.contains("is-active")).to.equal(true);
expect(doc.querySelectorAll(".gist-file-card")).to.have.length(2);
expect(doc.getElementById("file-2")).to.not.equal(null);
expect(ui.errors).to.deep.equal([]);
});
it("outlines Markdown h1-h3 with redaction slots and hides short outlines", async function () {
const doc = "# Guide\n\nIntro.\n\n## Setup\n\nText.\n\n### Data\n\nText.\n\n## Results from XXXX-2\n\nText.\n";
ui = await browser("/r/test/GUIDE.md", {
"/api/repo/test/files/": [{ name: "GUIDE.md", path: "", sha: "1", size: 80 }, { name: "SHORT.md", path: "", sha: "2", size: 20 }],
"/api/repo/test/file/GUIDE.md": doc,
"/api/repo/test/file/SHORT.md": "# Short\n\n## Only\n",
});
await delay(150);
const document = ui.window.document;
const rail = [...document.querySelectorAll(".readme-toc .toc-rail a")];
expect(rail.map((a) => a.textContent.trim())).to.deep.equal(["Guide", "Setup", "Data", "Results from 2"]);
expect(rail.map((a) => a.className.match(/lvl-\d/)[0])).to.deep.equal(["lvl-2", "lvl-2", "lvl-3", "lvl-2"]);
expect(rail[3].querySelector("mark.redact-chip")).to.not.equal(null);
expect(rail[1].getAttribute("href")).to.equal("#" + document.querySelector(".markdown-body h2").id);
expect(document.querySelectorAll(".tb-toc-menu, .tb-toc-m")).to.have.length(1);
await ui.go("/r/test/SHORT.md");
await delay(150);
expect(document.querySelector(".readme-toc")).to.equal(null);
expect(document.querySelector(".tb-toc-m")).to.equal(null);
expect(ui.errors).to.deep.equal([]);
});
});