page-path-utils.ts 4.9 KB

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