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

normalize pages #1111

Merged
merged 2 commits into from Mar 19, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 8 additions & 2 deletions src/config.ts
Expand Up @@ -9,7 +9,7 @@ import {LoaderResolver} from "./dataloader.js";
import {visitMarkdownFiles} from "./files.js";
import {formatIsoDate, formatLocaleDate} from "./format.js";
import {createMarkdownIt, parseMarkdown} from "./markdown.js";
import {resolvePath} from "./path.js";
import {isAssetPath, parseRelativeUrl, resolvePath} from "./path.js";
import {resolveTheme} from "./theme.js";

export interface Page {
Expand Down Expand Up @@ -197,7 +197,13 @@ function normalizePage(spec: any): Page {
let {name, path} = spec;
name = String(name);
path = String(path);
if (path.endsWith("/")) path = `${path}index`;
if (isAssetPath(path)) {
const u = parseRelativeUrl(join("/", path)); // add leading slash
let {pathname} = u;
pathname = pathname.replace(/\.html$/i, ""); // remove trailing .html
pathname = pathname.replace(/\/$/, "/index"); // add trailing index
path = pathname + u.search + u.hash;
}
return {name, path};
}

Expand Down
18 changes: 3 additions & 15 deletions src/markdown.ts
Expand Up @@ -14,7 +14,7 @@ import {rewriteHtmlPaths} from "./html.js";
import {parseInfo} from "./info.js";
import type {JavaScriptNode} from "./javascript/parse.js";
import {parseJavaScript} from "./javascript/parse.js";
import {relativePath} from "./path.js";
import {isAssetPath, parseRelativeUrl, relativePath} from "./path.js";
import {transpileSql} from "./sql.js";
import {transpileTag} from "./tag.js";
import {InvalidThemeError} from "./theme.js";
Expand Down Expand Up @@ -280,22 +280,10 @@ function makeSoftbreakRenderer(baseRenderer: RenderRule): RenderRule {
};
}

export function parseRelativeUrl(url: string): {pathname: string; search: string; hash: string} {
let search: string;
let hash: string;
const i = url.indexOf("#");
if (i < 0) hash = "";
else (hash = url.slice(i)), (url = url.slice(0, i));
const j = url.indexOf("?");
if (j < 0) search = "";
else (search = url.slice(j)), (url = url.slice(0, j));
return {pathname: url, search, hash};
}

