-
Notifications
You must be signed in to change notification settings - Fork 0
Section deeplinks + comment permalinks in the doc viewer #9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| import { describe, it, expect } from "vitest"; | ||
| import { fragmentFor, parseHash } from "@/lib/docs/deeplink"; | ||
|
|
||
| describe("deeplink codec", () => { | ||
| it("routes comment / section / empty hashes", () => { | ||
| expect(parseHash("#comment-42")).toEqual({ kind: "comment", id: 42 }); | ||
| expect(parseHash("comment-42")).toEqual({ kind: "comment", id: 42 }); // leading # optional | ||
| expect(parseHash("#setup")).toEqual({ kind: "section", id: "setup" }); | ||
| expect(parseHash("#")).toEqual({ kind: "none" }); | ||
| expect(parseHash("")).toEqual({ kind: "none" }); | ||
| }); | ||
|
|
||
| it("builds comment fragments without encoding and section fragments with it", () => { | ||
| expect(fragmentFor({ kind: "comment", id: 7 })).toBe("comment-7"); | ||
| expect(fragmentFor({ kind: "section", id: "setup" })).toBe("setup"); | ||
| expect(fragmentFor({ kind: "section", id: "my heading" })).toBe("my%20heading"); | ||
| }); | ||
|
|
||
| it("round-trips section ids with spaces, unicode, and reserved characters", () => { | ||
| for (const id of ["setup", "my heading", "café", "日本語", "a/b?c#d", "100%", "section-comment-3"]) { | ||
| const t = parseHash("#" + fragmentFor({ kind: "section", id })); | ||
| expect(t).toEqual({ kind: "section", id }); | ||
| } | ||
| }); | ||
|
|
||
| it("does not throw on malformed percent-encoding — treats it as a literal section id", () => { | ||
| expect(parseHash("#foo%")).toEqual({ kind: "section", id: "foo%" }); | ||
| expect(parseHash("#%E0%A4%A")).toEqual({ kind: "section", id: "%E0%A4%A" }); | ||
| }); | ||
|
|
||
| it("a comment-<digits> hash is a comment, but comment-word is a section", () => { | ||
| expect(parseHash("#comment-1")).toEqual({ kind: "comment", id: 1 }); | ||
| expect(parseHash("#comment-intro")).toEqual({ kind: "section", id: "comment-intro" }); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| // Deeplink fragment contract — the ONE place the copy side and the read side agree | ||
| // on how a section/comment id maps to a URL #fragment. copyLink builds the fragment | ||
| // with fragmentFor(); the hash router reads it with parseHash(). Keeping both on one | ||
| // codec is what makes permalinks round-trip: encode-on-write must mirror | ||
| // decode-on-read, and the routing (comment vs section) must match how ids are | ||
| // minted (see lib/docs/sections.ts, which never mints a comment-<n> section id, so | ||
| // the comment branch here can never swallow a section link). | ||
|
|
||
| export type HashTarget = | ||
| | { kind: "comment"; id: number } | ||
| | { kind: "section"; id: string } | ||
| | { kind: "none" }; | ||
|
|
||
| // The #fragment for a copyable permalink. Comment permalinks are comment-<id> | ||
| // (ASCII, no encoding needed); a section id is author- or slug-derived and may hold | ||
| // Unicode, spaces, or reserved characters, so it's percent-encoded — parseHash | ||
| // decodes it back. | ||
| export function fragmentFor(target: { kind: "comment"; id: number } | { kind: "section"; id: string }): string { | ||
| return target.kind === "comment" ? `comment-${target.id}` : encodeURIComponent(target.id); | ||
| } | ||
|
|
||
| // Parse a URL hash (with or without the leading '#') into a navigation target. | ||
| // Decodes percent-encoding; malformed encoding (a lone '%', a truncated escape in a | ||
| // pasted link) is treated as a literal section id rather than throwing. An empty | ||
| // fragment is "none" (no selection). | ||
| export function parseHash(hash: string): HashTarget { | ||
| const raw = hash.startsWith("#") ? hash.slice(1) : hash; | ||
| let h: string; | ||
| try { | ||
| h = decodeURIComponent(raw); | ||
| } catch { | ||
| h = raw; | ||
| } | ||
| if (!h) return { kind: "none" }; | ||
| const m = /^comment-(\d+)$/.exec(h); | ||
| if (m) return { kind: "comment", id: Number(m[1]) }; | ||
| return { kind: "section", id: h }; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| import { describe, it, expect } from "vitest"; | ||
| import { extractSections } from "@/lib/docs/sections"; | ||
|
|
||
| describe("extractSections", () => { | ||
| it("slugs heading text and records level", () => { | ||
| expect(extractSections("<h1>Hello World</h1>")).toEqual([ | ||
| { id: "hello-world", level: 1, text: "Hello World" }, | ||
| ]); | ||
| const levels = extractSections( | ||
| "<h1>a</h1><h2>b</h2><h3>c</h3><h4>d</h4><h5>e</h5><h6>f</h6>" | ||
| ).map((s) => s.level); | ||
| expect(levels).toEqual([1, 2, 3, 4, 5, 6]); | ||
| }); | ||
|
|
||
| it("uses the text content of nested inline markup", () => { | ||
| const [s] = extractSections("<h2>v0 API <code>shape</code></h2>"); | ||
| expect(s.id).toBe("v0-api-shape"); | ||
| expect(s.text).toBe("v0 API shape"); | ||
| }); | ||
|
|
||
| it("decodes entities and collapses punctuation to a single hyphen", () => { | ||
| expect(extractSections("<h2>Tom & Jerry</h2>")[0].id).toBe("tom-jerry"); | ||
| expect(extractSections("<h2> Multiple spaces </h2>")[0]).toEqual({ | ||
| id: "multiple-spaces", | ||
| level: 2, | ||
| text: "Multiple spaces", | ||
| }); | ||
| }); | ||
|
|
||
| it("de-dupes repeated slugs in document order", () => { | ||
| expect(extractSections("<h2>Setup</h2><h3>Setup</h3><h2>Setup</h2>").map((s) => s.id)).toEqual([ | ||
| "setup", | ||
| "setup-1", | ||
| "setup-2", | ||
| ]); | ||
| }); | ||
|
|
||
| it("falls back to section-N for empty slugs (emoji / punctuation only)", () => { | ||
| expect(extractSections("<h1>Intro</h1><h2>🎉</h2><h3>!!!</h3>").map((s) => s.id)).toEqual([ | ||
| "intro", | ||
| "section-2", | ||
| "section-3", | ||
| ]); | ||
| }); | ||
|
|
||
| it("prefers an author-provided id and ignores other *id attributes", () => { | ||
| expect(extractSections('<h2 id="custom-anchor">Whatever</h2>')[0].id).toBe("custom-anchor"); | ||
| expect(extractSections('<h2 data-id="x" id="real">Title</h2>')[0].id).toBe("real"); | ||
| expect(extractSections("<h2>No id here</h2>")[0].id).toBe("no-id-here"); | ||
| }); | ||
|
|
||
| it("never mints an id in the reserved comment-<n> namespace", () => { | ||
| expect(extractSections("<h2>Comment 12</h2>")[0].id).toBe("section-comment-12"); | ||
| // Author ids get the same guard, so a heading id can't hijack a #comment-<n> | ||
| // permalink; a non-colliding author id is still used verbatim. | ||
| expect(extractSections('<h2 id="comment-5">Title</h2>')[0].id).toBe("section-comment-5"); | ||
| expect(extractSections('<h2 id="intro">Title</h2>')[0].id).toBe("intro"); | ||
| }); | ||
|
|
||
| it("keeps non-Latin headings instead of emptying them", () => { | ||
| const [s] = extractSections("<h2>日本語</h2>"); | ||
| expect(s.id).toBe("日本語"); | ||
| expect(s.level).toBe(2); | ||
| }); | ||
|
|
||
| it("ignores headings inside comments, script, and style", () => { | ||
| const html = | ||
| "<!-- <h1>hidden</h1> --><style><h1>styled</h1></style>" + | ||
| "<script><h1>scripted</h1></script><h1>Real</h1>"; | ||
| expect(extractSections(html).map((s) => s.id)).toEqual(["real"]); | ||
| }); | ||
|
|
||
| it("ignores headings in template/noscript so ids don't shift vs the rendered DOM", () => { | ||
| // Neither reaches the overlay's document.querySelectorAll (template is inert; | ||
| // noscript is text while scripting is on), so counting them would misalign the | ||
| // id assigned to every later heading. Both real headings keep their clean ids. | ||
| const html = | ||
| "<h1>First</h1><template><h2>tpl only</h2></template>" + | ||
| "<noscript><h2>ns only</h2></noscript><h2>Second</h2>"; | ||
| expect(extractSections(html).map((s) => s.id)).toEqual(["first", "second"]); | ||
| }); | ||
|
|
||
| it("returns an empty list when there are no headings", () => { | ||
| expect(extractSections("<p>just a paragraph</p>")).toEqual([]); | ||
| }); | ||
| }); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.