page-path-utils.ts 4.3 KB

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