Skip to content

Commit 49a3c84

Browse files
authored
base option (#747)
* base option * adopt base
1 parent dc6d5de commit 49a3c84

File tree

5 files changed

+20
-3
lines changed

5 files changed

+20
-3
lines changed

docs/config.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,10 @@ An HTML fragment to add to the header. Defaults to the empty string.
148148

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

151+
## base
152+
153+
The base path when serving the site. Currently this only affects the custom 404 page, if any.
154+
151155
## toc
152156

153157
The table of contents configuration.

observablehq.config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ export default {
8383
},
8484
{name: "Contributing", path: "/contributing"}
8585
],
86+
base: "/framework",
8687
scripts: [{type: "module", async: true, src: "analytics.js"}],
8788
head: `<link rel="apple-touch-icon" href="https://static.observablehq.com/favicon-512.0667824687f99c942a02e06e2db1a060911da0bf3606671676a255b1cf97b4fe.png">
8889
<link rel="icon" type="image/png" href="https://static.observablehq.com/favicon-512.0667824687f99c942a02e06e2db1a060911da0bf3606671676a255b1cf97b4fe.png" sizes="512x512">

src/config.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ export interface Script {
3434
export interface Config {
3535
root: string; // defaults to docs
3636
output: string; // defaults to dist
37+
base: string; // defaults to "/"
3738
title?: string;
3839
pages: (Page | Section)[]; // TODO rename to sidebar?
3940
pager: boolean; // defaults to true
@@ -87,6 +88,7 @@ export async function normalizeConfig(spec: any = {}, defaultRoot = "docs"): Pro
8788
let {
8889
root = defaultRoot,
8990
output = "dist",
91+
base = "/",
9092
style,
9193
theme = "default",
9294
deploy,
@@ -99,6 +101,7 @@ export async function normalizeConfig(spec: any = {}, defaultRoot = "docs"): Pro
99101
} = spec;
100102
root = String(root);
101103
output = String(output);
104+
base = normalizeBase(base);
102105
if (style === null) style = null;
103106
else if (style !== undefined) style = {path: String(style)};
104107
else style = {theme: (theme = normalizeTheme(theme))};
@@ -112,7 +115,14 @@ export async function normalizeConfig(spec: any = {}, defaultRoot = "docs"): Pro
112115
footer = String(footer);
113116
toc = normalizeToc(toc);
114117
deploy = deploy ? {workspace: String(deploy.workspace).replace(/^@+/, ""), project: String(deploy.project)} : null;
115-
return {root, output, title, pages, pager, scripts, head, header, footer, toc, style, deploy};
118+
return {root, output, base, title, pages, pager, scripts, head, header, footer, toc, style, deploy};
119+
}
120+
121+
function normalizeBase(base: any): string {
122+
base = String(base);
123+
if (!base.startsWith("/")) throw new Error(`base must start with slash: ${base}`);
124+
if (!base.endsWith("/")) base += "/";
125+
return base;
116126
}
117127

118128
function normalizeTheme(spec: any): string[] {

src/render.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,10 @@ type RenderInternalOptions =
5858
| {preview: true}; // preview
5959

6060
async function render(parseResult: ParseResult, options: RenderOptions & RenderInternalOptions): Promise<string> {
61-
const {root, path, pages, title, preview} = options;
61+
const {root, base, path, pages, title, preview} = options;
6262
const toc = mergeToc(parseResult.data?.toc, options.toc);
6363
return String(html`<!DOCTYPE html>
64-
<meta charset="utf-8">${path === "/404" ? html`\n<base href="/">` : ""}
64+
<meta charset="utf-8">${path === "/404" ? html`\n<base href="${preview ? "/" : base}">` : ""}
6565
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
6666
${
6767
parseResult.title || title

test/config-test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ describe("readConfig(undefined, root)", () => {
99
assert.deepStrictEqual(await readConfig(undefined, "test/input/build/config"), {
1010
root: "test/input/build/config",
1111
output: "dist",
12+
base: "/",
1213
style: {theme: ["air", "near-midnight"]},
1314
pages: [
1415
{path: "/index", name: "Index"},
@@ -34,6 +35,7 @@ describe("readConfig(undefined, root)", () => {
3435
assert.deepStrictEqual(await readConfig(undefined, "test/input/build/simple"), {
3536
root: "test/input/build/simple",
3637
output: "dist",
38+
base: "/",
3739
style: {theme: ["air", "near-midnight"]},
3840
pages: [{name: "Build test case", path: "/simple"}],
3941
title: undefined,

0 commit comments

Comments
 (0)