index.ts 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. import escapeStringRegexp from 'escape-string-regexp';
  2. import { isValidObjectId } from '../objectid-utils';
  3. import { addTrailingSlash } from '../path-utils';
  4. import { isTopPage as _isTopPage } from './is-top-page';
  5. export const isTopPage = _isTopPage;
  6. /**
  7. * Whether path is the top page of users
  8. * @param path
  9. */
  10. export const isUsersTopPage = (path: string): boolean => {
  11. return path === '/user';
  12. };
  13. /**
  14. * Whether the path is permalink
  15. * @param path
  16. */
  17. export const isPermalink = (path: string): boolean => {
  18. const pageIdStr = path.substring(1);
  19. return isValidObjectId(pageIdStr);
  20. };
  21. /**
  22. * Whether path is user's homepage
  23. * @param path
  24. */
  25. export const isUsersHomepage = (path: string): boolean => {
  26. // https://regex101.com/r/utVQct/1
  27. if (path.match(/^\/user\/[^/]+$/)) {
  28. return true;
  29. }
  30. return false;
  31. };
  32. /**
  33. * Whether path is the protected pages for systems
  34. * @param path
  35. */
  36. export const isUsersProtectedPages = (path: string): boolean => {
  37. return isUsersTopPage(path) || isUsersHomepage(path);
  38. };
  39. /**
  40. * Whether path is movable
  41. * @param path
  42. */
  43. export const isMovablePage = (path: string): boolean => {
  44. return !isTopPage(path) && !isUsersProtectedPages(path);
  45. };
  46. /**
  47. * Whether path belongs to the user page
  48. * @param path
  49. */
  50. export const isUserPage = (path: string): boolean => {
  51. // https://regex101.com/r/MwifLR/1
  52. if (path.match(/^\/user\/.*?$/)) {
  53. return true;
  54. }
  55. return false;
  56. };
  57. /**
  58. * Whether path is the top page of users
  59. * @param path
  60. */
  61. export const isTrashTopPage = (path: string): boolean => {
  62. return path === '/trash';
  63. };
  64. /**
  65. * Whether path belongs to the trash page
  66. * @param path
  67. */
  68. export const isTrashPage = (path: string): boolean => {
  69. // https://regex101.com/r/BSDdRr/1
  70. if (path.match(/^\/trash(\/.*)?$/)) {
  71. return true;
  72. }
  73. return false;
  74. };
  75. /**
  76. * Whether path belongs to the shared page
  77. * @param path
  78. */
  79. export const isSharedPage = (path: string): boolean => {
  80. // https://regex101.com/r/ZjdOiB/1
  81. if (path.match(/^\/share(\/.*)?$/)) {
  82. return true;
  83. }
  84. return false;
  85. };
  86. const restrictedPatternsToCreate: Array<RegExp> = [
  87. /\^|\$|\*|\+|#|%|\?/,
  88. /^\/-\/.*/,
  89. /^\/_r\/.*/,
  90. /^\/_apix?(\/.*)?/,
  91. /^\/?https?:\/\/.+$/, // avoid miss in renaming
  92. /\/{2,}/, // avoid miss in renaming
  93. /\s+\/\s+/, // avoid miss in renaming
  94. /.+\/edit$/,
  95. /.+\.md$/,
  96. /^(\.\.)$/, // see: https://github.com/weseek/growi/issues/3582
  97. /(\/\.\.)\/?/, // see: https://github.com/weseek/growi/issues/3582
  98. /\\/, // see: https://github.com/weseek/growi/issues/7241
  99. /^\/(_search|_private-legacy-pages)(\/.*|$)/,
  100. /^\/(installer|register|login|logout|admin|me|files|trash|paste|comments|tags|share|attachment)(\/.*|$)/,
  101. /^\/user\/[^/]+$/, // see: https://regex101.com/r/utVQct/1
  102. ];
  103. export const isCreatablePage = (path: string): boolean => {
  104. return !restrictedPatternsToCreate.some(pattern => path.match(pattern));
  105. };
  106. /**
  107. * return user's homepage path
  108. * @param user
  109. */
  110. // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
  111. export const userHomepagePath = (user: any): string => {
  112. if (!user || !user.username) {
  113. return '';
  114. }
  115. return `/user/${user.username}`;
  116. };
  117. /**
  118. * return user path
  119. * @param parentPath
  120. * @param childPath
  121. * @param newPath
  122. */
  123. export const convertToNewAffiliationPath = (oldPath: string, newPath: string, childPath: string): string => {
  124. if (newPath == null) {
  125. throw new Error('Please input the new page path');
  126. }
  127. const pathRegExp = new RegExp(`^${escapeStringRegexp(oldPath)}`, 'i');
  128. return childPath.replace(pathRegExp, newPath);
  129. };
  130. /**
  131. * Encode SPACE and IDEOGRAPHIC SPACE
  132. * @param {string} path
  133. * @returns {string}
  134. */
  135. export const encodeSpaces = (path?:string): string | undefined => {
  136. if (path == null) {
  137. return undefined;
  138. }
  139. // Encode SPACE and IDEOGRAPHIC SPACE
  140. return path.replace(/ /g, '%20').replace(/\u3000/g, '%E3%80%80');
  141. };
  142. /**
  143. * Generate editor path
  144. * @param {string} paths
  145. * @returns {string}
  146. */
  147. export const generateEditorPath = (...paths: string[]): string => {
  148. const joinedPath = [...paths].join('/');
  149. if (!isCreatablePage(joinedPath)) {
  150. throw new Error('Invalid characters on path');
  151. }
  152. try {
  153. const url = new URL(joinedPath, 'https://dummy');
  154. return `${url.pathname}#edit`;
  155. }
  156. catch (err) {
  157. throw new Error('Invalid path format');
  158. }
  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. * check if string has '/' in it
  234. */
  235. export const hasSlash = (str: string): boolean => {
  236. return str.includes('/');
  237. };
  238. /**
  239. * Generate RegExp instance for one level lower path
  240. */
  241. export const generateChildrenRegExp = (path: string): RegExp => {
  242. // https://regex101.com/r/laJGzj/1
  243. // ex. /any_level1
  244. if (isTopPage(path)) return new RegExp(/^\/[^/]+$/);
  245. // https://regex101.com/r/mrDJrx/1
  246. // ex. /parent/any_child OR /any_level1
  247. return new RegExp(`^${path}(\\/[^/]+)\\/?$`);
  248. };
  249. export * from './is-top-page';
  250. export * from './collect-ancestor-paths';