Public: anyone can read this; the crew can change it.
Meta tags on every page: og/twitter tags with a description written from the page's live state, private-safe #995
scout/forge:scout/453-meta-tags-on-every-page-og-twitter-tags → main 5 commits fork
7 files
changed +115
−3
src/web/layout.ts +13
| @@ -21,6 +21,7 @@ import { | ||
| 21 | 21 | THEMES, |
| 22 | 22 | type ThemeName, |
| 23 | 23 | } from "../design/themes.ts"; |
| 24 | import { PRIVATE_LINE, type Visibility } from "./preview.ts"; | |
| 24 | 25 | import { stylesheetAsset } from "./asset.ts"; |
| 25 | 26 | import { linkHeader, pageLinks } from "./standards.ts"; |
| 26 | 27 | import { shellJs } from "./client.ts"; |
| @@ -52,6 +53,11 @@ export interface PageOptions { | ||
| 52 | 53 | * an issue's first line. Absent, the forge's own line stands in. |
| 53 | 54 | */ |
| 54 | 55 | description?: string; |
| 56 | /** | |
| 57 | * What the page is about, for a preview. `private` previews as the title | |
| 58 | * and the word "private" — the description is dropped, whoever asks. | |
| 59 | */ | |
| 60 | visibility?: Visibility; | |
| 55 | 61 | /** |
| 56 | 62 | * The shell without its column width: a page whose content earns a wide |
| 57 | 63 | * monitor (the code browser, a tree beside a file). The topbar and footer |
| @@ -497,6 +503,7 @@ function footer(): Html { | ||
| 497 | 503 | const SITE_LINE = "Your code, your machine."; |
| 498 | 504 | |
| 499 | 505 | function pageDescription(options: PageOptions): string { |
| 506 | if (options.visibility === "private") return PRIVATE_LINE; | |
| 500 | 507 | const own = options.description?.trim().split(/\r?\n/)[0]?.trim() ?? ""; |
| 501 | 508 | const line = own === "" ? SITE_LINE : own; |
| 502 | 509 | return line.length <= 200 ? line : `${line.slice(0, 197).trimEnd()}…`; |
| @@ -562,6 +569,12 @@ export function page(options: PageOptions, status = 200): Response { | ||
| 562 | 569 | content="${ctx.url.origin}/icons/forge-512.png" |
| 563 | 570 | /> |
| 564 | 571 | <meta name="twitter:card" content="summary" /> |
| 572 | <meta name="twitter:title" content="${options.title}" /> | |
| 573 | <meta name="twitter:description" content="${pageDescription(options)}" /> | |
| 574 | <meta | |
| 575 | name="twitter:image" | |
| 576 | content="${ctx.url.origin}/icons/forge-512.png" | |
| 577 | /> | |
| 565 | 578 | <link rel="manifest" href="/manifest.webmanifest" /> |
| 566 | 579 | ${links.map( |
| 567 | 580 | (l) => |
src/web/pages/pull.ts +9
| @@ -65,6 +65,7 @@ import { html, raw, when, type Html } from "../html.ts"; | ||
| 65 | 65 | import { renderMarkdown } from "../markdown.ts"; |
| 66 | 66 | import type { IconName as Glyph } from "../icons.ts"; |
| 67 | 67 | import { flashTo, page, redirect } from "../layout.ts"; |
| 68 | import { threadLine } from "../preview.ts"; | |
| 68 | 69 | import { liveClientJs, liveMount } from "../live.ts"; |
| 69 | 70 | import { |
| 70 | 71 | avatar, |
| @@ -911,6 +912,14 @@ export async function pullPage(ctx: Ctx): Promise<Response> { | ||
| 911 | 912 | return page({ |
| 912 | 913 | ctx, |
| 913 | 914 | title: `${pull.title} · ${repo.name}`, |
| 915 | description: threadLine({ | |
| 916 | kind: "pull", | |
| 917 | state: pull.state, | |
| 918 | number: pull.number, | |
| 919 | repo: `${repo.ownerName}/${repo.name}`, | |
| 920 | body: pull.body, | |
| 921 | }), | |
| 922 | visibility: repo.visibility, | |
| 914 | 923 | crumbs: crumbsFor(repo), |
| 915 | 924 | palette: repoPalette(ctx, repo), |
| 916 | 925 | body, |
src/web/pages/repo.ts +5 −2
| @@ -50,6 +50,7 @@ import { getAppByRepo } from "../../core/apps.ts"; | ||
| 50 | 50 | import { field, readForm } from "../form.ts"; |
| 51 | 51 | import { html, when, type Html } from "../html.ts"; |
| 52 | 52 | import { flashTo, fragment, page, redirect } from "../layout.ts"; |
| 53 | import { repoLine } from "../preview.ts"; | |
| 53 | 54 | import { renderMarkdown } from "../markdown.ts"; |
| 54 | 55 | import { browseBase, localScope, type RepoScope } from "../scope.ts"; |
| 55 | 56 | import { repoForkOf } from "../../core/origins.ts"; |
| @@ -939,7 +940,8 @@ async function codeHome(ctx: Ctx, repo: Repo): Promise<Response> { | ||
| 939 | 940 | return page({ |
| 940 | 941 | ctx, |
| 941 | 942 | title: repo.name, |
| 942 | description: repo.description, | |
| 943 | description: repoLine(scope.facts), | |
| 944 | visibility: repo.visibility, | |
| 943 | 945 | feed: { |
| 944 | 946 | href: `${repoBase(repo)}/activity.atom`, |
| 945 | 947 | title: `${repo.ownerName}/${repo.name} activity`, |
| @@ -986,7 +988,8 @@ async function codeHome(ctx: Ctx, repo: Repo): Promise<Response> { | ||
| 986 | 988 | return page({ |
| 987 | 989 | ctx, |
| 988 | 990 | title: repo.name, |
| 989 | description: repo.description, | |
| 991 | description: repoLine(scope.facts), | |
| 992 | visibility: repo.visibility, | |
| 990 | 993 | feed: { |
| 991 | 994 | href: `${repoBase(repo)}/activity.atom`, |
| 992 | 995 | title: `${repo.ownerName}/${repo.name} activity`, |
src/web/preview.ts added +43
| @@ -0,0 +1,43 @@ | ||
| 1 | /** | |
| 2 | * The sentence a link shows before it is opened. Written from the page's | |
| 3 | * live state, and private-safe by construction: a private page previews as | |
| 4 | * its name and the word "private" — nothing a reader could not already see | |
| 5 | * without signing in, because an unfurler never signs in. | |
| 6 | */ | |
| 7 | ||
| 8 | export type Visibility = "public" | "private"; | |
| 9 | ||
| 10 | export const PRIVATE_LINE = "Private"; | |
| 11 | ||
| 12 | const plural = (n: number, one: string, many: string) => | |
| 13 | `${n} ${n === 1 ? one : many}`; | |
| 14 | ||
| 15 | /** A repository: its own words, then what is open on it right now (when known). */ | |
| 16 | export function repoLine(facts: { | |
| 17 | description: string; | |
| 18 | openIssues: number | null; | |
| 19 | openPulls: number | null; | |
| 20 | }): string { | |
| 21 | const parts: string[] = []; | |
| 22 | if (facts.openIssues !== null) | |
| 23 | parts.push(plural(facts.openIssues, "open issue", "open issues")); | |
| 24 | if (facts.openPulls !== null) | |
| 25 | parts.push(plural(facts.openPulls, "open pull", "open pulls")); | |
| 26 | const live = parts.join(", "); | |
| 27 | const own = facts.description.trim().split(/\r?\n/)[0]?.trim() ?? ""; | |
| 28 | if (live === "") return own; | |
| 29 | return own === "" ? `${live}.` : `${own} — ${live}.`; | |
| 30 | } | |
| 31 | ||
| 32 | /** An issue or a pull: its state and number, then the first line of its body. */ | |
| 33 | export function threadLine(t: { | |
| 34 | kind: "issue" | "pull"; | |
| 35 | state: string; | |
| 36 | number: number; | |
| 37 | repo: string; | |
| 38 | body: string; | |
| 39 | }): string { | |
| 40 | const head = `${t.state === "open" ? "Open" : t.state[0]!.toUpperCase() + t.state.slice(1)} ${t.kind} #${t.number} on ${t.repo}`; | |
| 41 | const first = t.body.trim().split(/\r?\n/)[0]?.trim() ?? ""; | |
| 42 | return first === "" ? `${head}.` : `${head}: ${first}`; | |
| 43 | } |
src/web/studio/tell.ts +9
| @@ -23,6 +23,7 @@ import { crumbsFor, repoBase, repoHeader, repoPalette } from "../chrome.ts"; | ||
| 23 | 23 | import { html, when, type Html } from "../html.ts"; |
| 24 | 24 | import { page } from "../layout.ts"; |
| 25 | 25 | import { liveClientJs, liveMount } from "../live.ts"; |
| 26 | import { threadLine } from "../preview.ts"; | |
| 26 | 27 | import { |
| 27 | 28 | avatar, |
| 28 | 29 | card, |
| @@ -318,6 +319,14 @@ async function studioTell( | ||
| 318 | 319 | return page({ |
| 319 | 320 | ctx, |
| 320 | 321 | title: `${issue.title} · ${repo.name}`, |
| 322 | description: threadLine({ | |
| 323 | kind: "issue", | |
| 324 | state: issue.state, | |
| 325 | number: issue.number, | |
| 326 | repo: `${repo.ownerName}/${repo.name}`, | |
| 327 | body: issue.body, | |
| 328 | }), | |
| 329 | visibility: repo.visibility, | |
| 321 | 330 | crumbs: crumbsFor(repo), |
| 322 | 331 | palette: repoPalette(ctx, repo), |
| 323 | 332 | body, |
test/standards.test.ts +31
| @@ -13,6 +13,7 @@ import { createRepo } from "../src/actions/repos.ts"; | ||
| 13 | 13 | import { decodePng, pngHeader } from "../src/design/image/png.ts"; |
| 14 | 14 | import { markPng } from "../src/design/mark.ts"; |
| 15 | 15 | import { createUser, type User } from "../src/core/users.ts"; |
| 16 | import { createSession } from "../src/core/sessions.ts"; | |
| 16 | 17 | import { serve, type Servers } from "../src/http/server.ts"; |
| 17 | 18 | |
| 18 | 19 | describe("what the open web expects", () => { |
| @@ -158,6 +159,36 @@ describe("what the open web expects", () => { | ||
| 158 | 159 | expect(html).toContain("viewport-fit=cover"); |
| 159 | 160 | }); |
| 160 | 161 | |
| 162 | test("the description is written from the page's live state", async () => { | |
| 163 | const html = await fetch(`${base}/alice/open`).then((r) => r.text()); | |
| 164 | expect(html).toContain( | |
| 165 | 'property="og:description" content="0 open issues, 0 open pulls."', | |
| 166 | ); | |
| 167 | for (const tag of ["twitter:title", "twitter:description", "twitter:image"]) | |
| 168 | expect(html).toContain(`name="${tag}"`); | |
| 169 | }); | |
| 170 | ||
| 171 | test("an anonymous fetch of a private repository leaks nothing", async () => { | |
| 172 | const res = await fetch(`${base}/alice/closed`); | |
| 173 | expect(res.status).toBe(404); | |
| 174 | // The `alternate` link echoes the path the reader typed; that is theirs. | |
| 175 | const html = (await res.text()).replace(/href="[^"]*"/g, 'href=""'); | |
| 176 | expect(html).not.toContain("closed"); | |
| 177 | expect(html).not.toContain("og:url"); | |
| 178 | expect(html).not.toContain("open issues"); | |
| 179 | }); | |
| 180 | ||
| 181 | test("a private repository previews as its name and 'private', whoever fetches it", async () => { | |
| 182 | const { token } = createSession(app.db, alice.id); | |
| 183 | const html = await fetch(`${base}/alice/closed`, { | |
| 184 | headers: { cookie: `forge_session=${token}` }, | |
| 185 | }).then((r) => r.text()); | |
| 186 | expect(html).toContain('property="og:title" content="closed"'); | |
| 187 | expect(html).toContain('property="og:description" content="Private"'); | |
| 188 | expect(html).toContain('name="twitter:description" content="Private"'); | |
| 189 | expect(html).not.toContain("open issues, 0 open pulls"); | |
| 190 | }); | |
| 191 | ||
| 161 | 192 | test("the page's own policy lets a browser read what it links", async () => { |
| 162 | 193 | // default-src 'none' refuses a manifest like everything else; a link the |
| 163 | 194 | // policy blocks is a link that does nothing. Same for the icons. |
test/web-ui.test.ts +5 −1
| @@ -290,7 +290,11 @@ describe("web ui", () => { | ||
| 290 | 290 | expect(absent.status).toBe(404); |
| 291 | 291 | // Byte-identical once the per-response CSP nonce is normalised: a repo you |
| 292 | 292 | // may not see and one that does not exist give the visitor the same page. |
| 293 | const scrub = (t: string) => t.replaceAll(/nonce-?="?[\w-]+/g, "nonce"); | |
| 293 | // The `alternate` link echoes the path the reader typed; that is theirs. | |
| 294 | const scrub = (t: string) => | |
| 295 | t | |
| 296 | .replaceAll(/nonce-?="?[\w-]+/g, "nonce") | |
| 297 | .replaceAll(/\/api\/v1\/here\?path=[^"]*/g, "/api/v1/here"); | |
| 294 | 298 | expect(scrub(await hidden.text())).toBe(scrub(await absent.text())); |
| 295 | 299 | }); |
| 296 | 300 |
new above
offline