page-path-utils.ts 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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. ];
  91. export const isCreatablePage = (path: string): boolean => {
  92. return !restrictedPatternsToCreate.some(pattern => path.match(pattern));
  93. };
  94. /**
  95. * return user path
  96. * @param user
  97. */
  98. // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
  99. export const userPageRoot = (user: any): string => {
  100. if (!user || !user.username) {
  101. return '';
  102. }
  103. return `/user/${user.username}`;
  104. };
  105. /**
  106. * return user path
  107. * @param parentPath
  108. * @param childPath
  109. * @param newPath
  110. */
  111. export const convertToNewAffiliationPath = (oldPath: string, newPath: string, childPath: string): string => {
  112. if (newPath === null) {
  113. throw new Error('Please input the new page path');
  114. }
  115. const pathRegExp = new RegExp(`^${escapeStringRegexp(oldPath)}`, 'i');
  116. return childPath.replace(pathRegExp, newPath);
  117. };
  118. /**
  119. * Encode SPACE and IDEOGRAPHIC SPACE
  120. * @param {string} path
  121. * @returns {string}
  122. */
  123. export const encodeSpaces = (path?:string): string | undefined => {
  124. if (path == null) {
  125. return undefined;
  126. }
  127. // Encode SPACE and IDEOGRAPHIC SPACE
  128. return path.replace(/ /g, '%20').replace(/\u3000/g, '%E3%80%80');
  129. };
  130. /**
  131. * Generate editor path
  132. * @param {string} paths
  133. * @returns {string}
  134. */
  135. export const generateEditorPath = (...paths: string[]): string => {
  136. const joinedPath = [...paths].join('/');
  137. if (!isCreatablePage(joinedPath)) {
  138. throw new Error('Invalid characters on path');
  139. }
  140. try {
  141. const url = new URL(joinedPath, 'https://dummy');
  142. return `${url.pathname}#edit`;
  143. }
  144. catch (err) {
  145. throw new Error('Invalid path format');
  146. }
  147. };
  148. /**
  149. * returns ancestors paths
  150. * @param {string} path
  151. * @param {string[]} ancestorPaths
  152. * @returns {string[]}
  153. */
  154. export const collectAncestorPaths = (path: string, ancestorPaths: string[] = []): string[] => {
  155. if (isTopPage(path)) return ancestorPaths;
  156. const parentPath = nodePath.dirname(path);
  157. ancestorPaths.push(parentPath);
  158. return collectAncestorPaths(parentPath, ancestorPaths);
  159. };
  160. /**
  161. * return paths without duplicate area of regexp /^${path}\/.+/i
  162. * ex. expect(omitDuplicateAreaPathFromPaths(['/A', '/A/B', '/A/B/C'])).toStrictEqual(['/A'])
  163. * @param paths paths to be tested
  164. * @returns omitted paths
  165. */
  166. export const omitDuplicateAreaPathFromPaths = (paths: string[]): string[] => {
  167. const uniquePaths = Array.from(new Set(paths));
  168. return uniquePaths.filter((path) => {
  169. const isDuplicate = uniquePaths.filter(p => (new RegExp(`^${p}\\/.+`, 'i')).test(path)).length > 0;
  170. return !isDuplicate;
  171. });
  172. };
  173. /**
  174. * return pages with path without duplicate area of regexp /^${path}\/.+/i
  175. * if the pages' path are the same, it will NOT omit any of them since the other attributes will not be the same
  176. * @param paths paths to be tested
  177. * @returns omitted paths
  178. */
  179. export const omitDuplicateAreaPageFromPages = (pages: any[]): any[] => {
  180. return pages.filter((page) => {
  181. const isDuplicate = pages.some(p => (new RegExp(`^${p.path}\\/.+`, 'i')).test(page.path));
  182. return !isDuplicate;
  183. });
  184. };
  185. /**
  186. * Check if the area of either path1 or path2 includes the area of the other path
  187. * The area of path is the same as /^\/hoge\//i
  188. * @param pathToTest string
  189. * @param pathToBeTested string
  190. * @returns boolean
  191. */
  192. export const isEitherOfPathAreaOverlap = (path1: string, path2: string): boolean => {
  193. if (path1 === path2) {
  194. return true;
  195. }
  196. const path1WithSlash = addTrailingSlash(path1);
  197. const path2WithSlash = addTrailingSlash(path2);
  198. const path1Area = new RegExp(`^${escapeStringRegexp(path1WithSlash)}`, 'i');
  199. const path2Area = new RegExp(`^${escapeStringRegexp(path2WithSlash)}`, 'i');
  200. if (path1Area.test(path2) || path2Area.test(path1)) {
  201. return true;
  202. }
  203. return false;
  204. };
  205. /**
  206. * Check if the area of pathToTest includes the area of pathToBeTested
  207. * The area of path is the same as /^\/hoge\//i
  208. * @param pathToTest string
  209. * @param pathToBeTested string
  210. * @returns boolean
  211. */
  212. export const isPathAreaOverlap = (pathToTest: string, pathToBeTested: string): boolean => {
  213. if (pathToTest === pathToBeTested) {
  214. return true;
  215. }
  216. const pathWithSlash = addTrailingSlash(pathToTest);
  217. const pathAreaToTest = new RegExp(`^${escapeStringRegexp(pathWithSlash)}`, 'i');
  218. if (pathAreaToTest.test(pathToBeTested)) {
  219. return true;
  220. }
  221. return false;
  222. };
  223. /**
  224. * Determine whether can move by fromPath and toPath
  225. * @param fromPath string
  226. * @param toPath string
  227. * @returns boolean
  228. */
  229. export const canMoveByPath = (fromPath: string, toPath: string): boolean => {
  230. return !isPathAreaOverlap(fromPath, toPath);
  231. };
  232. /**
  233. * Determine whether can drag by path
  234. * @param path string
  235. * @returns boolean
  236. */
  237. export const canDragByPath = (path: string): boolean => {
  238. return !isUserPage(path);
  239. };