| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177 |
- import nodePath from 'path';
- import escapeStringRegexp from 'escape-string-regexp';
- /**
- * Whether path is the top page
- * @param path
- */
- export const isTopPage = (path: string): boolean => {
- return path === '/';
- };
- /**
- * Whether path belongs to the trash page
- * @param path
- */
- export const isTrashPage = (path: string): boolean => {
- // https://regex101.com/r/BSDdRr/1
- if (path.match(/^\/trash(\/.*)?$/)) {
- return true;
- }
- return false;
- };
- /**
- * Whether path belongs to the user page
- * @param path
- */
- export const isUserPage = (path: string): boolean => {
- // https://regex101.com/r/SxPejV/1
- if (path.match(/^\/user(\/.*)?$/)) {
- return true;
- }
- return false;
- };
- /**
- * Whether path is right under the path '/user'
- * @param path
- */
- export const isUserNamePage = (path: string): boolean => {
- // https://regex101.com/r/GUZntH/1
- if (path.match(/^\/user\/[^/]+$/)) {
- return true;
- }
- return false;
- };
- /**
- * Whether path belongs to the shared page
- * @param path
- */
- export const isSharedPage = (path: string): boolean => {
- // https://regex101.com/r/ZjdOiB/1
- if (path.match(/^\/share(\/.*)?$/)) {
- return true;
- }
- return false;
- };
- const restrictedPatternsToDelete: Array<RegExp> = [
- /^\/user\/[^/]+$/, // user page
- ];
- export const isDeletablePage = (path: string): boolean => {
- return !restrictedPatternsToDelete.some(pattern => path.match(pattern));
- };
- const restrictedPatternsToCreate: Array<RegExp> = [
- /\^|\$|\*|\+|#|%|\?/,
- /^\/-\/.*/,
- /^\/_r\/.*/,
- /^\/_apix?(\/.*)?/,
- /^\/?https?:\/\/.+$/, // avoid miss in renaming
- /\/{2,}/, // avoid miss in renaming
- /\s+\/\s+/, // avoid miss in renaming
- /.+\/edit$/,
- /.+\.md$/,
- /^(\.\.)$/, // see: https://github.com/weseek/growi/issues/3582
- /(\/\.\.)\/?/, // see: https://github.com/weseek/growi/issues/3582
- /^\/(installer|register|login|logout|admin|me|files|trash|paste|comments|tags|share)(\/.*|$)/,
- ];
- export const isCreatablePage = (path: string): boolean => {
- return !restrictedPatternsToCreate.some(pattern => path.match(pattern));
- };
- /**
- * return user path
- * @param user
- */
- // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
- export const userPageRoot = (user: any): string => {
- if (!user || !user.username) {
- return '';
- }
- return `/user/${user.username}`;
- };
- /**
- * return user path
- * @param parentPath
- * @param childPath
- * @param newPath
- */
- export const convertToNewAffiliationPath = (oldPath: string, newPath: string, childPath: string): string => {
- if (newPath === null) {
- throw new Error('Please input the new page path');
- }
- const pathRegExp = new RegExp(`^${escapeStringRegexp(oldPath)}`, 'i');
- return childPath.replace(pathRegExp, newPath);
- };
- /**
- * Encode SPACE and IDEOGRAPHIC SPACE
- * @param {string} path
- * @returns {string}
- */
- export const encodeSpaces = (path?:string): string | undefined => {
- if (path == null) {
- return undefined;
- }
- // Encode SPACE and IDEOGRAPHIC SPACE
- return path.replace(/ /g, '%20').replace(/\u3000/g, '%E3%80%80');
- };
- /**
- * Generate editor path
- * @param {string} paths
- * @returns {string}
- */
- export const generateEditorPath = (...paths: string[]): string => {
- const joinedPath = [...paths].join('/');
- if (!isCreatablePage(joinedPath)) {
- throw new Error('Invalid characters on path');
- }
- try {
- const url = new URL(joinedPath, 'https://dummy');
- return `${url.pathname}#edit`;
- }
- catch (err) {
- throw new Error('Invalid path format');
- }
- };
- /**
- * returns ancestors paths
- * @param {string} path
- * @param {string[]} ancestorPaths
- * @returns {string[]}
- */
- export const collectAncestorPaths = (path: string, ancestorPaths: string[] = []): string[] => {
- if (isTopPage(path)) return ancestorPaths;
- const parentPath = nodePath.dirname(path);
- ancestorPaths.push(parentPath);
- return collectAncestorPaths(parentPath, ancestorPaths);
- };
- /**
- * return paths without duplicate area of regexp /^${path}\/.+/i
- * ex. expect(omitDuplicateAreaPathFromPaths(['/A', '/A/B', '/A/B/C'])).toStrictEqual(['/A'])
- * @param paths paths to be tested
- * @returns omitted paths
- */
- export const omitDuplicateAreaPathFromPaths = (paths: string[]): string[] => {
- return paths.filter((path) => {
- const isDuplicate = paths.filter(p => (new RegExp(`^${p}\\/.+`, 'i')).test(path)).length > 0;
- return !isDuplicate;
- });
- };
|