Files
racket-wiki/static/js/wiki/routes.js
T
2026-09-02 08:38:03 +02:00

72 lines
2.3 KiB
JavaScript

/*
* Client-side wiki routes.
*
* This module owns page/CMap hash construction and all hash recognition. It
* returns plain route records and has no knowledge of sessions, permissions,
* application state or DOM views.
*/
/** Build the client-side route for a wiki page reference. */
function pageRoute(reference) {
return `#/${encodeURIComponent(reference)}`;
}
/** Build the client-side route for a concept map. */
function cmapRoute(slug) {
return `#cmap/${encodeURIComponent(slug)}`;
}
/**
* goal : Recognize one browser hash as a wiki application route.
* pre : hash is the current location hash or another string to inspect.
* post : No browser or application state is changed.
* result : A named route record; unknown and empty hashes produce home.
* internals: Exact special views are recognized first. Parameterized Todo,
* CMap, search and page routes are then decoded into their values.
*/
function parseWikiRoute(hash) {
const value = String(hash || "");
switch (value) {
case "#profile": return { name: "profile" };
case "#cmaps": return { name: "cmaps" };
case "#recent": return { name: "recent" };
case "#bookmarks": return { name: "bookmarks" };
case "#todos": return { name: "todos" };
case "#admin": return { name: "admin" };
case "#admin/users": return { name: "admin-users" };
case "#admin/mail": return { name: "admin-mail" };
case "#admin/aliases": return { name: "admin-aliases" };
case "#admin/orphaned-uploads": return { name: "admin-orphaned-uploads" };
case "#admin/archived-cmaps": return { name: "admin-archived-cmaps" };
}
const todoMatch = value.match(/^#todo\/([^/]+)\/(\d+)$/);
if (todoMatch) {
return {
name: "todo",
slug: decodeURIComponent(todoMatch[1]),
number: Number(todoMatch[2])
};
}
const cmapMatch = value.match(/^#cmap\/([^/]+)$/);
if (cmapMatch) {
return { name: "cmap", slug: decodeURIComponent(cmapMatch[1]) };
}
const searchMatch = value.match(/^#search\/(.+)$/);
if (searchMatch) {
return { name: "search", query: decodeURIComponent(searchMatch[1]) };
}
const pageMatch = value.match(/^#\/([^/]+)$/);
if (pageMatch) {
return { name: "page", slug: decodeURIComponent(pageMatch[1]) };
}
return { name: "home" };
}
export { cmapRoute, pageRoute, parseWikiRoute };