Files
outline/app/utils/routeHelpers.ts
Tom Moor 15b1069bcc chore: Move to Typescript (#2783)
This PR moves the entire project to Typescript. Due to the ~1000 ignores this will lead to a messy codebase for a while, but the churn is worth it – all of those ignore comments are places that were never type-safe previously.

closes #1282
2021-11-29 06:40:55 -08:00

113 lines
2.4 KiB
TypeScript

import queryString from "query-string";
import Collection from "~/models/Collection";
import Document from "~/models/Document";
export function homePath(): string {
return "/home";
}
export function draftsPath(): string {
return "/drafts";
}
export function templatesPath(): string {
return "/templates";
}
export function settingsPath(): string {
return "/settings";
}
export function archivePath(): string {
return "/archive";
}
export function trashPath(): string {
return "/trash";
}
export function groupSettingsPath(): string {
return "/settings/groups";
}
export function collectionUrl(url: string, section?: string): string {
if (section) return `${url}/${section}`;
return url;
}
export function updateCollectionUrl(
oldUrl: string,
collection: Collection
): string {
// Update url to match the current one
return oldUrl.replace(
new RegExp("/collection/[0-9a-zA-Z-_~]*"),
collection.url
);
}
export function documentUrl(doc: Document): string {
return doc.url;
}
export function editDocumentUrl(doc: Document): string {
return `${doc.url}/edit`;
}
export function documentMoveUrl(doc: Document): string {
return `${doc.url}/move`;
}
export function documentHistoryUrl(doc: Document, revisionId?: string): string {
let base = `${doc.url}/history`;
if (revisionId) base += `/${revisionId}`;
return base;
}
/**
* Replace full url's document part with the new one in case
* the document slug has been updated
*/
export function updateDocumentUrl(oldUrl: string, document: Document): string {
// Update url to match the current one
return oldUrl.replace(new RegExp("/doc/[0-9a-zA-Z-_~]*"), document.url);
}
export function newDocumentPath(
collectionId: string,
params: {
parentDocumentId?: string;
templateId?: string;
template?: boolean;
} = {}
): string {
return `/collection/${collectionId}/new?${queryString.stringify(params)}`;
}
export function searchUrl(
query?: string,
params: {
collectionId?: string;
ref?: string;
} = {}
): string {
let search = queryString.stringify(params);
let route = "/search";
if (query) {
route += `/${encodeURIComponent(query.replace(/%/g, "%25"))}`;
}
search = search ? `?${search}` : "";
return `${route}${search}`;
}
export function notFoundUrl(): string {
return "/404";
}
export const matchDocumentSlug =
":documentSlug([0-9a-zA-Z-_~]*-[a-zA-z0-9]{10,15})";
export const matchDocumentEdit = `/doc/${matchDocumentSlug}/edit`;