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

Closed opened this in answer to
Closed without merging

scout/forge:scout/453-meta-tags-on-every-page-og-twitter-tags → main 5 commits fork

  • Closed without merging. Nothing was applied to main.

Review

Sign in to review this change.
7 files changed +115 −3
src/web/layout.ts +13
@@ -21,6 +21,7 @@ import {
2121 THEMES,
2222 type ThemeName,
2323 } from "../design/themes.ts";
24 import { PRIVATE_LINE, type Visibility } from "./preview.ts";
2425 import { stylesheetAsset } from "./asset.ts";
2526 import { linkHeader, pageLinks } from "./standards.ts";
2627 import { shellJs } from "./client.ts";
@@ -52,6 +53,11 @@ export interface PageOptions {
5253 * an issue's first line. Absent, the forge's own line stands in.
5354 */
5455 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;
5561 /**
5662 * The shell without its column width: a page whose content earns a wide
5763 * monitor (the code browser, a tree beside a file). The topbar and footer
@@ -497,6 +503,7 @@ function footer(): Html {
497503 const SITE_LINE = "Your code, your machine.";
498504
499505 function pageDescription(options: PageOptions): string {
506 if (options.visibility === "private") return PRIVATE_LINE;
500507 const own = options.description?.trim().split(/\r?\n/)[0]?.trim() ?? "";
501508 const line = own === "" ? SITE_LINE : own;
502509 return line.length <= 200 ? line : `${line.slice(0, 197).trimEnd()}…`;
@@ -562,6 +569,12 @@ export function page(options: PageOptions, status = 200): Response {
562569 content="${ctx.url.origin}/icons/forge-512.png"
563570 />
564571 <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 />
565578 <link rel="manifest" href="/manifest.webmanifest" />
566579 ${links.map(
567580 (l) =>
src/web/pages/pull.ts +9
@@ -65,6 +65,7 @@ import { html, raw, when, type Html } from "../html.ts";
6565 import { renderMarkdown } from "../markdown.ts";
6666 import type { IconName as Glyph } from "../icons.ts";
6767 import { flashTo, page, redirect } from "../layout.ts";
68 import { threadLine } from "../preview.ts";
6869 import { liveClientJs, liveMount } from "../live.ts";
6970 import {
7071 avatar,
@@ -911,6 +912,14 @@ export async function pullPage(ctx: Ctx): Promise<Response> {
911912 return page({
912913 ctx,
913914 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,
914923 crumbs: crumbsFor(repo),
915924 palette: repoPalette(ctx, repo),
916925 body,
src/web/pages/repo.ts +5 −2
@@ -50,6 +50,7 @@ import { getAppByRepo } from "../../core/apps.ts";
5050 import { field, readForm } from "../form.ts";
5151 import { html, when, type Html } from "../html.ts";
5252 import { flashTo, fragment, page, redirect } from "../layout.ts";
53 import { repoLine } from "../preview.ts";
5354 import { renderMarkdown } from "../markdown.ts";
5455 import { browseBase, localScope, type RepoScope } from "../scope.ts";
5556 import { repoForkOf } from "../../core/origins.ts";
@@ -939,7 +940,8 @@ async function codeHome(ctx: Ctx, repo: Repo): Promise<Response> {
939940 return page({
940941 ctx,
941942 title: repo.name,
942 description: repo.description,
943 description: repoLine(scope.facts),
944 visibility: repo.visibility,
943945 feed: {
944946 href: `${repoBase(repo)}/activity.atom`,
945947 title: `${repo.ownerName}/${repo.name} activity`,
@@ -986,7 +988,8 @@ async function codeHome(ctx: Ctx, repo: Repo): Promise<Response> {
986988 return page({
987989 ctx,
988990 title: repo.name,
989 description: repo.description,
991 description: repoLine(scope.facts),
992 visibility: repo.visibility,
990993 feed: {
991994 href: `${repoBase(repo)}/activity.atom`,
992995 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";
2323 import { html, when, type Html } from "../html.ts";
2424 import { page } from "../layout.ts";
2525 import { liveClientJs, liveMount } from "../live.ts";
26 import { threadLine } from "../preview.ts";
2627 import {
2728 avatar,
2829 card,
@@ -318,6 +319,14 @@ async function studioTell(
318319 return page({
319320 ctx,
320321 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,
321330 crumbs: crumbsFor(repo),
322331 palette: repoPalette(ctx, repo),
323332 body,
test/standards.test.ts +31
@@ -13,6 +13,7 @@ import { createRepo } from "../src/actions/repos.ts";
1313 import { decodePng, pngHeader } from "../src/design/image/png.ts";
1414 import { markPng } from "../src/design/mark.ts";
1515 import { createUser, type User } from "../src/core/users.ts";
16 import { createSession } from "../src/core/sessions.ts";
1617 import { serve, type Servers } from "../src/http/server.ts";
1718
1819 describe("what the open web expects", () => {
@@ -158,6 +159,36 @@ describe("what the open web expects", () => {
158159 expect(html).toContain("viewport-fit=cover");
159160 });
160161
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
161192 test("the page's own policy lets a browser read what it links", async () => {
162193 // default-src 'none' refuses a manifest like everything else; a link the
163194 // 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", () => {
290290 expect(absent.status).toBe(404);
291291 // Byte-identical once the per-response CSP nonce is normalised: a repo you
292292 // 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");
294298 expect(scrub(await hidden.text())).toBe(scrub(await absent.text()));
295299 });
296300

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs