Skip to content
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

sidebar option #696

Merged
merged 3 commits into from
Feb 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions docs/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,10 @@ In this case, the path to the stylesheet is resolved relative to the page’s Ma

The project’s title. If specified, this text is used for the link to the home page in the sidebar, and to complement the titles of the webpages. For instance, a page titled “Sales” in a project titled “ACME, Inc.” will display “Sales | ACME, Inc.” in the browser’s title bar. If not specified, the home page link will appear as “Home” in the sidebar, and page titles will be shown as-is.

## sidebar

Whether to show the sidebar. Defaults to true if **pages** is not empty.

## pages

An array containing pages and/or sections. If not specified, it defaults to all Markdown files found in the source root in directory listing order.
Expand Down
7 changes: 5 additions & 2 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ export interface Config {
output: string; // defaults to dist
base: string; // defaults to "/"
title?: string;
pages: (Page | Section)[]; // TODO rename to sidebar?
sidebar: boolean; // defaults to true if pages isn’t empty
pages: (Page | Section)[];
pager: boolean; // defaults to true
scripts: Script[]; // defaults to empty array
head: string; // defaults to empty string
Expand Down Expand Up @@ -89,6 +90,7 @@ export async function normalizeConfig(spec: any = {}, defaultRoot = "docs"): Pro
root = defaultRoot,
output = "dist",
base = "/",
sidebar,
style,
theme = "default",
deploy,
Expand All @@ -108,14 +110,15 @@ export async function normalizeConfig(spec: any = {}, defaultRoot = "docs"): Pro
let {title, pages = await readPages(root), pager = true, toc = true} = spec;
if (title !== undefined) title = String(title);
pages = Array.from(pages, normalizePageOrSection);
sidebar = sidebar === undefined ? pages.length > 0 : Boolean(sidebar);
pager = Boolean(pager);
scripts = Array.from(scripts, normalizeScript);
head = String(head);
header = String(header);
footer = String(footer);
toc = normalizeToc(toc);
deploy = deploy ? {workspace: String(deploy.workspace).replace(/^@+/, ""), project: String(deploy.project)} : null;
return {root, output, base, title, pages, pager, scripts, head, header, footer, toc, style, deploy};
return {root, output, base, title, sidebar, pages, pager, scripts, head, header, footer, toc, style, deploy};
}

function normalizeBase(base: any): string {
Expand Down
3 changes: 2 additions & 1 deletion src/render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ type RenderInternalOptions =

async function render(parseResult: ParseResult, options: RenderOptions & RenderInternalOptions): Promise<string> {
const {root, base, path, pages, title, preview} = options;
const sidebar = parseResult.data?.sidebar !== undefined ? Boolean(parseResult.data.sidebar) : options.sidebar;
const toc = mergeToc(parseResult.data?.toc, options.toc);
return String(html`<!DOCTYPE html>
<meta charset="utf-8">${path === "/404" ? html`\n<base href="${preview ? "/" : base}">` : ""}
Expand Down Expand Up @@ -89,7 +90,7 @@ import ${preview || parseResult.cells.length > 0 ? `{${preview ? "open, " : ""}d
${
preview ? `\nopen({hash: ${JSON.stringify(parseResult.hash)}, eval: (body) => (0, eval)(body)});\n` : ""
}${parseResult.cells.map((cell) => `\n${renderDefineCell(cell)}`).join("")}`)}
</script>${pages.length > 0 ? html`\n${await renderSidebar(title, pages, path)}` : ""}${
</script>${sidebar ? html`\n${await renderSidebar(title, pages, path)}` : ""}${
toc.show ? html`\n${renderToc(findHeaders(parseResult), toc.label)}` : ""
}
<div id="observablehq-center">${renderHeader(options, parseResult.data)}
Expand Down
2 changes: 2 additions & 0 deletions test/config-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ describe("readConfig(undefined, root)", () => {
output: "dist",
base: "/",
style: {theme: ["air", "near-midnight"]},
sidebar: true,
pages: [
{path: "/index", name: "Index"},
{path: "/one", name: "One<Two"},
Expand All @@ -37,6 +38,7 @@ describe("readConfig(undefined, root)", () => {
output: "dist",
base: "/",
style: {theme: ["air", "near-midnight"]},
sidebar: true,
pages: [{name: "Build test case", path: "/simple"}],
title: undefined,
toc: {label: "Contents", show: true},
Expand Down