From 0468ba3ce6bb4b3732499175245ff4c5bfb71b11 Mon Sep 17 00:00:00 2001 From: Felix Boehm <188768+fb55@users.noreply.github.com> Date: Sat, 21 Nov 2026 09:56:22 +0000 Subject: [PATCH 1/5] Make the HTML theme work on phones, and give it some life The theme had no navigation on small screens, signatures were its smallest text, and headings changed style from page to page. Reviewed over seven rounds by three models against the best documentation sites. Also fixes type parameters' constraints losing their closing bracket, stray spaces around inline code in summaries, and source links into dependencies' node_modules. Co-Authored-By: Claude Opus 5.5 --- assets/main.js | 180 +++++- assets/style.css | 1133 ++++++++++++++++++++++++++++++++---- src/convert.ts | 9 +- src/render/html.ts | 319 +++++++--- src/render/types.ts | 42 +- src/source.ts | 5 +- test/fixtures/rendering.ts | 10 + test/render.test.ts | 33 +- test/source.test.ts | 9 + 9 files changed, 1521 insertions(+), 219 deletions(-) diff --git a/assets/main.js b/assets/main.js index b6034cd..74eec8f 100644 --- a/assets/main.js +++ b/assets/main.js @@ -74,12 +74,15 @@ async function search(query) { const parent = document.createElement("span"); parent.className = "result-parent"; parent.textContent = `${entry.parent}.`; + // Long qualified names break after their parent's name. + parent.append(document.createElement("wbr")); name.append(parent); } name.append(entry.name); const kind = document.createElement("span"); kind.className = "result-kind"; kind.textContent = entry.kind; + kind.dataset.kind = entry.kind; link.append(name, kind); item.append(link); return item; @@ -94,8 +97,33 @@ input.addEventListener("keydown", (event) => { // Enter also ends input methods' compositions. if (event.key === "Enter" && !event.isComposing) { results.querySelector("a")?.click(); + } else if (event.key === "ArrowDown") { + event.preventDefault(); + results.querySelector("a")?.focus(); } }); +// Arrow keys move between results, and back to the input from the first. +results.addEventListener("keydown", (/** @type {KeyboardEvent} */ event) => { + // Typing goes on in the input. + if ( + (event.key.length === 1 || event.key === "Backspace") && + !event.ctrlKey && + !event.metaKey && + !event.altKey + ) { + input.focus(); + return; + } + if (event.key !== "ArrowDown" && event.key !== "ArrowUp") return; + const links = [...results.querySelectorAll("a")]; + const position = links.indexOf( + /** @type {HTMLAnchorElement} */ (event.target), + ); + if (position === -1) return; + event.preventDefault(); + const next = position + (event.key === "ArrowDown" ? 1 : -1); + (next < 0 ? input : links[Math.min(next, links.length - 1)]).focus(); +}); dialog.addEventListener("close", () => { input.value = ""; results.replaceChildren(); @@ -109,19 +137,165 @@ document.addEventListener("keydown", (event) => { } }); +// Apple's keyboards show the shortcut with Command. +if (/Mac|iPhone|iPad/u.test(navigator.platform)) { + const key = document.querySelector(".search-button kbd"); + if (key) key.textContent = "⌘K"; +} + +/** + * @param {HTMLElement} element An element in a scrolling list. + * @param {HTMLElement} list The list. + * @returns The element's offset from the top of the list's content. + */ +function offsetIn(element, list) { + return element.offsetParent === list + ? element.offsetTop + : element.offsetTop - list.offsetTop; +} + +/** + * Scrolls a list to an element, if it is not wholly in view. + * + * @param {HTMLElement} element An element in the list. + * @param {HTMLElement} list The list. + */ +function reveal(element, list) { + const top = offsetIn(element, list); + if ( + top < list.scrollTop || + top + element.offsetHeight > list.scrollTop + list.clientHeight + ) { + list.scrollTop = top - list.clientHeight / 2; + } +} + /** - * Mirrors the section in view, which CSS highlights, for assistive technology. + * @param {string} query A media query. + * @param {(matches: boolean) => void} listener Called with whether it + * matches, now and when that changes. + */ +function whenMatches(query, listener) { + const list = matchMedia(query); + listener(list.matches); + list.addEventListener("change", () => { + listener(list.matches); + }); +} + +// The navigation is a sidebar, or on narrow screens a drawer. +const site = document.querySelector("#Site"); +const narrow = matchMedia("(width < 60rem)"); +if (site instanceof HTMLElement) { + /** Scrolls the navigation, not the page, to the current page's link. */ + const showCurrent = () => { + const current = site.querySelector("[aria-current=page]"); + if (current instanceof HTMLElement) reveal(current, site); + }; + // The drawer is modal: the page behind it is inert while it is open. + const page = document.querySelectorAll( + "body > :not(.layout, dialog), .layout > :not(#Site)", + ); + // Before closing, so focus can return to the button that opened it. + site.addEventListener("beforetoggle", (event) => { + const isOpening = event.newState === "open"; + for (const element of page) { + if (element instanceof HTMLElement) element.inert = isOpening; + } + }); + site.addEventListener("toggle", (event) => { + if (event.newState === "open") { + showCurrent(); + site.querySelector("button")?.focus(); + } + }); + whenMatches(narrow.media, (matches) => { + if (!matches && site.matches(":popover-open")) site.hidePopover(); + if (!matches) showCurrent(); + }); +} + +// The table of contents is a column on wide screens, and above the page, +// collapsed, on others. +const contents = document.querySelector(".toc details"); +if (contents instanceof HTMLDetailsElement) { + const summary = contents.querySelector("summary"); + const wide = matchMedia("(width >= 80rem)"); + whenMatches(wide.media, (matches) => { + contents.open = matches; + // As a column, its summary is a heading, not a control. + if (summary) summary.tabIndex = matches ? -1 : 0; + }); + summary?.addEventListener("click", (event) => { + if (wide.matches) event.preventDefault(); + }); + // Above the page, it closes once a section is chosen, or on leaving it. + contents.addEventListener("click", (event) => { + if ( + !wide.matches && + event.target instanceof Element && + event.target.closest("a") + ) { + contents.open = false; + } + }); + document.addEventListener("click", (event) => { + if ( + !wide.matches && + contents.open && + event.target instanceof Node && + !contents.contains(event.target) + ) { + contents.open = false; + } + }); + // Keyboards leaving it close it too, so it covers nothing they reach. + // Clicks within it, on headings or its scrollbar, focus nothing. + contents.addEventListener("focusout", (event) => { + if ( + !wide.matches && + contents.open && + event.relatedTarget instanceof Node && + !contents.contains(event.relatedTarget) + ) { + contents.open = false; + } + }); + contents.addEventListener("keydown", (event) => { + if (!wide.matches && event.key === "Escape") { + contents.open = false; + summary?.focus(); + } + }); +} + +/** + * Mirrors the section in view, which CSS highlights, for assistive technology, + * and keeps it in view in the contents' list. */ function syncCurrent() { const current = document.querySelector(".toc a:target-current"); for (const link of document.querySelectorAll(".toc a")) { link.setAttribute("aria-current", String(link === current)); } + const list = current?.closest(".toc-list"); + if ( + current instanceof HTMLElement && + list instanceof HTMLElement && + list.clientHeight > 0 + ) { + reveal(current, list); + } } if (CSS.supports("scroll-target-group: auto")) { - syncCurrent(); - document.addEventListener("scrollend", syncCurrent); + // The section in view updates after scrolling ends. + const sync = () => { + setTimeout(syncCurrent, 0); + }; + sync(); + document.addEventListener("scrollend", sync); + addEventListener("hashchange", sync); } export {}; diff --git a/assets/style.css b/assets/style.css index 04cc43e..2063436 100644 --- a/assets/style.css +++ b/assets/style.css @@ -4,16 +4,20 @@ --muted: light-dark(#59636e, #9198a1); --background: light-dark(#fff, #0d1117); --surface: light-dark(#f6f8fa, #151b23); + /* Controls' edges; dividers and code boxes use the subtler one. */ --border: light-dark(#d1d9e0, #3d444d); + --border-subtle: light-dark(#e1e6eb, #262c36); --accent: light-dark(#0969da, #4493f8); --keyword: light-dark(#cf222e, #ff7b72); + --type: light-dark(#0550ae, #79c0ff); --name: light-dark(#8250df, #d2a8ff); --literal: light-dark(#0a3069, #a5d6ff); --parameter: light-dark(#953800, #ffa657); - --deprecated: light-dark(#9a6700, #d29922); + --green: light-dark(#1a7f37, #3fb950); --font: system-ui, sans-serif; --mono: ui-monospace, "SF Mono", Menlo, Consolas, monospace; --header-height: 3.25rem; + --max-width: 86rem; accent-color: var(--accent); } @@ -24,6 +28,9 @@ html { scroll-padding-top: calc(var(--header-height) + 1rem); + /* Phones in landscape keep the text's size. */ + -webkit-text-size-adjust: 100%; + text-size-adjust: 100%; } body { @@ -39,30 +46,52 @@ a { text-underline-offset: 0.15em; } +:focus-visible { + outline: 2px solid var(--accent); + outline-offset: 2px; +} + code, -kbd, -pre { +kbd { font-family: var(--mono); font-size: 0.875em; } :not(pre) > code { - padding: 0.1em 0.35em; + padding: 0.1em 0.25em; border-radius: 0.3em; background: var(--surface); } -/* Long lines wrap: Safari cannot scroll code by keyboard. */ pre { - overflow-wrap: anywhere; - white-space: pre-wrap; + margin: 0.75rem 0; padding: 0.85rem 1rem; - border: 1px solid var(--border); + border: 1px solid var(--border-subtle); border-radius: 0.5rem; background: var(--surface); + font-family: var(--mono); + font-size: 0.875rem; line-height: 1.5; } +pre code { + font-size: inherit; +} + +/* Examples keep their lines and indentation, and scroll. */ +pre:not(.signature) { + overflow-x: auto; + white-space: pre; +} + +/* Signatures wrap, continuing lines indented under their start. */ +pre.signature { + overflow-wrap: break-word; + white-space: pre-wrap; + padding-inline-start: calc(1rem + 2ch); + text-indent: -2ch; +} + /* Highlighting, with both themes' colors as custom properties. */ .shiki, .shiki span { @@ -73,51 +102,92 @@ pre { background: var(--surface); } -.signature .k { +code .k { color: var(--keyword); } -.signature .n { +code .t { + color: var(--type); +} + +code .n { color: var(--name); } -.signature .l { +code .l { color: var(--literal); } -.signature .p { +code .p { color: var(--parameter); } +main h2 > a { + color: inherit; +} + +/* Types link quietly; their underline darkens on hover. */ +code a, +h2 > a, +dl.index dt a { + text-decoration-color: color-mix(in srgb, currentColor 40%, transparent); +} + +code a:hover, +h2 > a:hover, +dl.index dt a:hover { + text-decoration-color: currentColor; +} + +h1, +h2, +h3, +h4 { + line-height: 1.25; + text-wrap: balance; +} + +p { + text-wrap: pretty; +} + .skip { position: absolute; left: 0.5rem; - top: -3rem; + top: -4rem; + z-index: 10; + padding: 0.4rem 0.75rem; + border: 2px solid var(--accent); + border-radius: 0.4rem; + background: var(--background); } .skip:focus { top: 0.5rem; - z-index: 2; } +/* The header's content aligns with the layout's. */ .top { position: sticky; top: 0; - z-index: 1; + z-index: 2; display: flex; align-items: center; - gap: 1rem; + gap: 0.5rem 0.75rem; height: var(--header-height); - padding: 0 1rem; - border-bottom: 1px solid var(--border); - background: color-mix(in srgb, var(--background) 85%, transparent); - backdrop-filter: blur(8px); + padding-inline: max(1rem, (100% - var(--max-width)) / 2 + 1rem); + border-bottom: 1px solid var(--border-subtle); + background: var(--background); } .project { + min-width: 0; + overflow: hidden; color: var(--text); font-weight: 600; text-decoration: none; + text-overflow: ellipsis; + white-space: nowrap; } .version { @@ -126,58 +196,135 @@ pre { font-size: 0.875em; } -.search-button { - margin-left: auto; - padding: 0.35rem 0.75rem; +.site-title { + min-width: 0; + font-weight: 600; + overflow-wrap: break-word; +} + +/* Zoomed text keeps the project's name readable; the drawer has the version. */ +@media (width < 30rem) { + .top .version { + display: none; + } +} + +.top button, +.site .close-button, +.search-bar button { + flex-shrink: 0; + padding: 0.3rem 0.7rem; border: 1px solid var(--border); border-radius: 0.5rem; background: var(--surface); - color: var(--muted); + color: var(--text); font: inherit; + font-size: 0.875rem; cursor: pointer; } +.top button:hover, +.site .close-button:hover, +.search-bar button:hover { + border-color: var(--muted); +} + +.search-button { + margin-left: auto; +} + +/* Where there is room, search looks like the field it opens. */ +@media (width >= 40rem) { + .search-button { + display: inline-flex; + align-items: center; + justify-content: space-between; + min-width: 12rem; + color: var(--muted); + } +} + +.search-button kbd { + margin-left: 0.25rem; + color: var(--muted); +} + kbd { padding: 0 0.3em; border: 1px solid var(--border); border-radius: 0.25em; } +/* Touch keyboards have no shortcuts, and phones no room for them. */ +@media (width < 40rem), (pointer: coarse) { + .search-button kbd { + display: none; + } +} + +/* + * Narrow: the page, with its contents collapsed above it, and the + * navigation in a drawer. Medium: the navigation beside them. Wide: the + * contents in a column of their own. + */ .layout { display: grid; - grid-template-areas: "main" "toc" "site"; - gap: 2rem; - max-width: 90rem; + grid-template-areas: "main"; + column-gap: 2.5rem; + max-width: var(--max-width); margin: 0 auto; - padding: 1.5rem 1rem 4rem; + /* At the page's end, sticky columns keep their place under the header. */ + padding: 1.5rem 1rem 0; } main { grid-area: main; min-width: 0; + padding-bottom: 4rem; + container-type: inline-size; /* Long names break rather than widen the page. */ - overflow-wrap: anywhere; + overflow-wrap: break-word; } main:focus { outline: none; } -.site { - grid-area: site; +/* Reading lines stay short however wide the page is. */ +main :is(p, li, dd, blockquote) { + max-inline-size: 72ch; } +/* Above the page, the contents stay in reach under the header. */ .toc { - grid-area: toc; + grid-area: main; + align-self: start; + position: sticky; + top: var(--header-height); + z-index: 1; + margin-inline: -1rem; + border-bottom: 1px solid var(--border-subtle); + background: var(--background); +} + +.layout:has(> .toc) { + padding-top: 0; +} + +.toc + main { + margin-top: 4.25rem; +} + +html:has(.toc) { + scroll-padding-top: calc(var(--header-height) + 3.5rem); } .site, .toc { - font-size: 0.9rem; + font-size: 0.875rem; } .site h2, -.toc h2, .toc h3 { margin: 1.25rem 0 0.35rem; color: var(--muted); @@ -190,24 +337,26 @@ main:focus { .toc ul { margin: 0; padding: 0; + border-inline-start: 1px solid var(--border-subtle); list-style: none; } +/* Pages of categories, under their class. */ +.site ul ul { + padding-inline-start: 0.75rem; + border: 0; +} + .site a, .toc a { display: block; - overflow: hidden; - padding: 0.1rem 0.5rem; - border-left: 2px solid transparent; + margin-inline-start: -1px; + padding: 0.2rem 0.75rem; + border-inline-start: 2px solid transparent; color: var(--text); + line-height: 1.4; + overflow-wrap: break-word; text-decoration: none; - text-overflow: ellipsis; - white-space: nowrap; -} - -/* Pages of categories, under their class. */ -.site ul ul { - padding-inline-start: 0.75rem; } .site a:hover, @@ -215,14 +364,27 @@ main:focus { color: var(--accent); } -/* A class is current on the pages of its categories, too. */ -.site a[aria-current], -.toc a[aria-current="true"] { - border-left-color: var(--accent); +.site a:focus-visible, +.toc a:focus-visible, +.results a:focus-visible { + outline-offset: -2px; +} + +.site a[aria-current="page"] { + border-inline-start-color: var(--accent); color: var(--accent); font-weight: 600; } +/* A class, on the pages of its categories. */ +.site a[aria-current="true"] { + font-weight: 600; +} + +.site a.deprecated { + text-decoration: line-through; +} + /* The section in view, where browsers track it. */ @supports (scroll-target-group: auto) { .toc { @@ -230,62 +392,276 @@ main:focus { } .toc a:target-current { - border-left-color: var(--accent); + border-inline-start-color: var(--accent); color: var(--accent); font-weight: 600; } } -@media (width >= 64rem) { +.toc summary { + padding: 0.6rem 1rem; + font-weight: 600; + cursor: pointer; +} + +/* Open, it lies over the page. */ +.toc:has(details[open]) { + box-shadow: 0 0.75rem 1.5rem light-dark(rgb(0 0 0 / 12%), rgb(0 0 0 / 45%)); +} + +.toc-list { + max-height: calc(100dvh - var(--header-height) - 4rem); + overflow-y: auto; + overscroll-behavior: contain; + padding: 0 1rem 0.75rem; +} + +.toc-list h3:first-child { + margin-top: 0.25rem; +} + +/* The navigation, in a drawer from the start edge. */ +.menu-button { + margin-inline-start: -0.25rem; +} + +.site { + inset: 0 auto 0 0; + width: min(20rem, 100% - 3rem); + height: 100dvh; + max-height: none; + margin: 0; + padding: 0.75rem 1.25rem 2rem; + overflow-y: auto; + overscroll-behavior: contain; + border: 0; + border-inline-end: 1px solid var(--border); + background: var(--background); + color: var(--text); +} + +.site::backdrop { + background: rgb(0 0 0 / 40%); +} + +/* Close stays in reach as the drawer scrolls, on a bar of its own. */ +.site { + padding-top: 0; +} + +.site-bar { + position: sticky; + top: 0; + z-index: 1; + display: flex; + align-items: center; + justify-content: space-between; + gap: 0.75rem; + margin: 0 -1.25rem 0.5rem; + padding: 0.75rem 1.25rem; + border-bottom: 1px solid var(--border-subtle); + background: var(--background); +} + +/* Touch targets. */ +@media (width < 60rem), (pointer: coarse) { + .site a, + .toc a { + padding-block: 0.45rem; + } + + .top button, + .site .close-button, + .search-bar button { + min-height: 2.5rem; + } + + .results a { + padding-block: 0.55rem; + } +} + +/* It slides in; closing, or a resize past the breakpoint, is instant. */ +@media (width < 60rem) and (prefers-reduced-motion: no-preference) { + .site:popover-open { + transition: translate 0.2s ease-out; + } + + @starting-style { + .site:popover-open { + translate: -100% 0; + } + } +} + +@media (width >= 60rem) { .layout { - grid-template-areas: "site main toc"; - grid-template-columns: 15rem minmax(0, 1fr) 13rem; + grid-template-areas: "site main"; + grid-template-columns: 14rem minmax(0, 1fr); } - .site, .toc { + margin-inline: 0; + } + + .toc summary { + padding-inline: 0; + } + + /* Open, the contents are a panel beside the page's text. */ + .toc-list { + position: absolute; + top: 100%; + left: -0.75rem; + width: min(24rem, 100% + 1.5rem); + padding: 0.5rem 0.75rem 0.75rem; + border: 1px solid var(--border); + border-top: 0; + border-radius: 0 0 0.5rem 0.5rem; + background: var(--background); + box-shadow: 0 0.75rem 1.5rem light-dark(rgb(0 0 0 / 12%), rgb(0 0 0 / 45%)); + } + + .toc:has(details[open]) { + box-shadow: none; + } + + .menu-button, + .site-bar { + display: none; + } + + /* The drawer, as a sidebar: undoing popovers' styles. */ + .site { + grid-area: site; + display: block; position: sticky; + inset: auto; top: calc(var(--header-height) + 1.5rem); align-self: start; + width: auto; + height: auto; max-height: calc(100dvh - var(--header-height) - 3rem); - overflow-y: auto; - overscroll-behavior: contain; + padding: 0; + border: 0; + background: none; + color: inherit; + scrollbar-width: thin; } +} - .site h2:first-child, - .toc h2:first-child { - margin-top: 0; +/* Short screens keep the header only; the contents, in a row of their own + above the page, scroll away. */ +@media (height < 30rem) and (width < 80rem) { + .layout { + grid-template-areas: "toc" "main"; + } + + .toc { + grid-area: toc; + position: static; + } + + .toc + main { + margin-top: 1.5rem; + } + + .toc:has(details[open]) { + box-shadow: none; + } + + .toc-list { + position: static; + width: auto; + max-height: none; + border: 0; + box-shadow: none; + } + + html:has(.toc) { + scroll-padding-top: calc(var(--header-height) + 1rem); } } -@media (40rem <= width < 64rem) { +@media (height < 30rem) and (60rem <= width < 80rem) { .layout { - grid-template-areas: "site main"; - grid-template-columns: 13rem minmax(0, 1fr); + grid-template-areas: "site toc" "site main"; + grid-template-rows: auto 1fr; } - .toc { - display: none; + .toc-list { + padding-inline: 0; } } -h1, -h2, -h3, -h4 { - line-height: 1.25; +@media (width >= 80rem) { + .layout, + .layout:has(> .toc) { + grid-template-areas: "site main toc"; + grid-template-columns: 15rem minmax(0, 1fr) 13rem; + padding-top: 1.5rem; + } + + html:has(.toc) { + scroll-padding-top: calc(var(--header-height) + 1rem); + } + + .toc, + .toc:has(details[open]) { + grid-area: toc; + top: calc(var(--header-height) + 1.5rem); + border: 0; + background: none; + box-shadow: none; + } + + .toc + main { + margin-top: 0; + } + + /* The contents, as a column: always open, titled by their summary. */ + .toc details::details-content { + content-visibility: visible; + } + + .toc summary { + margin-bottom: 0.5rem; + padding: 0; + list-style: none; + font-size: 0.8125rem; + cursor: default; + } + + .toc summary::-webkit-details-marker { + display: none; + } + + .toc-list h3:first-child { + margin-top: 0; + } + + .toc-list { + position: static; + width: auto; + max-height: calc(100dvh - var(--header-height) - 5rem); + padding: 0; + border: 0; + border-radius: 0; + box-shadow: none; + scrollbar-width: thin; + } } h1 { margin: 0; - font-size: 2rem; - overflow-wrap: anywhere; + font-size: clamp(1.5rem, 1rem + 2.5vw, 2rem); } main h2 { - margin-top: 2.5rem; + margin: 2.5rem 0 0.5rem; padding-bottom: 0.3rem; - border-bottom: 1px solid var(--border); + border-bottom: 1px solid var(--border-subtle); + font-size: 1.5rem; } .page-header { @@ -309,36 +685,67 @@ main h2 { border: 1px solid var(--border); border-radius: 1em; color: var(--muted); + font-family: var(--font); font-size: 0.75rem; font-weight: 400; vertical-align: middle; } .source { - margin-top: -0.5rem; + margin: 0 0 1rem; color: var(--muted); font-size: 0.8rem; } -.source a { - color: inherit; +.source a, +.source-link { + color: var(--muted); } +/* Members: one rule between each, their name and source on one row. */ .member { - margin: 1.5rem 0; - padding-top: 0.5rem; - border-top: 1px solid var(--border); + padding-block: 1.75rem; +} + +.member + .member { + border-top: 1px solid var(--border-subtle); } .member:focus { outline: none; } -.member > h3 { - margin: 0.5rem 0; +.member > :last-child, +.signature-block > :last-child { + margin-bottom: 0; +} + +.member-header + pre, +.signature-block > pre:first-child { + margin-top: 0; +} + +.member-header { + display: flex; + flex-wrap: wrap; + align-items: baseline; + justify-content: space-between; + gap: 0.25rem 1rem; + margin-bottom: 0.75rem; +} + +.member-name { + min-width: 0; + margin: 0; + padding: 0; + border: 0; font-family: var(--mono); - font-size: 1.1rem; - overflow-wrap: anywhere; + font-size: 1.25rem; + font-weight: 600; +} + +.source-link { + font-size: 0.8rem; } .anchor { @@ -347,42 +754,90 @@ main h2 { } .anchor:hover { - text-decoration: underline; + color: var(--accent); } -.deprecated > h3 .anchor, -dt.deprecated a, -.site a.deprecated { +/* Where a link to the member lands. */ +.member-name:target .anchor { + color: var(--accent); +} + +.deprecated > .member-header .anchor, +dt.deprecated a { text-decoration: line-through; } +/* Overloads, one after another. */ .signature-block + .signature-block { - margin-top: 1.5rem; + margin-top: 1rem; + padding-top: 1rem; + border-top: 1px dashed var(--border-subtle); } -.tag-title, -.member h4, -.member h5 { - margin: 1rem 0 0.25rem; +/* Parameters, returns and tags within a signature or member. */ +.detail-title { + margin: 1.25rem 0 0.4rem; + padding: 0; + border: 0; + font-family: var(--font); font-size: 0.95rem; + font-weight: 600; } -dl.index, +.detail-title + p { + margin-top: 0; +} + +/* Parameters: each over its description, as types can be long. */ dl.parameters { - display: grid; - grid-template-columns: minmax(auto, max-content) 1fr; - gap: 0.35rem 1.5rem; margin: 0.5rem 0; } -dl.index dt, +/* Wrapped types indent under their name. */ dl.parameters dt { - overflow-wrap: anywhere; + margin-top: 0.75rem; + padding-inline-start: 1rem; + text-indent: -1rem; +} + +dl.parameters dt:first-child { + margin-top: 0; +} + +.returns { + margin: 0.4rem 0 0; +} + +.returns-description { + margin: 0.2rem 0 0 1rem; +} + +.returns-description > :first-child { + margin-top: 0; +} + +.returns-description > :last-child { + margin-bottom: 0; +} + +dl.parameters dt > code, +.returns > code, +.relation > code, +dl.index dt code { + padding: 0; + background: none; +} + +dl.parameters dt > code > :first-child { + font-weight: 600; } -dl.index dd, dl.parameters dd { - margin: 0; + margin: 0.2rem 0 0 1rem; +} + +dd:empty { + display: none; } dd > p:first-child { @@ -393,16 +848,75 @@ dd > p:last-child { margin-bottom: 0; } -@media (width < 40rem) { - dl.index, - dl.parameters { - grid-template-columns: 1fr; +/* Indexes: names beside summaries where there is room, aligned throughout. */ +dl.index { + display: grid; + gap: 0 1.5rem; + margin: 0.75rem 0; +} + +/* Stacked, entries are apart whether or not they have summaries. */ +dl.index dt:not(:first-child) { + margin-top: 0.75rem; +} + +dl.index dd { + margin: 0.15rem 0 0 1rem; +} + +@container (inline-size >= 36rem) { + dl.index { + grid-template-columns: fit-content(20rem) minmax(0, 1fr); + row-gap: 0.6rem; } - dl.index dd, - dl.parameters dd { - margin: 0 0 0.75rem 1rem; + /* Short names align across sections; long ones widen theirs. */ + dl.index dt { + grid-column: 1; + min-width: 12rem; } + + dl.index dt:not(:first-child) { + margin-top: 0; + } + + dl.index dd { + grid-column: 2; + margin: 0; + } +} + +/* A module's README, after its index. */ +section.readme { + margin-top: 3rem; + padding-top: 1.5rem; + border-top: 1px solid var(--border-subtle); +} + +section.readme h1 { + font-size: 1.5rem; +} + +hr { + margin: 2rem 0; + border: 0; + border-top: 1px solid var(--border-subtle); +} + +blockquote { + margin: 1rem 0; + padding-inline-start: 1rem; + border-inline-start: 3px solid var(--border); + color: var(--muted); +} + +main h3:not(.member-name, .detail-title) { + margin: 1.75rem 0 0.5rem; + font-size: 1.2rem; +} + +main li + li { + margin-top: 0.25rem; } ul.types { @@ -426,7 +940,12 @@ table { th, td { padding: 0.35rem 0.75rem; - border: 1px solid var(--border); + border: 1px solid var(--border-subtle); + text-align: start; +} + +th { + background: var(--surface); } img { @@ -435,14 +954,28 @@ img { /* Layout IDs are capitalized: slugs, being lowercase, never take them. */ #Search { - width: min(40rem, 100vw - 2rem); + width: min(40rem, 100% - 2rem); max-height: min(32rem, 100dvh - 4rem); - margin-top: 4rem; + margin: 4rem auto auto; padding: 0; border: 1px solid var(--border); border-radius: 0.75rem; background: var(--background); color: var(--text); + box-shadow: 0 1rem 3rem rgb(0 0 0 / 25%); +} + +/* The input stays in place while the results scroll. */ +#Search[open] { + display: flex; + flex-direction: column; +} + +@media (width < 40rem) { + #Search { + max-height: min(32rem, 100dvh - 1.5rem); + margin-top: clamp(0.75rem, 6dvh, 4rem); + } } #Search:modal { @@ -455,59 +988,82 @@ img { background: rgb(0 0 0 / 40%); } -#Search input { - width: 100%; - padding: 0.85rem 1rem; - border: 0; - border-bottom: 1px solid var(--muted); - background: transparent; - color: inherit; - font: inherit; -} - .search-bar { display: flex; - justify-content: end; - padding: 0.5rem 0.5rem 0; + align-items: center; + gap: 0.5rem; + padding: 0.5rem 0.5rem 0.5rem 1rem; + border-bottom: 1px solid var(--border); } -.search-bar button { - padding: 0.25rem 0.6rem; - border: 1px solid var(--border); - border-radius: 0.4rem; - background: var(--surface); +/* The bar shows the input's focus. */ +.search-bar:focus-within { + border-bottom-color: var(--accent); +} + +.search-bar input::-webkit-search-cancel-button { + display: none; +} + +.search-bar input { + flex: 1; + min-width: 0; + padding: 0.4rem 0; + border: 0; + background: transparent; color: inherit; font: inherit; + outline: none; +} + +.search-bar form { + margin: 0; } .results { + flex: 1; margin: 0; padding: 0.5rem; + overflow-y: auto; list-style: none; } +.results:empty { + display: none; +} + +.results li:not(:has(a)) { + padding: 0.4rem 0.6rem; + color: var(--muted); +} + .results a { display: flex; gap: 0.75rem; align-items: baseline; + line-height: 1.35; padding: 0.4rem 0.6rem; border-radius: 0.4rem; color: var(--text); text-decoration: none; } +/* Enter in the input opens the first result. */ .results a:is(:hover, :focus-visible), -.results li:first-child a { +#Search:has(input:focus) .results li:first-child a { background: var(--surface); } -/* Qualified names wrap within narrow dialogs. */ +/* Qualified names wrap within narrow dialogs, after their dots. */ .results a > :first-child { - overflow-wrap: anywhere; + min-width: 0; + overflow-wrap: break-word; } .results .result-kind { + flex-shrink: 0; margin-left: auto; + white-space: nowrap; color: var(--muted); font-size: 0.8rem; } @@ -515,3 +1071,322 @@ img { .results .result-parent { color: var(--muted); } + +/* + * Motion and color: small touches, each with a job. Movement is only for + * those who have not asked for less; fades of color are for everyone. + */ + +/* Pages change in place: the header and navigation stay, the page fades in. */ +@media (prefers-reduced-motion: no-preference) { + @view-transition { + navigation: auto; + } + + .top { + view-transition-name: top; + } +} + +@media (prefers-reduced-motion: no-preference) and (width >= 60rem) { + .site { + view-transition-name: site; + } +} + +/* The contents bar holds still under the header, too. */ +@media (prefers-reduced-motion: no-preference) and (width < 80rem) { + .toc { + view-transition-name: toc; + } +} + +::view-transition-old(root) { + animation: 0.12s ease-out both fade-out; +} + +::view-transition-new(root) { + animation: 0.22s ease-out both page-in; +} + +@keyframes fade-out { + to { + opacity: 0; + } +} + +@keyframes page-in { + from { + opacity: 0; + translate: 0 0.4rem; + } +} + +/* A thread of the syntax colors along the top. */ +.top::before { + content: ""; + position: absolute; + inset: 0 0 auto; + block-size: 2px; + background: linear-gradient( + 90deg, + var(--accent), + var(--name) 55%, + light-dark(#fb8f44, #ffa657) + ); +} + +::selection { + background: color-mix(in srgb, var(--accent) 22%, transparent); +} + +/* What a page documents, marked in the color its names have in code. */ +[data-kind] { + --kind: var(--accent); +} + +[data-kind="Class"] { + --kind: var(--name); +} + +:is([data-kind="Interface"], [data-kind="Type Alias"]) { + --kind: var(--type); +} + +:is([data-kind="Function"], [data-kind="Method"], [data-kind="Constructor"]) { + --kind: var(--parameter); +} + +:is([data-kind="Variable"], [data-kind="Property"], [data-kind="Accessor"]) { + --kind: var(--green); +} + +:is([data-kind="Enumeration"], [data-kind="Enumeration Member"]) { + --kind: var(--keyword); +} + +.kind[data-kind]::before, +.result-kind[data-kind]::before { + content: ""; + display: inline-block; + inline-size: 0.5em; + block-size: 0.5em; + margin-inline-end: 0.4em; + border-radius: 50%; + background: var(--kind); + vertical-align: 0.1em; +} + +/* Arriving at a member, it glows briefly, a little wider than its text. */ +.member:has(> .member-header > .member-name:target) { + animation: arrive 2s ease-out; +} + +/* Its glow lands clear of the sticky bars above it. */ +.member-name { + scroll-margin-top: 1.25rem; +} + +@keyframes arrive { + from, + 30% { + background-color: color-mix(in srgb, var(--accent) 10%, transparent); + box-shadow: + calc(-1 * var(--bleed)) 0 + color-mix(in srgb, var(--accent) 10%, transparent), + var(--bleed) 0 color-mix(in srgb, var(--accent) 10%, transparent); + } +} + +/* The glow reaches the screen's edges on phones. */ +.member { + --bleed: 0.75rem; +} + +@media (width < 60rem) { + .member { + --bleed: 1rem; + } +} + +/* Members' links show themselves on hover, in the margin before the name, + taking no room from what follows. */ +.member-name { + position: relative; +} + +.member-name .anchor::before { + content: "#" / ""; + position: absolute; + inset-inline-end: 100%; + padding-inline-end: 0.3em; + color: var(--muted); + font-weight: 400; + opacity: 0; + transition: opacity 0.15s; +} + +.anchor:focus-visible::before { + opacity: 1; +} + +/* Hovered, where pointers hover and the margin has room for it. */ +@media (hover: hover) and (width >= 60rem) { + .member-header:hover .anchor::before { + opacity: 1; + } +} + +/* On phones, its focus mark stays within the screen. */ +@media (width < 60rem) { + .member-name .anchor::before { + display: none; + } +} + +/* Links, rails and buttons respond softly. */ +a { + transition: + color 0.15s, + text-decoration-color 0.15s; +} + +.site a, +.toc a { + transition: + color 0.15s, + border-color 0.15s; +} + +.site a:not([aria-current="page"]):hover, +.toc a:not([aria-current="true"]):hover { + border-inline-start-color: var(--border); +} + +@supports (scroll-target-group: auto) { + .toc a:target-current:hover { + border-inline-start-color: var(--accent); + } +} + +.top button, +.site .close-button, +.search-bar button { + transition: + border-color 0.15s, + background-color 0.15s; +} + +@media (prefers-reduced-motion: no-preference) { + .top button, + .site .close-button, + .search-bar button { + transition: + border-color 0.15s, + background-color 0.15s, + scale 0.1s; + } + + .top button:active, + .site .close-button:active, + .search-bar button:active { + scale: 0.97; + } +} + +/* Search opens with a fade, and a little drop where motion is welcome. */ +#Search[open] { + opacity: 1; + transition: opacity 0.15s ease-out; +} + +@starting-style { + #Search[open] { + opacity: 0; + } +} + +@media (prefers-reduced-motion: no-preference) { + #Search[open] { + translate: 0 0; + transition: + opacity 0.15s ease-out, + translate 0.2s ease-out; + } + + @starting-style { + #Search[open] { + translate: 0 -0.5rem; + } + } +} + +/* Backdrops darken gradually. */ +#Search::backdrop, +.site::backdrop { + transition: background-color 0.2s; +} + +@starting-style { + #Search[open]::backdrop, + .site:popover-open::backdrop { + background-color: transparent; + } +} + +/* Collapsed contents fade open. */ +@media (width < 80rem) { + .toc details::details-content { + opacity: 0; + transition: + opacity 0.15s ease-out, + content-visibility 0.15s; + transition-behavior: allow-discrete; + } + + .toc details[open]::details-content { + opacity: 1; + } +} + +/* A slim chevron, turning as the contents open. */ +.toc summary { + display: flex; + align-items: center; + gap: 0.5em; + list-style: none; +} + +.toc summary::-webkit-details-marker { + display: none; +} + +.toc summary::before { + content: ""; + inline-size: 0.4em; + block-size: 0.4em; + border-block-end: 1.5px solid currentColor; + border-inline-end: 1.5px solid currentColor; + rotate: -45deg; +} + +.toc details[open] > summary::before { + rotate: 45deg; +} + +.toc summary:dir(rtl)::before { + rotate: 135deg; +} + +@media (prefers-reduced-motion: no-preference) { + .toc summary::before { + transition: rotate 0.15s ease-out; + } +} + +/* As a column, the contents need no control. */ +@media (width >= 80rem) { + .toc summary::before { + display: none; + } +} diff --git a/src/convert.ts b/src/convert.ts index cad9020..3f119f0 100644 --- a/src/convert.ts +++ b/src/convert.ts @@ -456,8 +456,15 @@ function relativizeSources(modules: Module[]): void { base = dirname(base); } } + const dependencies = `${sep}node_modules${sep}`; for (const source of all) { - source.file = relative(base, source.file).replaceAll("\\", "/"); + // Dependencies' files are named within their packages. + const index = source.file.lastIndexOf(dependencies); + source.file = ( + index === -1 + ? relative(base, source.file) + : source.file.slice(index + dependencies.length) + ).replaceAll("\\", "/"); } } diff --git a/src/render/html.ts b/src/render/html.ts index 8f5a6b5..c1b9301 100644 --- a/src/render/html.ts +++ b/src/render/html.ts @@ -91,7 +91,12 @@ function pageHeading(declaration: Declaration): Result { ].filter((badge) => typeof badge === "string"); return h( "header.page-header", - h("p.kind", kindNames[declaration.kind]), + h( + "p.kind", + { "data-kind": kindNames[declaration.kind] }, + kindNames[declaration.kind], + badges.flatMap((badge) => [" ", h("span.badge", badge)]), + ), h( "h1", declaration.name, @@ -103,10 +108,54 @@ function pageHeading(declaration: Declaration): Result { ) : undefined, ), - badges.flatMap((badge) => [" ", h("span.badge", badge)]), ); } +/** + * @param sources A member's sources. + * @returns A link to the first, for the member's heading row. + */ +function sourceLink(sources: Source[] | undefined): Result | undefined { + if (sources === undefined || sources.length === 0) return undefined; + const [source] = sources; + const label = `${source.file}:${source.line}`; + return source.url === undefined + ? h("span.source-link", label) + : h( + "a.source-link", + { href: source.url, "aria-label": `Source: ${label}` }, + label, + ); +} + +/** + * Keeps the navigation's scroll position from page to page of a site: run + * right after it, so pages show it in place from their first frame. + */ +const restoreNavigation = `try { + const site = document.getElementById("Site"); + const key = "typeref:nav:" + new URL(document.body.dataset.base, location.href).pathname; + const restore = () => { + site.scrollTop = Number(sessionStorage.getItem(key)); + }; + restore(); + // Prerendered pages run this early: again when they are shown. + if (document.prerendering) { + document.addEventListener("prerenderingchange", restore, { once: true }); + } + addEventListener("pagehide", () => sessionStorage.setItem(key, String(site.scrollTop))); +} catch {}`; + +/** + * @param name A name, such as `getDocumentTypeNodeName` or `parse5-sax-parser`. + * @returns It, able to break between its words in narrow columns. + */ +function breakable(name: string): (Result | string)[] { + return name + .split(/(?<=[a-z\d])(?=[A-Z])|(?<=[._-])(?=.)/u) + .flatMap((part, index) => (index === 0 ? [part] : [h("wbr"), part])); +} + export interface HtmlOptions { /** * Classes, interfaces and enums with at least this many members get a page @@ -123,6 +172,8 @@ class HtmlWriter { private page = ""; /** The members documented on the current page. */ private readonly rendered = new Set(); + /** The IDs of the sections' headings on the current page. */ + private readonly sectionAnchors = new Map(); constructor( project: Project, @@ -148,9 +199,11 @@ class HtmlWriter { defaultColor: false, }).children : []; - return child?.type === "element" - ? child - : defaultHandlers.code(state, node); + if (child?.type === "element") return child; + // Code scrolls, in a region keyboards can reach, as Shiki's does. + const pre = defaultHandlers.code(state, node); + pre.properties.tabIndex = 0; + return pre; }, // Wide tables scroll in a region keyboards can reach (Safari's cannot). table: (state, node) => @@ -183,6 +236,7 @@ class HtmlWriter { for (const page of this.router.pages) { this.page = page.url; this.rendered.clear(); + this.sectionAnchors.clear(); let title: string; let content: Result; if (page.kind === "readme") { @@ -260,13 +314,20 @@ class HtmlWriter { } private inlineText(parts: Part[] | undefined): Child { - return toHast( + const tree = toHast( { type: "root", children: inlineMarkdown(parts ?? [], (target) => this.link(target)), }, this.markdown, ); + // Roots' children are joined by line breaks, which are spaces inline. + if (tree.type === "root") { + tree.children = tree.children.filter( + (child) => child.type !== "text" || child.value !== "\n", + ); + } + return tree; } private tokens(list: Token[]): (Result | string)[] { @@ -275,6 +336,7 @@ class HtmlWriter { const url = token.target && this.link(token.target); const className = { keyword: "k", + intrinsic: "t", name: "n", literal: "l", parameter: "p", @@ -334,6 +396,16 @@ class HtmlWriter { : undefined, h("link", { rel: "stylesheet", href: `${base}assets/style.css` }), h("script", { type: "module", src: `${base}assets/main.js` }), + // Pages a reader points at load ahead, where browsers can. + h( + "script", + { type: "speculationrules" }, + JSON.stringify({ + prerender: [ + { where: { href_matches: "/*" }, eagerness: "moderate" }, + ], + }), + ), ), h( "body", @@ -341,6 +413,12 @@ class HtmlWriter { h("a.skip", { href: "#Main" }, "Skip to content"), h( "header.top", + // Narrow screens show the site's navigation on demand. + h( + "button.menu-button", + { type: "button", popovertarget: "Site" }, + "Menu", + ), h( "a.project", { href: this.url(this.router.pages[0].url) }, @@ -364,25 +442,53 @@ class HtmlWriter { ), h( "div.layout", - h("main#Main", { tabindex: "-1" }, content), + h( + "nav#Site.site", + { "aria-label": "API", popover: "auto" }, + h( + "div.site-bar", + h( + "span.site-title", + project.name, + project.version !== undefined && project.version !== "" + ? [" ", h("span.version", project.version)] + : undefined, + ), + h( + "button.close-button", + { + type: "button", + popovertarget: "Site", + popovertargetaction: "hide", + }, + "Close", + ), + ), + this.navigation(page), + ), + // Before the first frame, which page transitions show. + h("script", restoreNavigation), this.toc(page), - h("nav.site", { "aria-label": "API" }, this.navigation(page)), + h("main#Main", { tabindex: "-1" }, content), ), h( "dialog#Search", { closedby: "any", "aria-label": "Search" }, h( - "form", - { method: "dialog", class: "search-bar" }, - h("button", { type: "submit" }, "Close"), + "div.search-bar", + h("input", { + type: "search", + placeholder: "Search the API", + "aria-label": "Search the API", + autocomplete: "off", + autofocus: true, + }), + h( + "form", + { method: "dialog" }, + h("button", { type: "submit" }, "Close"), + ), ), - h("input", { - type: "search", - placeholder: "Search the API", - "aria-label": "Search the API", - autocomplete: "off", - autofocus: true, - }), h("ul.results", { "aria-live": "polite" }), ), ), @@ -397,9 +503,29 @@ class HtmlWriter { return h( null, this.project.readme !== undefined && this.project.readme !== "" - ? h("p", [ - h("a", { href: this.url(this.router.pages[0].url) }, "Overview"), - h("a", { href: this.url(this.router.indexUrl) }, "API reference"), + ? h("ul", [ + h( + "li", + h( + "a", + { + href: this.url(this.router.pages[0].url), + "aria-current": page.kind === "readme" ? "page" : undefined, + }, + "Overview", + ), + ), + h( + "li", + h( + "a", + { + href: this.url(this.router.indexUrl), + "aria-current": page.kind === "index" ? "page" : undefined, + }, + "API reference", + ), + ), ]) : undefined, sections(topLevel(this.project)).flatMap((section) => [ @@ -428,7 +554,7 @@ class HtmlWriter { ? "deprecated" : undefined, }, - declaration.name, + breakable(declaration.name), ), categories && h( @@ -468,28 +594,40 @@ class HtmlWriter { const categories = page.section ? undefined : this.router.categoryPages(current); - const list = page.section - ? [ - { - ...page.section, - // Members of several categories are on their first one's page. - members: page.section.members.filter( - (member) => this.router.url(member)?.split("#")[0] === page.url, + let content: Result[]; + if (categories) { + // The page indexes its categories, which have pages of their own. + content = [ + h( + "ul", + categories.map(({ section }) => + h( + "li", + h( + "a", + { href: `#${this.sectionAnchor(section!.title)}` }, + section!.title, + ), ), - }, - ] - : (categories?.map((category) => category.section!) ?? - sections(current.children ?? [])); - if (list.every((section) => section.members.length === 0)) { - return undefined; - } - // The index of categories' pages links to members on those pages. - const label = categories ? "Members" : "On this page"; - return h( - "nav.toc", - { "aria-label": label }, - h("h2", label), - list.flatMap((section) => [ + ), + ), + ]; + } else { + const list = page.section + ? [ + { + ...page.section, + // Members of several categories are on their first one's page. + members: page.section.members.filter( + (member) => this.router.url(member)?.split("#")[0] === page.url, + ), + }, + ] + : sections(current.children ?? []); + if (list.every((section) => section.members.length === 0)) { + return undefined; + } + content = list.flatMap((section) => [ h("h3", section.title), h( "ul", @@ -497,13 +635,32 @@ class HtmlWriter { const url = this.memberUrl(member); return url === undefined ? undefined - : h("li", h("a", { href: url }, member.name)); + : h("li", h("a", { href: url }, breakable(member.name))); }), ), - ]), + ]); + } + // Collapsed where the page is too narrow for it to have a column. + return h( + "nav.toc", + { "aria-label": "On this page" }, + h("details", h("summary", "On this page"), h("div.toc-list", content)), ); } + /** + * @param title A section's title. + * @returns The ID of its heading on the current page. + */ + private sectionAnchor(title: string): string { + let anchor = this.sectionAnchors.get(title); + if (anchor === undefined) { + anchor = this.router.anchor(this.page, title); + this.sectionAnchors.set(title, anchor); + } + return anchor; + } + private index(project: Project): Result { // With one entry point, the index is the module's page. return h( @@ -531,6 +688,7 @@ class HtmlWriter { "header.page-header", h( "p.kind", + { "data-kind": kindNames[declaration.kind] }, `${kindNames[declaration.kind]} `, h("a", { href: this.memberUrl(declaration) }, declaration.name), ), @@ -551,6 +709,8 @@ class HtmlWriter { "section", h( "h2", + // The index of a class's categories outlines them. + url === undefined ? {} : { id: this.sectionAnchor(section.title) }, url === undefined ? section.title : h("a", { href: this.url(url) }, section.title), @@ -761,7 +921,7 @@ class HtmlWriter { level: number, ): Result[] { return tags.map(({ title, content }) => - h("section.tag", h(`h${level}.tag-title`, title), this.text(content)), + h("section.tag", h(`h${level}.detail-title`, title), this.text(content)), ); } @@ -774,7 +934,7 @@ class HtmlWriter { } return h( null, - h(`h${level}`, "Type parameters"), + h(`h${level}.detail-title`, "Type parameters"), h( "dl.parameters", parameters.flatMap((parameter) => [ @@ -784,8 +944,8 @@ class HtmlWriter { "code", this.tokens( tokens((printer) => { - printer.typeParameters([parameter], true); - }).slice(1, -1), + printer.typeParameter(parameter, true); + }), ), ), ), @@ -802,7 +962,7 @@ class HtmlWriter { if (parameters.length === 0) return undefined; return h( null, - h(`h${level}`, "Parameters"), + h(`h${level}.detail-title`, "Parameters"), h( "dl.parameters", parameters.flatMap((parameter) => [ @@ -823,15 +983,24 @@ class HtmlWriter { ); } + /** + * @param declaration A declaration with signatures. + * @param level The level of their details' headings. + * @param prefix A keyword before each, such as `function`. + * @param isSourceShown Whether each shows its source: not when the + * declaration's heading shows the only one. + * @returns The signatures. + */ private signatures( declaration: Declaration, level: number, prefix: string, + isSourceShown = true, ): Result[] { return signaturesOf(declaration).map((signature) => h( "div.signature-block", - this.signature(signature, level, prefix, declaration), + this.signature(signature, level, prefix, declaration, isSourceShown), ), ); } @@ -841,6 +1010,7 @@ class HtmlWriter { level: number, prefix: string, declaration: Declaration, + isSourceShown: boolean, ): Result { // Construct signatures are named `new`, methods may be too. const name = @@ -860,7 +1030,7 @@ class HtmlWriter { printer.signature(signature, name); } }), - definedIn(signature.sources), + isSourceShown ? definedIn(signature.sources) : undefined, this.text(signature.comment?.summary), this.signatureDetails(signature, level, declaration), ); @@ -881,9 +1051,11 @@ class HtmlWriter { this.parameterList(signature.parameters, level), ...(signature.returns ? [ - h(`h${level}`, "Returns"), - h("p", h("code", this.type(signature.returns))), - this.text(returnsOf(comment)), + h(`h${level}.detail-title`, "Returns"), + h("p.returns", h("code", this.type(signature.returns))), + (returnsOf(comment) ?? []).length > 0 + ? h("div.returns-description", this.text(returnsOf(comment))) + : undefined, ] : []), this.tags(blockTags(comment), level), @@ -931,26 +1103,30 @@ class HtmlWriter { ["static", "abstract", "protected", "readonly", "optional"] as const ).filter((flag) => member.flags[flag]); if (isDeprecated(member)) flags.push("deprecated" as "static"); - const heading = h( - `h${level}`, - { id: anchor }, - h("a.anchor", { href: `#${anchor}` }, member.name), - flags.flatMap((flag) => [" ", h("span.badge", flag)]), - ); + // A member's only source is on its heading's row; overloads show theirs. + let sources: Source[] | undefined; let body: Child; switch (member.kind) { case "Constructor": case "Method": { - body = this.signatures(member, level + 1, ""); + const list = signaturesOf(member); + if (list.length === 1) [{ sources }] = list; + body = this.signatures(member, level + 1, "", list.length > 1); break; } case "Accessor": { + const accessors = [member.getSignature, member.setSignature]; + const present = accessors.filter( + (signature) => signature !== undefined, + ); + if (present.length === 1) [{ sources }] = present; body = h( null, - [member.getSignature, member.setSignature].map((signature, index) => + accessors.map((signature, index) => signature ? h( - null, + // Getters and setters are apart, as overloads are. + "div.signature-block", this.code((printer) => { printer .modifiers(member.flags) @@ -958,7 +1134,7 @@ class HtmlWriter { .punctuation(" ") .signature(signature, member.name); }), - definedIn(signature.sources), + present.length > 1 ? definedIn(signature.sources) : undefined, this.text(signature.comment?.summary), this.signatureDetails(signature, level + 1, member), ) @@ -983,6 +1159,7 @@ class HtmlWriter { break; } default: { + ({ sources } = member); body = h( null, this.code((printer) => { @@ -996,7 +1173,6 @@ class HtmlWriter { .tokens.push({ kind: "literal", text: member.defaultValue }); } }), - definedIn(member.sources), // Members of function types are described as functions. member.type?.type === "function" ? h( @@ -1014,7 +1190,16 @@ class HtmlWriter { class: isDeprecated(member) ? "member deprecated" : "member", tabindex: anchor === undefined ? undefined : "-1", }, - heading, + h( + "div.member-header", + h( + `h${level}.member-name`, + { id: anchor }, + h("a.anchor", { href: `#${anchor}` }, breakable(member.name)), + flags.flatMap((flag) => [" ", h("span.badge", flag)]), + ), + sourceLink(sources), + ), body, ); } diff --git a/src/render/types.ts b/src/render/types.ts index eaba9ea..c181394 100644 --- a/src/render/types.ts +++ b/src/render/types.ts @@ -15,7 +15,13 @@ import type { } from "../model.ts"; export interface Token { - kind: "punctuation" | "keyword" | "name" | "literal" | "parameter"; + kind: + | "punctuation" + | "keyword" + | "intrinsic" + | "name" + | "literal" + | "parameter"; text: string; target?: Target; } @@ -123,7 +129,7 @@ export class TypePrinter { if (isWrapped) this.punctuation("("); switch (type.type) { case "intrinsic": { - this.keyword(type.name); + this.tokens.push({ kind: "intrinsic", text: type.name }); break; } case "literal": { @@ -280,21 +286,31 @@ export class TypePrinter { if (parameters === undefined || parameters.length === 0) return this; this.punctuation("<"); this.list(parameters, (parameter) => { - for (const modifier of parameter.modifiers ?? []) { - this.keyword(modifier).punctuation(" "); - } - this.name(parameter.name); - if (isFull && parameter.constraint) { - this.punctuation(" ").keyword("extends").punctuation(" "); - this.type(parameter.constraint); - } - if (isFull && parameter.default) { - this.punctuation(" = ").type(parameter.default); - } + this.typeParameter(parameter, isFull); }); return this.punctuation(">"); } + /** + * @param parameter A type parameter. + * @param isFull Whether to print its constraint and default. + * @returns The printer, with the parameter: `in T extends U = V`. + */ + typeParameter(parameter: TypeParameter, isFull = false): this { + for (const modifier of parameter.modifiers ?? []) { + this.keyword(modifier).punctuation(" "); + } + this.name(parameter.name); + if (isFull && parameter.constraint) { + this.punctuation(" ").keyword("extends").punctuation(" "); + this.type(parameter.constraint); + } + if (isFull && parameter.default) { + this.punctuation(" = ").type(parameter.default); + } + return this; + } + /** * @param flags A member's flags. * @returns The printer, with the member's modifiers in TypeScript's order. diff --git a/src/source.ts b/src/source.ts index 7e0cf75..0c4ae86 100644 --- a/src/source.ts +++ b/src/source.ts @@ -91,9 +91,12 @@ export class Sources { /** * @param file A file. * @returns A link to the file in its repository, at the checked-out - * commit; `undefined` outside of repositories on GitHub or GitLab. + * commit; `undefined` outside of repositories on GitHub or GitLab, and + * for installed dependencies. */ fileUrl(file: string): string | undefined { + // Installed dependencies are not in their dependents' repositories. + if (file.split(/[/\\]/u).includes("node_modules")) return undefined; const repository = this.getRepository(dirname(file)); if (!repository) return undefined; const path = relative(repository.root, file) diff --git a/test/fixtures/rendering.ts b/test/fixtures/rendering.ts index 04eeafc..6d7beee 100644 --- a/test/fixtures/rendering.ts +++ b/test/fixtures/rendering.ts @@ -111,3 +111,13 @@ export interface Handlers { */ handle: (value: string) => boolean; } + +/** + * The first of a list. + * + * @returns The first item. + */ +export declare function first>(list: R): T; + +/** Accepts a `function`. */ +export declare function accepts(callback: () => void): void; diff --git a/test/render.test.ts b/test/render.test.ts index 505c40b..7b9a231 100644 --- a/test/render.test.ts +++ b/test/render.test.ts @@ -255,7 +255,8 @@ describe("renderHtml", () => { it("links the headings of members of object types to their anchors", async () => { const files = await renderHtml(fixture(["rendering"])); expect(files.get("types/Hooks.html")).toMatch( - /

onError<\/a>/u, + // CamelCase names may break between their words. + /

onError<\/a>/u, ); }); @@ -285,6 +286,8 @@ describe("renderHtml", () => { files .get("interfaces/Multi/index-category.html")! .includes('class="toc"'), + // Its outline is of the categories' indexes, on the page. + /