2024-07-13 19:51:49 +08:00
|
|
|
/**
|
|
|
|
* Checks if a name looks like a full path (with a file extension), but is not a conflicted file.
|
2024-07-13 02:38:45 +08:00
|
|
|
*/
|
2024-07-13 19:51:49 +08:00
|
|
|
export function looksLikePathWithExtension(name: string): boolean {
|
|
|
|
return /\.[a-zA-Z0-9]+$/.test(name) && !/\.conflicted\./.test(name);
|
2024-07-13 02:38:45 +08:00
|
|
|
}
|
|
|
|
|
2023-07-28 21:20:56 +08:00
|
|
|
export function validatePageName(name: string) {
|
2023-07-06 22:47:50 +08:00
|
|
|
// Page can not be empty and not end with a file extension (e.g. "bla.md")
|
2023-07-28 21:20:56 +08:00
|
|
|
if (name === "") {
|
|
|
|
throw new Error("Page name can not be empty");
|
|
|
|
}
|
|
|
|
if (name.startsWith(".")) {
|
|
|
|
throw new Error("Page name cannot start with a '.'");
|
|
|
|
}
|
2024-07-13 19:51:49 +08:00
|
|
|
if (looksLikePathWithExtension(name)) {
|
2023-07-28 21:20:56 +08:00
|
|
|
throw new Error("Page name can not end with a file extension");
|
|
|
|
}
|
2023-07-06 22:47:50 +08:00
|
|
|
}
|
2024-01-24 18:58:33 +08:00
|
|
|
|
|
|
|
export type PageRef = {
|
|
|
|
page: string;
|
|
|
|
pos?: number;
|
|
|
|
anchor?: string;
|
2024-01-25 21:51:40 +08:00
|
|
|
header?: string;
|
2024-07-17 23:03:25 +08:00
|
|
|
meta?: boolean;
|
2024-01-24 18:58:33 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
const posRegex = /@(\d+)$/;
|
|
|
|
// Should be kept in sync with the regex in index.plug.yaml
|
|
|
|
const anchorRegex = /\$([a-zA-Z\.\-\/]+[\w\.\-\/]*)$/;
|
2024-01-25 21:51:40 +08:00
|
|
|
const headerRegex = /#([^#]*)$/;
|
2024-01-24 18:58:33 +08:00
|
|
|
|
|
|
|
export function parsePageRef(name: string): PageRef {
|
|
|
|
// Normalize the page name
|
|
|
|
if (name.startsWith("[[") && name.endsWith("]]")) {
|
|
|
|
name = name.slice(2, -2);
|
|
|
|
}
|
|
|
|
const pageRef: PageRef = { page: name };
|
2024-07-17 23:03:25 +08:00
|
|
|
if (pageRef.page.startsWith("^")) {
|
|
|
|
// A carrot prefix means we're looking for a meta page, but that doesn't matter for most use cases
|
|
|
|
pageRef.page = pageRef.page.slice(1);
|
|
|
|
pageRef.meta = true;
|
|
|
|
}
|
2024-01-24 18:58:33 +08:00
|
|
|
const posMatch = pageRef.page.match(posRegex);
|
|
|
|
if (posMatch) {
|
|
|
|
pageRef.pos = parseInt(posMatch[1]);
|
|
|
|
pageRef.page = pageRef.page.replace(posRegex, "");
|
|
|
|
}
|
|
|
|
const anchorMatch = pageRef.page.match(anchorRegex);
|
|
|
|
if (anchorMatch) {
|
|
|
|
pageRef.anchor = anchorMatch[1];
|
|
|
|
pageRef.page = pageRef.page.replace(anchorRegex, "");
|
|
|
|
}
|
2024-01-25 21:51:40 +08:00
|
|
|
const headerMatch = pageRef.page.match(headerRegex);
|
|
|
|
if (headerMatch) {
|
|
|
|
pageRef.header = headerMatch[1];
|
|
|
|
pageRef.page = pageRef.page.replace(headerRegex, "");
|
|
|
|
}
|
2024-01-24 18:58:33 +08:00
|
|
|
return pageRef;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function encodePageRef(pageRef: PageRef): string {
|
|
|
|
let name = pageRef.page;
|
|
|
|
if (pageRef.pos) {
|
|
|
|
name += `@${pageRef.pos}`;
|
|
|
|
}
|
|
|
|
if (pageRef.anchor) {
|
|
|
|
name += `$${pageRef.anchor}`;
|
|
|
|
}
|
2024-01-25 21:51:40 +08:00
|
|
|
if (pageRef.header) {
|
|
|
|
name += `#${pageRef.header}`;
|
|
|
|
}
|
2024-01-24 18:58:33 +08:00
|
|
|
return name;
|
|
|
|
}
|