@@ -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");
|
||||
});
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user