Skip to content

Commit 9c4f457

Browse files
committed
doc: improve navigability of API docs
1 parent a706342 commit 9c4f457

File tree

3 files changed

+97
-30
lines changed

3 files changed

+97
-30
lines changed

doc/api_assets/style.css

Lines changed: 70 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -159,56 +159,66 @@ em code {
159159
margin-bottom: 1rem;
160160
}
161161

162-
#gtoc ul {
162+
#gtoc > ul {
163163
list-style: none;
164164
margin-left: 0;
165165
line-height: 1.5rem;
166166
}
167167

168-
li.version-picker {
168+
li.picker-header {
169169
position: relative;
170170
}
171171

172-
li.version-picker:hover > a {
172+
li.picker-header:hover > a {
173173
border-radius: 2px 2px 0 0;
174174
}
175175

176-
li.version-picker:hover > ol {
176+
li.picker-header:hover > .picker {
177177
display: block;
178178
z-index: 1;
179179
}
180180

181-
li.version-picker a span {
181+
li.picker-header a span {
182182
font-size: .7rem;
183183
}
184184

185-
ol.version-picker {
185+
.picker {
186186
background-color: var(--color-fill-app);
187187
border: 1px solid var(--color-brand-secondary);
188188
border-radius: 0 0 2px 2px;
189189
display: none;
190190
list-style: none;
191191
position: absolute;
192-
right: 0;
192+
left: 0;
193193
top: 100%;
194-
width: 100%;
194+
width: max-content;
195+
min-width: min(300px, 75vw);
196+
max-width: 75vw;
197+
max-height: min(600px, 60vh);
198+
overflow-y: auto;
199+
}
200+
201+
.picker > ul, .picker > ol {
202+
list-style: none;
203+
margin-left: 0;
204+
line-height: 1.5rem;
195205
}
196206

197-
#gtoc ol.version-picker li {
207+
.picker li {
198208
display: block;
199209
border-right: 0;
200210
margin-right: 0;
201211
}
202212

203-
ol.version-picker li a {
213+
.picker li a {
204214
border-radius: 0;
205215
display: block;
206216
margin: 0;
207217
padding: .1rem;
208218
padding-left: 1rem;
209219
}
210220

211-
ol.version-picker li:last-child a {
221+
.picker li:last-child a {
212222
border-bottom-right-radius: 1px;
213223
border-bottom-left-radius: 1px;
214224
}
@@ -219,6 +229,11 @@ ol.version-picker li:last-child a {
219229
padding-bottom: 1px;
220230
}
221231

232+
.picker .line {
233+
margin: 0;
234+
width: 100%;
235+
}
236+
222237
.api_stability {
223238
color: var(--white) !important;
224239
margin: 0 0 1rem;
@@ -499,6 +514,41 @@ hr {
499514
margin-top: .666rem;
500515
}
501516

517+
.toc ul {
518+
margin: 0
519+
}
520+
521+
.toc li a::before {
522+
content: "■";
523+
color: var(--color-text-primary);
524+
padding-right: 1em;
525+
font-size: 0.9em;
526+
}
527+
528+
.toc li a:hover::before {
529+
color: var(--white);
530+
}
531+
532+
.toc ul ul a {
533+
padding-left: 1rem;
534+
}
535+
536+
.toc ul ul ul a {
537+
padding-left: 2rem;
538+
}
539+
540+
.toc ul ul ul ul a {
541+
padding-left: 3rem;
542+
}
543+
544+
.toc ul ul ul ul ul a {
545+
padding-left: 4rem;
546+
}
547+
548+
.toc ul ul ul ul ul ul a {
549+
padding-left: 5rem;
550+
}
551+
502552
#toc .stability_0::after {
503553
background-color: var(--red2);
504554
color: var(--white);
@@ -711,10 +761,18 @@ kbd {
711761
}
712762
}
713763

764+
.header {
765+
position: sticky;
766+
top: 0;
767+
background-color: var(--color-fill-app);
768+
padding-top: 1.5rem;
769+
z-index: 1;
770+
}
771+
714772
.header-container {
715773
display: flex;
716774
align-items: center;
717-
margin: 1.5rem 0 1rem;
775+
margin-bottom: 1rem;
718776
justify-content: space-between;
719777
}
720778

doc/template.html

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
</div>
2323

2424
<div id="column1" data-id="__ID__" class="interior">
25-
<header>
25+
<header class="header">
2626
<div class="header-container">
2727
<h1>Node.js __VERSION__ documentation</h1>
2828
<button class="theme-toggle-btn" id="theme-toggle-btn" title="Toggle dark mode/light mode" aria-label="Toggle dark mode/light mode" hidden>
@@ -39,16 +39,23 @@ <h1>Node.js __VERSION__ documentation</h1>
3939
</div>
4040
<div id="gtoc">
4141
<ul>
42-
<li>
43-
<a href="index.html">Index</a>
42+
<li class="picker-header">
43+
<a href="#">Table of contents <span>&#x25bc;</span></a>
44+
45+
<div class="picker">__TOC_PICKER__</div>
46+
</li>
47+
__ALTDOCS__
48+
<li class="picker-header">
49+
<a href="index.html">Index <span>&#x25bc;</span></a>
50+
51+
<div class="picker">__GTOC__</div>
4452
</li>
4553
<li>
4654
<a href="all.html">View on single page</a>
4755
</li>
4856
<li>
4957
<a href="__FILENAME__.json">View as JSON</a>
5058
</li>
51-
__ALTDOCS__
5259
__EDIT_ON_GITHUB__
5360
</ul>
5461
</div>

tools/doc/html.mjs

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,9 @@ export function toHTML({ input, content, filename, nodeVersion, versions }) {
9898
.replace(/__FILENAME__/g, filename)
9999
.replace('__SECTION__', content.section)
100100
.replace(/__VERSION__/g, nodeVersion)
101-
.replace('__TOC__', content.toc)
102-
.replace('__GTOC__', gtocHTML.replace(
101+
.replace(/__TOC__/g, content.toc)
102+
.replace(/__TOC_PICKER__/g, content.tocPicker)
103+
.replace(/__GTOC__/g, gtocHTML.replace(
103104
`class="nav-${id}"`, `class="nav-${id} active"`))
104105
.replace('__EDIT_ON_GITHUB__', editOnGitHub(filename))
105106
.replace('__CONTENT__', processContent(content));
@@ -442,17 +443,18 @@ export function buildToc({ filename, apilinks }) {
442443
});
443444

444445
if (toc !== '') {
445-
file.toc = '<details id="toc" open><summary>Table of contents</summary>' +
446-
unified()
447-
.use(markdown)
448-
.use(gfm)
449-
.use(remark2rehype, { allowDangerousHtml: true })
450-
.use(raw)
451-
.use(htmlStringify)
452-
.processSync(toc).toString() +
453-
'</details>';
446+
const inner = unified()
447+
.use(markdown)
448+
.use(gfm)
449+
.use(remark2rehype, { allowDangerousHtml: true })
450+
.use(raw)
451+
.use(htmlStringify)
452+
.processSync(toc).toString();
453+
454+
file.toc = `<details id="toc" open><summary>Table of contents</summary>${inner}</details>`;
455+
file.tocPicker = `<div class="toc">${inner}</div>`;
454456
} else {
455-
file.toc = '<!-- TOC -->';
457+
file.toc = file.tocPicker = '<!-- TOC -->';
456458
}
457459
};
458460
}
@@ -508,9 +510,9 @@ function altDocs(filename, docCreated, versions) {
508510
const list = versions.filter(isDocInVersion).map(wrapInListItem).join('\n');
509511

510512
return list ? `
511-
<li class="version-picker">
513+
<li class="picker-header">
512514
<a href="#">View another version <span>&#x25bc;</span></a>
513-
<ol class="version-picker">${list}</ol>
515+
<div class="picker"><ol id="alt-docs">${list}</ol></div>
514516
</li>
515517
` : '';
516518
}

0 commit comments

Comments
 (0)