This commit is contained in:
Zef Hemel
2024-01-25 14:51:40 +01:00
parent e508628826
commit 604bea3ee0
13 changed files with 129 additions and 9 deletions
+5
View File
@@ -7,6 +7,10 @@ Deno.test("Page utility functions", () => {
assertEquals(parsePageRef("[[foo]]"), { page: "foo" });
assertEquals(parsePageRef("foo@1"), { page: "foo", pos: 1 });
assertEquals(parsePageRef("foo$bar"), { page: "foo", anchor: "bar" });
assertEquals(parsePageRef("foo#My header"), {
page: "foo",
header: "My header",
});
assertEquals(parsePageRef("foo$bar@1"), {
page: "foo",
anchor: "bar",
@@ -21,4 +25,5 @@ Deno.test("Page utility functions", () => {
assertEquals(encodePageRef({ page: "foo" }), "foo");
assertEquals(encodePageRef({ page: "foo", pos: 10 }), "foo@10");
assertEquals(encodePageRef({ page: "foo", anchor: "bar" }), "foo$bar");
assertEquals(encodePageRef({ page: "foo", header: "bar" }), "foo#bar");
});
+10
View File
@@ -15,11 +15,13 @@ export type PageRef = {
page: string;
pos?: number;
anchor?: string;
header?: string;
};
const posRegex = /@(\d+)$/;
// Should be kept in sync with the regex in index.plug.yaml
const anchorRegex = /\$([a-zA-Z\.\-\/]+[\w\.\-\/]*)$/;
const headerRegex = /#([^#]*)$/;
export function parsePageRef(name: string): PageRef {
// Normalize the page name
@@ -37,6 +39,11 @@ export function parsePageRef(name: string): PageRef {
pageRef.anchor = anchorMatch[1];
pageRef.page = pageRef.page.replace(anchorRegex, "");
}
const headerMatch = pageRef.page.match(headerRegex);
if (headerMatch) {
pageRef.header = headerMatch[1];
pageRef.page = pageRef.page.replace(headerRegex, "");
}
return pageRef;
}
@@ -48,5 +55,8 @@ export function encodePageRef(pageRef: PageRef): string {
if (pageRef.anchor) {
name += `$${pageRef.anchor}`;
}
if (pageRef.header) {
name += `#${pageRef.header}`;
}
return name;
}