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

base option #747

Merged
merged 2 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 @@ -148,6 +148,10 @@ An HTML fragment to add to the header. Defaults to the empty string.

An HTML fragment to add to the footer. Defaults to “Built with Observable.”

## base

The base path when serving the site. Currently this only affects the custom 404 page, if any.

## toc

The table of contents configuration.
Expand Down
1 change: 1 addition & 0 deletions observablehq.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ export default {
},
{name: "Contributing", path: "/contributing"}
],
base: "/framework",
scripts: [{type: "module", async: true, src: "analytics.js"}],
head: `<link rel="apple-touch-icon" href="https://static.observablehq.com/favicon-512.0667824687f99c942a02e06e2db1a060911da0bf3606671676a255b1cf97b4fe.png">
<link rel="icon" type="image/png" href="https://static.observablehq.com/favicon-512.0667824687f99c942a02e06e2db1a060911da0bf3606671676a255b1cf97b4fe.png" sizes="512x512">
Expand Down
12 changes: 11 additions & 1 deletion src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export interface Script {
export interface Config {
root: string; // defaults to docs
output: string; // defaults to dist
base: string; // defaults to "/"
title?: string;
pages: (Page | Section)[]; // TODO rename to sidebar?
pager: boolean; // defaults to true
Expand Down Expand Up @@ -87,6 +88,7 @@ export async function normalizeConfig(spec: any = {}, defaultRoot = "docs"): Pro
let {
root = defaultRoot,
output = "dist",
base = "/",
style,
theme = "default",
deploy,
Expand All @@ -99,6 +101,7 @@ export async function normalizeConfig(spec: any = {}, defaultRoot = "docs"): Pro
} = spec;
root = String(root);
output = String(output);
base = normalizeBase(base);
if (style === null) style = null;
else if (style !== undefined) style = {path: String(style)};
else style = {theme: (theme = normalizeTheme(theme))};
Expand All @@ -112,7 +115,14 @@ export async function normalizeConfig(spec: any = {}, defaultRoot = "docs"): Pro
footer = String(footer);
toc = normalizeToc(toc);
deploy = deploy ? {workspace: String(deploy.workspace).replace(/^@+/, ""), project: String(deploy.project)} : null;
return {root, output, title, pages, pager, scripts, head, header, footer, toc, style, deploy};
return {root, output, base, title, pages, pager, scripts, head, header, footer, toc, style, deploy};
}

function normalizeBase(base: any): string {
base = String(base);
if (!base.startsWith("/")) throw new Error(`base must start with slash: ${base}`);
if (!base.endsWith("/")) base += "/";
return base;
}

function normalizeTheme(spec: any): string[] {
Expand Down
4 changes: 2 additions & 2 deletions src/render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,10 @@ type RenderInternalOptions =
| {preview: true}; // preview

async function render(parseResult: ParseResult, options: RenderOptions & RenderInternalOptions): Promise<string> {
const {root, path, pages, title, preview} = options;
const {root, base, path, pages, title, preview} = options;
const toc = mergeToc(parseResult.data?.toc, options.toc);
return String(html`<!DOCTYPE html>
<meta charset="utf-8">${path === "/404" ? html`\n<base href="/">` : ""}
<meta charset="utf-8">${path === "/404" ? html`\n<base href="${preview ? "/" : base}">` : ""}
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
${
parseResult.title || title
Expand Down
2 changes: 2 additions & 0 deletions test/config-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ describe("readConfig(undefined, root)", () => {
assert.deepStrictEqual(await readConfig(undefined, "test/input/build/config"), {
root: "test/input/build/config",
output: "dist",
base: "/",
style: {theme: ["air", "near-midnight"]},
pages: [
{path: "/index", name: "Index"},
Expand All @@ -34,6 +35,7 @@ describe("readConfig(undefined, root)", () => {
assert.deepStrictEqual(await readConfig(undefined, "test/input/build/simple"), {
root: "test/input/build/simple",
output: "dist",
base: "/",
style: {theme: ["air", "near-midnight"]},
pages: [{name: "Build test case", path: "/simple"}],
title: undefined,
Expand Down