page-path-utils.ts 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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 is the top page of users
  56. * @param path
  57. */
  58. export const isTrashTopPage = (path: string): boolean => {
  59. return path === '/trash';
  60. };
  61. /**
  62. * Whether path belongs to the trash page
  63. * @param path
  64. */
  65. export const isTrashPage = (path: string): boolean => {
  66. // https://regex101.com/r/BSDdRr/1
  67. if (path.match(/^\/trash(\/.*)?$/)) {
  68. return true;
  69. }
  70. return false;
  71. };
  72. /**
  73. * Whether path belongs to the shared page
  74. * @param path
  75. */
  76. export const isSharedPage = (path: string): boolean => {
  77. // https://regex101.com/r/ZjdOiB/1
  78. if (path.match(/^\/share(\/.*)?$/)) {
  79. return true;
  80. }
  81. return false;
  82. };
  83. const restrictedPatternsToCreate: Array<RegExp> = [
  84. /\^|\$|\*|\+|#|%|\?/,
  85. /^\/-\/.*/,
  86. /^\/_r\/.*/,
  87. /^\/_apix?(\/.*)?/,
  88. /^\/?https?:\/\/.+$/, // avoid miss in renaming
  89. /\/{2,}/, // avoid miss in renaming
  90. /\s+\/\s+/, // avoid miss in renaming
  91. /.+\/edit$/,
  92. /.+\.md$/,
  93. /^(\.\.)$/, // see: https://github.com/weseek/growi/issues/3582
  94. /(\/\.\.)\/?/, // see: https://github.com/weseek/growi/issues/3582
  95. /^\/(_search|_private-legacy-pages)(\/.*|$)/,
  96. /^\/(installer|register|login|logout|admin|me|files|trash|paste|comments|tags|share)(\/.*|$)/,
  97. /^\/user\/[^/]+$/, // see: https://regex101.com/r/utVQct/1
  98. ];
  99. export const isCreatablePage = (path: string): boolean => {
  100. return !restrictedPatternsToCreate.some(pattern => path.match(pattern));
  101. };
  102. /**
  103. * return user path
  104. * @param user
  105. */
  106. // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
  107. export const userPageRoot = (user: any): string => {
  108. if (!user || !user.username) {
  109. return '';
  110. }
  111. return `/user/${user.username}`;
  112. };
  113. /**
  114. * return user path
  115. * @param parentPath
  116. * @param childPath
  117. * @param newPath
  118. */
  119. export const convertToNewAffiliationPath = (oldPath: string, newPath: string, childPath: string): string => {
  120. if (newPath == null) {
  121. throw new Error('Please input the new page path');
  122. }
  123. const pathRegExp = new RegExp(`^${escapeStringRegexp(oldPath)}`, 'i');
  124. return childPath.replace(pathRegExp, newPath);
  125. };
  126. /**
  127. * Encode SPACE and IDEOGRAPHIC SPACE
  128. * @param {string} path
  129. * @returns {string}
  130. */
  131. export const encodeSpaces = (path?:string): string | undefined => {
  132. if (path == null) {
  133. return undefined;
  134. }
  135. // Encode SPACE and IDEOGRAPHIC SPACE
  136. return path.replace(/ /g, '%20').replace(/\u3000/g, '%E3%80%80');
  137. };
  138. /**
  139. * Generate editor path
  140. * @param {string} paths
  141. * @returns {string}
  142. */
  143. export const generateEditorPath = (...paths: string[]): string => {
  144. const joinedPath = [...paths].join('/');
  145. if (!isCreatablePage(joinedPath)) {
  146. throw new Error('Invalid characters on path');
  147. }
  148. try {
  149. const url = new URL(joinedPath, 'https://dummy');
  150. return `${url.pathname}#edit`;
  151. }
  152. catch (err) {
  153. throw new Error('Invalid path format');
  154. }
  155. };
  156. /**
  157. * returns ancestors paths
  158. * @param {string} path
  159. * @param {string[]} ancestorPaths
  160. * @returns {string[]}
  161. */
  162. export const collectAncestorPaths = (path: string, ancestorPaths: string[] = []): string[] => {
  163. if (isTopPage(path)) return ancestorPaths;
  164. const parentPath = nodePath.dirname(path);
  165. ancestorPaths.push(parentPath);
  166. return collectAncestorPaths(parentPath, ancestorPaths);
  167. };
  168. /**
  169. * return paths without duplicate area of regexp /^${path}\/.+/i
  170. * ex. expect(omitDuplicateAreaPathFromPaths(['/A', '/A/B', '/A/B/C'])).toStrictEqual(['/A'])
  171. * @param paths paths to be tested
  172. * @returns omitted paths
  173. */
  174. export const omitDuplicateAreaPathFromPaths = (paths: string[]): string[] => {
  175. const uniquePaths = Array.from(new Set(paths));
  176. return uniquePaths.filter((path) => {
  177. const isDuplicate = uniquePaths.filter(p => (new RegExp(`^${p}\\/.+`, 'i')).test(path)).length > 0;
  178. return !isDuplicate;
  179. });
  180. };
  181. /**
  182. * return pages with path without duplicate area of regexp /^${path}\/.+/i
  183. * if the pages' path are the same, it will NOT omit any of them since the other attributes will not be the same
  184. * @param paths paths to be tested
  185. * @returns omitted paths
  186. */
  187. export const omitDuplicateAreaPageFromPages = (pages: any[]): any[] => {
  188. return pages.filter((page) => {
  189. const isDuplicate = pages.some(p => (new RegExp(`^${p.path}\\/.+`, 'i')).test(page.path));
  190. return !isDuplicate;
  191. });
  192. };
  193. /**
  194. * Check if the area of either path1 or path2 includes the area of the other path
  195. * The area of path is the same as /^\/hoge\//i
  196. * @param pathToTest string
  197. * @param pathToBeTested string
  198. * @returns boolean
  199. */
  200. export const isEitherOfPathAreaOverlap = (path1: string, path2: string): boolean => {
  201. if (path1 === path2) {
  202. return true;
  203. }
  204. const path1WithSlash = addTrailingSlash(path1);
  205. const path2WithSlash = addTrailingSlash(path2);
  206. const path1Area = new RegExp(`^${escapeStringRegexp(path1WithSlash)}`, 'i');
  207. const path2Area = new RegExp(`^${escapeStringRegexp(path2WithSlash)}`, 'i');
  208. if (path1Area.test(path2) || path2Area.test(path1)) {
  209. return true;
  210. }
  211. return false;
  212. };
  213. /**
  214. * Check if the area of pathToTest includes the area of pathToBeTested
  215. * The area of path is the same as /^\/hoge\//i
  216. * @param pathToTest string
  217. * @param pathToBeTested string
  218. * @returns boolean
  219. */
  220. export const isPathAreaOverlap = (pathToTest: string, pathToBeTested: string): boolean => {
  221. if (pathToTest === pathToBeTested) {
  222. return true;
  223. }
  224. const pathWithSlash = addTrailingSlash(pathToTest);
  225. const pathAreaToTest = new RegExp(`^${escapeStringRegexp(pathWithSlash)}`, 'i');
  226. if (pathAreaToTest.test(pathToBeTested)) {
  227. return true;
  228. }
  229. return false;
  230. };
  231. /**
  232. * Determine whether can move by fromPath and toPath
  233. * @param fromPath string
  234. * @param toPath string
  235. * @returns boolean
  236. */
  237. export const canMoveByPath = (fromPath: string, toPath: string): boolean => {
  238. return !isPathAreaOverlap(fromPath, toPath);
  239. };
  240. /**
  241. * check if string has '/' in it
  242. */
  243. export const hasSlash = (str: string): boolean => {
  244. return str.includes('/');
  245. };
  246. /**
  247. * Generate RegExp instance for one level lower path
  248. */
  249. export const generateChildrenRegExp = (path: string): RegExp => {
  250. // https://regex101.com/r/laJGzj/1
  251. // ex. /any_level1
  252. if (isTopPage(path)) return new RegExp(/^\/[^/]+$/);
  253. // https://regex101.com/r/mrDJrx/1
  254. // ex. /parent/any_child OR /any_level1
  255. return new RegExp(`^${path}(\\/[^/]+)\\/?$`);
  256. };