Skip to content

Commit 8d083da

Browse files
clean up doc template (#2712)
Co-authored-by: Phil Hawksworth <phil@deno.com>
1 parent 250dfae commit 8d083da

File tree

1 file changed

+39
-54
lines changed

1 file changed

+39
-54
lines changed

_includes/doc.tsx

Lines changed: 39 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -2,64 +2,54 @@ import renderCommand from "./renderCommand.tsx";
22

33
export const layout = "layout.tsx";
44

5-
export const ogImage = (data: Lume.Data) => {
6-
return data.url + "/index.png";
7-
};
5+
export const ogImage = (data: Lume.Data) => `${data.url}/index.png`;
86

97
export default function Doc(data: Lume.Data, helpers: Lume.Helpers) {
10-
let file = data.page.sourcePath;
11-
const sidebar = data.sidebar;
12-
let renderedCommand = null;
13-
14-
if (data.command) {
15-
const { rendered, toc } = renderCommand(data.command, helpers);
16-
renderedCommand = rendered;
17-
data.toc = toc.concat(...data.toc);
18-
}
19-
8+
// Flags and simple derivations
9+
const API_LANDING = new Set(["/api/deno/", "/api/web/", "/api/node/"]);
2010
const isReference = data.url.startsWith("/api/");
21-
const isApiLandingPage = ["/api/deno/", "/api/web/", "/api/node/"].includes(
22-
data.url,
11+
const isApiLandingPage = API_LANDING.has(data.url);
12+
const isExampleScript = Boolean(
13+
(data.page.data.content as { type?: string })?.type,
2314
);
24-
const isExamples = data.url.startsWith("/examples/");
25-
const isExampleScript = (data.page.data.content as { type?: string })?.type;
2615
const isLintRule = data.url.startsWith("/lint/rules/");
27-
const isHome = data.url === "/";
2816

29-
const hasBreadcrumbs = !isExamples && !isHome &&
30-
!(isReference && !isApiLandingPage);
17+
// Compute file path used by Feedback component
18+
const file = isLintRule
19+
? `/lint/rules/${encodeURIComponent(data.title ?? "")}.md`
20+
: data.page.sourcePath;
3121

32-
if (isLintRule) {
33-
file = `/lint/rules/${encodeURIComponent(data.title ?? "")}.md`;
22+
// Render command block and merge its TOC if present
23+
let renderedCommand: unknown = null;
24+
if (data.command) {
25+
const { rendered, toc } = renderCommand(data.command, helpers);
26+
renderedCommand = rendered;
27+
data.toc = toc.concat(...data.toc);
3428
}
3529

3630
function getTocCtx(
37-
d: Lume.Data,
31+
d: unknown,
3832
): { document_navigation: unknown; document_navigation_str: string } | null {
39-
const ch: unknown = (d as unknown as { children?: unknown })?.children;
40-
if (ch && typeof ch === "object" && "props" in ch) {
41-
const props: unknown = (ch as { props?: unknown }).props;
42-
if (props && typeof props === "object" && "data" in props) {
43-
const pdata: unknown = (props as { data?: unknown }).data;
44-
if (pdata && typeof pdata === "object" && "toc_ctx" in pdata) {
45-
const toc: unknown = (pdata as { toc_ctx?: unknown }).toc_ctx;
46-
if (
47-
toc && typeof toc === "object" &&
48-
"document_navigation" in toc &&
49-
"document_navigation_str" in toc
50-
) {
51-
const t = toc as {
52-
document_navigation: unknown;
53-
document_navigation_str: string;
54-
};
55-
return t;
56-
}
57-
}
58-
}
33+
type Toc = {
34+
document_navigation: unknown;
35+
document_navigation_str: string;
36+
};
37+
const tocCtx: unknown = (d as {
38+
children?: { props?: { data?: { toc_ctx?: unknown } } };
39+
})?.children?.props?.data?.toc_ctx;
40+
if (
41+
tocCtx &&
42+
typeof tocCtx === "object" &&
43+
"document_navigation" in tocCtx &&
44+
"document_navigation_str" in tocCtx
45+
) {
46+
return tocCtx as Toc;
5947
}
6048
return null;
6149
}
6250

51+
const tocCtx = getTocCtx(data);
52+
6353
return (
6454
<>
6555
<main
@@ -107,17 +97,12 @@ export default function Doc(data: Lume.Data, helpers: Lume.Helpers) {
10797
<data.comp.Feedback file={file} />
10898
</div>
10999
</main>
110-
{(() => {
111-
const tocCtx = getTocCtx(data);
112-
return isReference && tocCtx
113-
? (
114-
<data.comp.RefToc
115-
documentNavigation={tocCtx.document_navigation}
116-
documentNavigationStr={tocCtx.document_navigation_str}
117-
/>
118-
)
119-
: null;
120-
})()}
100+
{isReference && tocCtx && (
101+
<data.comp.RefToc
102+
documentNavigation={tocCtx.document_navigation}
103+
documentNavigationStr={tocCtx.document_navigation_str}
104+
/>
105+
)}
121106
</>
122107
);
123108
}

0 commit comments

Comments
 (0)