export function makeLinkNormalizer(baseNormalize: (url: string) => string, clean: boolean): (url: string) => string {
return (url) => {
// Only clean relative links; ignore e.g. "https:" links.
if (!/^\w+:/.test(url)) {
// Only clean local links (and ignore e.g. "https:" links).
if (isAssetPath(url)) {
const u = parseRelativeUrl(url);
let {pathname} = u;
if (pathname && !pathname.endsWith("/") && !extname(pathname)) pathname += ".html";
Expand Down
12 changes: 12 additions & 0 deletions src/path.ts
Expand Up @@ -72,3 +72,15 @@ export function isAssetPath(specifier: string): boolean {
export function resolveRelativePath(source: string, target: string): string {
return relativePath(source, resolvePath(source, target));
}

export function parseRelativeUrl(url: string): {pathname: string; search: string; hash: string} {
let search: string;
let hash: string;
const i = url.indexOf("#");
if (i < 0) hash = "";
else (hash = url.slice(i)), (url = url.slice(0, i));
const j = url.indexOf("?");
if (j < 0) search = "";
else (search = url.slice(j)), (url = url.slice(0, j));
return {pathname: url, search, hash};
}
47 changes: 42 additions & 5 deletions test/config-test.ts
Expand Up @@ -83,20 +83,57 @@ describe("normalizeConfig(spec, root)", () => {
it("coerces pages to an array", async () => {
assert.deepStrictEqual((await config({pages: new Set()}, root)).pages, []);
});
it("coerces pages", async () => {
it("coerces and normalizes page paths", async () => {
const inpages = [
{name: 42, path: true},
{name: null, path: {toString: () => "yes"}}
{name: null, path: {toString: () => "yes"}},
{name: "Index", path: "/foo/index"},
{name: "Index.html", path: "/foo/index.html"},
{name: "Page.html", path: "/foo.html"}
];
const outpages = [
{name: "42", path: "true"},
{name: "null", path: "yes"}
{name: "42", path: "/true"},
{name: "null", path: "/yes"},
{name: "Index", path: "/foo/index"},
{name: "Index.html", path: "/foo/index"},
{name: "Page.html", path: "/foo"}
];
assert.deepStrictEqual((await config({pages: inpages}, root)).pages, outpages);
});
it("allows external page paths", async () => {
const pages = [{name: "Example.com", path: "https://example.com"}];
assert.deepStrictEqual((await config({pages}, root)).pages, pages);
});
it("allows page paths to have query strings and anchor fragments", async () => {
const inpages = [
{name: "Anchor fragment on index", path: "/test/index#foo=bar"},
{name: "Anchor fragment on index.html", path: "/test/index.html#foo=bar"},
{name: "Anchor fragment on page.html", path: "/test.html#foo=bar"},
{name: "Anchor fragment on slash", path: "/test/#foo=bar"},
{name: "Anchor fragment", path: "/test#foo=bar"},
{name: "Query string on index", path: "/test/index?foo=bar"},
{name: "Query string on index.html", path: "/test/index.html?foo=bar"},
{name: "Query string on page.html", path: "/test.html?foo=bar"},
{name: "Query string on slash", path: "/test/?foo=bar"},
{name: "Query string", path: "/test?foo=bar"}
];
const outpages = [
{name: "Anchor fragment on index", path: "/test/index#foo=bar"},
{name: "Anchor fragment on index.html", path: "/test/index#foo=bar"},
{name: "Anchor fragment on page.html", path: "/test#foo=bar"},
{name: "Anchor fragment on slash", path: "/test/index#foo=bar"},
{name: "Anchor fragment", path: "/test#foo=bar"},
{name: "Query string on index", path: "/test/index?foo=bar"},
{name: "Query string on index.html", path: "/test/index?foo=bar"},
{name: "Query string on page.html", path: "/test?foo=bar"},
{name: "Query string on slash", path: "/test/index?foo=bar"},
{name: "Query string", path: "/test?foo=bar"}
];
assert.deepStrictEqual((await config({pages: inpages}, root)).pages, outpages);
});
it("coerces sections", async () => {
const inpages = [{name: 42, pages: new Set([{name: null, path: {toString: () => "yes"}}])}];
const outpages = [{name: "42", open: true, pages: [{name: "null", path: "yes"}]}];
const outpages = [{name: "42", open: true, pages: [{name: "null", path: "/yes"}]}];
assert.deepStrictEqual((await config({pages: inpages}, root)).pages, outpages);
});
it("coerces toc", async () => {
Expand Down
23 changes: 1 addition & 22 deletions test/markdown-test.ts
Expand Up @@ -6,7 +6,7 @@ import deepEqual from "fast-deep-equal";
import {normalizeConfig} from "../src/config.js";
import {isEnoent} from "../src/error.js";
import type {MarkdownPage} from "../src/markdown.js";
import {makeLinkNormalizer, parseMarkdown, parseRelativeUrl} from "../src/markdown.js";
import {makeLinkNormalizer, parseMarkdown} from "../src/markdown.js";

const {md} = await normalizeConfig();

Expand Down Expand Up @@ -63,27 +63,6 @@ describe("parseMarkdown(input)", () => {
}
});

describe("parseRelativeUrl(url)", () => {
it("handles paths", () => {
assert.deepStrictEqual(parseRelativeUrl("foo"), {pathname: "foo", search: "", hash: ""});
assert.deepStrictEqual(parseRelativeUrl("foo.html"), {pathname: "foo.html", search: "", hash: ""});
assert.deepStrictEqual(parseRelativeUrl("../foo"), {pathname: "../foo", search: "", hash: ""});
assert.deepStrictEqual(parseRelativeUrl("./foo"), {pathname: "./foo", search: "", hash: ""});
assert.deepStrictEqual(parseRelativeUrl("/foo"), {pathname: "/foo", search: "", hash: ""});
assert.deepStrictEqual(parseRelativeUrl("/foo%3Fbar"), {pathname: "/foo%3Fbar", search: "", hash: ""});
});
it("handles queries", () => {
assert.deepStrictEqual(parseRelativeUrl("foo?bar"), {pathname: "foo", search: "?bar", hash: ""});
});
it("handles hashes", () => {
assert.deepStrictEqual(parseRelativeUrl("foo#bar"), {pathname: "foo", search: "", hash: "#bar"});
assert.deepStrictEqual(parseRelativeUrl("foo#bar?baz"), {pathname: "foo", search: "", hash: "#bar?baz"});
});
it("handles queries and hashes", () => {
assert.deepStrictEqual(parseRelativeUrl("foo?bar#baz"), {pathname: "foo", search: "?bar", hash: "#baz"});
});
});

describe("makeLinkNormalizer(normalize, false)", () => {
const normalize = makeLinkNormalizer(String, false);
it("appends .html to extension-less links", () => {
Expand Down
23 changes: 22 additions & 1 deletion test/path-test.ts
@@ -1,5 +1,5 @@
import assert from "node:assert";
import {isPathImport, relativePath, resolveLocalPath, resolvePath} from "../src/path.js";
import {isPathImport, parseRelativeUrl, relativePath, resolveLocalPath, resolvePath} from "../src/path.js";

describe("resolvePath(source, target)", () => {
it("returns the path to the specified target within the source root", () => {
Expand Down Expand Up @@ -127,3 +127,24 @@ describe("isPathImport(specifier)", () => {
assert.strictEqual(isPathImport("foo://bar"), false);
});
});

describe("parseRelativeUrl(url)", () => {
it("handles paths", () => {
assert.deepStrictEqual(parseRelativeUrl("foo"), {pathname: "foo", search: "", hash: ""});
assert.deepStrictEqual(parseRelativeUrl("foo.html"), {pathname: "foo.html", search: "", hash: ""});
assert.deepStrictEqual(parseRelativeUrl("../foo"), {pathname: "../foo", search: "", hash: ""});
assert.deepStrictEqual(parseRelativeUrl("./foo"), {pathname: "./foo", search: "", hash: ""});
assert.deepStrictEqual(parseRelativeUrl("/foo"), {pathname: "/foo", search: "", hash: ""});
assert.deepStrictEqual(parseRelativeUrl("/foo%3Fbar"), {pathname: "/foo%3Fbar", search: "", hash: ""});
});
it("handles queries", () => {
assert.deepStrictEqual(parseRelativeUrl("foo?bar"), {pathname: "foo", search: "?bar", hash: ""});
});
it("handles hashes", () => {
assert.deepStrictEqual(parseRelativeUrl("foo#bar"), {pathname: "foo", search: "", hash: "#bar"});
assert.deepStrictEqual(parseRelativeUrl("foo#bar?baz"), {pathname: "foo", search: "", hash: "#bar?baz"});
});
it("handles queries and hashes", () => {
assert.deepStrictEqual(parseRelativeUrl("foo?bar#baz"), {pathname: "foo", search: "?bar", hash: "#baz"});
});
});