page-path-utils.ts 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. import nodePath from 'path';
  2. import escapeStringRegexp from 'escape-string-regexp';
  3. import { addTrailingSlash } from './path-utils';
  4. /**
  5. * Whether path is the top page
  6. * @param path
  7. */
  8. export const isTopPage = (path: string): boolean => {
  9. return path === '/';
  10. };
  11. /**
  12. * Whether path is the top page of users
  13. * @param path
  14. */
  15. export const isUsersTopPage = (path: string): boolean => {
  16. return path === '/user';
  17. };
  18. /**
  19. * Whether path is user's home page
  20. * @param path
  21. */
  22. export const isUsersHomePage = (path: string): boolean => {
  23. // https://regex101.com/r/utVQct/1
  24. if (path.match(/^\/user\/[^/]+$/)) {
  25. return true;
  26. }
  27. return false;
  28. };
  29. /**
  30. * Whether path is the protected pages for systems
  31. * @param path
  32. */
  33. export const isUsersProtectedPages = (path: string): boolean => {
  34. return isUsersTopPage(path) || isUsersHomePage(path);
  35. };
  36. /**
  37. * Whether path is movable
  38. * @param path
  39. */
  40. export const isMovablePage = (path: string): boolean => {
  41. return !isTopPage(path) && !isUsersProtectedPages(path);
  42. };
  43. /**
  44. * Whether path belongs to the user page
  45. * @param path
  46. */
  47. export const isUserPage = (path: string): boolean => {
  48. // https://regex101.com/r/BSDdRr/1
  49. if (path.match(/^\/user(\/.*)?$/)) {
  50. return true;
  51. }
  52. return false;
  53. };
  54. /**
  55. * Whether path belongs to the trash page
  56. * @param path
  57. */
  58. export const isTrashPage = (path: string): boolean => {
  59. // https://regex101.com/r/BSDdRr/1
  60. if (path.match(/^\/trash(\/.*)?$/)) {
  61. return true;
  62. }
  63. return false;
  64. };
  65. /**
  66. * Whether path belongs to the shared page
  67. * @param path
  68. */
  69. export const isSharedPage = (path: string): boolean => {
  70. // https://regex101.com/r/ZjdOiB/1
  71. if (path.match(/^\/share(\/.*)?$/)) {
  72. return true;
  73. }
  74. return false;
  75. };
  76. const restrictedPatternsToCreate: Array<RegExp> = [
  77. /\^|\$|\*|\+|#|%|\?/,
  78. /^\/-\/.*/,
  79. /^\/_r\/.*/,
  80. /^\/_apix?(\/.*)?/,
  81. /^\/?https?:\/\/.+$/, // avoid miss in renaming
  82. /\/{2,}/, // avoid miss in renaming
  83. /\s+\/\s+/, // avoid miss in renaming
  84. /.+\/edit$/,
  85. /.+\.md$/,
  86. /^(\.\.)$/, // see: https://github.com/weseek/growi/issues/3582
  87. /(\/\.\.)\/?/, // see: https://github.com/weseek/growi/issues/3582
  88. /^\/(_search|_private-legacy-pages)(\/.*|$)/,
  89. /^\/(installer|register|login|logout|admin|me|files|trash|paste|comments|tags|share)(\/.*|$)/,
  90. /^\/user\/[^/]+$/, // see: https://regex101.com/r/utVQct/1
  91. ];
  92. export const isCreatablePage = (path: string): boolean => {
  93. return !restrictedPatternsToCreate.some(pattern => path.match(pattern));
  94. };
  95. /**
  96. * return user path
  97. * @param user
  98. */
  99. // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
  100. export const userPageRoot = (user: any): string => {
  101. if (!user || !user.username) {
  102. return '';
  103. }
  104. return `/user/${user.username}`;
  105. };
  106. /**
  107. * return user path
  108. * @param parentPath
  109. * @param childPath
  110. * @param newPath
  111. */
  112. export const convertToNewAffiliationPath = (oldPath: string, newPath: string, childPath: string): string => {
  113. if (newPath == null) {
  114. throw new Error('Please input the new page path');
  115. }
  116. const pathRegExp = new RegExp(`^${escapeStringRegexp(oldPath)}`, 'i');
  117. return childPath.replace(pathRegExp, newPath);
  118. };
  119. /**
  120. * Encode SPACE and IDEOGRAPHIC SPACE
  121. * @param {string} path
  122. * @returns {string}
  123. */
  124. export const encodeSpaces = (path?:string): string | undefined => {
  125. if (path == null) {
  126. return undefined;
  127. }
  128. // Encode SPACE and IDEOGRAPHIC SPACE
  129. return path.replace(/ /g, '%20').replace(/\u3000/g, '%E3%80%80');
  130. };
  131. /**
  132. * Generate editor path
  133. * @param {string} paths
  134. * @returns {string}
  135. */
  136. export const generateEditorPath = (...paths: string[]): string => {
  137. const joinedPath = [...paths].join('/');
  138. if (!isCreatablePage(joinedPath)) {
  139. throw new Error('Invalid characters on path');
  140. }
  141. try {
  142. const url = new URL(joinedPath, 'https://dummy');
  143. return `${url.pathname}#edit`;
  144. }
  145. catch (err) {
  146. throw new Error('Invalid path format');
  147. }
  148. };
  149. /**
  150. * returns ancestors paths
  151. * @param {string} path
  152. * @param {string[]} ancestorPaths
  153. * @returns {string[]}
  154. */
  155. export const collectAncestorPaths = (path: string, ancestorPaths: string[] = []): string[] => {
  156. if (isTopPage(path)) return ancestorPaths;
  157. const parentPath = nodePath.dirname(path);
  158. ancestorPaths.push(parentPath);
  159. return collectAncestorPaths(parentPath, ancestorPaths);
  160. };
  161. /**
  162. * return paths without duplicate area of regexp /^${path}\/.+/i
  163. * ex. expect(omitDuplicateAreaPathFromPaths(['/A', '/A/B', '/A/B/C'])).toStrictEqual(['/A'])
  164. * @param paths paths to be tested
  165. * @returns omitted paths
  166. */
  167. export const omitDuplicateAreaPathFromPaths = (paths: string[]): string[] => {
  168. const uniquePaths = Array.from(new Set(paths));
  169. return uniquePaths.filter((path) => {
  170. const isDuplicate = uniquePaths.filter(p => (new RegExp(`^${p}\\/.+`, 'i')).test(path)).length > 0;
  171. return !isDuplicate;
  172. });
  173. };
  174. /**
  175. * return pages with path without duplicate area of regexp /^${path}\/.+/i
  176. * if the pages' path are the same, it will NOT omit any of them since the other attributes will not be the same
  177. * @param paths paths to be tested
  178. * @returns omitted paths
  179. */
  180. export const omitDuplicateAreaPageFromPages = (pages: any[]): any[] => {
  181. return pages.filter((page) => {
  182. const isDuplicate = pages.some(p => (new RegExp(`^${p.path}\\/.+`, 'i')).test(page.path));
  183. return !isDuplicate;
  184. });
  185. };
  186. /**
  187. * Check if the area of either path1 or path2 includes the area of the other path
  188. * The area of path is the same as /^\/hoge\//i
  189. * @param pathToTest string
  190. * @param pathToBeTested string
  191. * @returns boolean
  192. */
  193. export const isEitherOfPathAreaOverlap = (path1: string, path2: string): boolean => {
  194. if (path1 === path2) {
  195. return true;
  196. }
  197. const path1WithSlash = addTrailingSlash(path1);
  198. const path2WithSlash = addTrailingSlash(path2);
  199. const path1Area = new RegExp(`^${escapeStringRegexp(path1WithSlash)}`, 'i');
  200. const path2Area = new RegExp(`^${escapeStringRegexp(path2WithSlash)}`, 'i');
  201. if (path1Area.test(path2) || path2Area.test(path1)) {
  202. return true;
  203. }
  204. return false;
  205. };
  206. /**
  207. * Check if the area of pathToTest includes the area of pathToBeTested
  208. * The area of path is the same as /^\/hoge\//i
  209. * @param pathToTest string
  210. * @param pathToBeTested string
  211. * @returns boolean
  212. */
  213. export const isPathAreaOverlap = (pathToTest: string, pathToBeTested: string): boolean => {
  214. if (pathToTest === pathToBeTested) {
  215. return true;
  216. }
  217. const pathWithSlash = addTrailingSlash(pathToTest);
  218. const pathAreaToTest = new RegExp(`^${escapeStringRegexp(pathWithSlash)}`, 'i');
  219. if (pathAreaToTest.test(pathToBeTested)) {
  220. return true;
  221. }
  222. return false;
  223. };
  224. /**
  225. * Determine whether can move by fromPath and toPath
  226. * @param fromPath string
  227. * @param toPath string
  228. * @returns boolean
  229. */
  230. export const canMoveByPath = (fromPath: string, toPath: string): boolean => {
  231. return !isPathAreaOverlap(fromPath, toPath);
  232. };