From 7e39bcfc6c5f8f43ca97da1b629bd38f752fb548 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A1=D0=B5=D0=BC=D1=91=D0=BD=20=D0=9D=D0=BE=D0=B2=D0=B8?= =?UTF-8?q?=D0=BA=D0=BE=D0=B2?= Date: Fri, 12 Jul 2024 23:38:45 +0500 Subject: [PATCH] Quick and dirty fix for the ".conflicted." pages navigation issue This will allow user to Ctrl-K to conflicted page, but refreshing the page still gives 404. --- plug-api/lib/page_ref.test.ts | 1 + plug-api/lib/page_ref.ts | 23 ++++++++++++++++++++++- 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/plug-api/lib/page_ref.test.ts b/plug-api/lib/page_ref.test.ts index 49995d31..1015618e 100644 --- a/plug-api/lib/page_ref.test.ts +++ b/plug-api/lib/page_ref.test.ts @@ -31,6 +31,7 @@ Deno.test("Page utility functions", () => { try { validatePageName("perfectly fine page name"); + validatePageName("this is special case of a.conflicted.page") } catch (error) { throw new AssertionError(`Something is very wrong with the validatePageName function: ${error}`); } diff --git a/plug-api/lib/page_ref.ts b/plug-api/lib/page_ref.ts index ab70af1f..a5daac85 100644 --- a/plug-api/lib/page_ref.ts +++ b/plug-api/lib/page_ref.ts @@ -1,3 +1,24 @@ + +/** + * Possible HACK. + * + * Normally we don't want to allow pages have dots in their names, + * because we need to serve files with extensions. But there are special cases like + * "conflicted" pages. Maybe there will be some more special cases in future. This + * is why this function exists. + * + */ +export function isThisPageSpecial(name: string): boolean { + const namePatterns: RegExp[] = [ + /\.conflicted\./ + ]; + return namePatterns.some(expr => expr.test(name)); +} + +export function looksLikeFileName(name: string): boolean { + return /\.[a-zA-Z0-9]+$/.test(name) && !isThisPageSpecial(name) +} + export function validatePageName(name: string) { // Page can not be empty and not end with a file extension (e.g. "bla.md") if (name === "") { @@ -6,7 +27,7 @@ export function validatePageName(name: string) { if (name.startsWith(".")) { throw new Error("Page name cannot start with a '.'"); } - if (/\.[a-zA-Z0-9]+$/.test(name)) { + if (looksLikeFileName(name)) { throw new Error("Page name can not end with a file extension"); } }