index.ts 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. import escapeStringRegexp from 'escape-string-regexp';
  2. import type { IUser } from '~/interfaces';
  3. import { isValidObjectId } from '../objectid-utils';
  4. import { addTrailingSlash } from '../path-utils';
  5. import { isTopPage as _isTopPage } from './is-top-page';
  6. export const isTopPage = _isTopPage;
  7. export * from './generate-children-regexp';
  8. /**
  9. * Whether path is the top page of users
  10. * @param path
  11. */
  12. export const isUsersTopPage = (path: string): boolean => {
  13. return path === '/user';
  14. };
  15. /**
  16. * Whether the path is permalink
  17. * @param path
  18. */
  19. export const isPermalink = (path: string): boolean => {
  20. const pageIdStr = path.substring(1);
  21. return isValidObjectId(pageIdStr);
  22. };
  23. /**
  24. * Whether path is user's homepage
  25. * @param path
  26. */
  27. export const isUsersHomepage = (path: string): boolean => {
  28. // https://regex101.com/r/utVQct/1
  29. if (path.match(/^\/user\/[^/]+$/)) {
  30. return true;
  31. }
  32. return false;
  33. };
  34. /**
  35. * Whether path is the protected pages for systems
  36. * @param path
  37. */
  38. export const isUsersProtectedPages = (path: string): boolean => {
  39. return isUsersTopPage(path) || isUsersHomepage(path);
  40. };
  41. /**
  42. * Whether path is movable
  43. * @param path
  44. */
  45. export const isMovablePage = (path: string): boolean => {
  46. return !isTopPage(path) && !isUsersProtectedPages(path);
  47. };
  48. /**
  49. * Whether path belongs to the user page
  50. * @param path
  51. */
  52. export const isUserPage = (path: string): boolean => {
  53. // https://regex101.com/r/MwifLR/1
  54. if (path.match(/^\/user\/.*?$/)) {
  55. return true;
  56. }
  57. return false;
  58. };
  59. /**
  60. * Whether path is the top page of users
  61. * @param path
  62. */
  63. export const isTrashTopPage = (path: string): boolean => {
  64. return path === '/trash';
  65. };
  66. /**
  67. * Whether path belongs to the trash page
  68. * @param path
  69. */
  70. export const isTrashPage = (path: string): boolean => {
  71. // https://regex101.com/r/BSDdRr/1
  72. if (path.match(/^\/trash(\/.*)?$/)) {
  73. return true;
  74. }
  75. return false;
  76. };
  77. /**
  78. * Whether path belongs to the shared page
  79. * @param path
  80. */
  81. export const isSharedPage = (path: string): boolean => {
  82. // https://regex101.com/r/ZjdOiB/1
  83. if (path.match(/^\/share(\/.*)?$/)) {
  84. return true;
  85. }
  86. return false;
  87. };
  88. const restrictedPatternsToCreate: Array<RegExp> = [
  89. /\^|\$|\*|\+|#|<|>|%|\?/,
  90. /^\/-\/.*/,
  91. /^\/_r\/.*/,
  92. /^\/_apix?(\/.*)?/,
  93. /^\/?https?:\/\/.+$/, // avoid miss in renaming
  94. /\/{2,}/, // avoid miss in renaming
  95. /\s+\/\s+/, // avoid miss in renaming
  96. /.+\/edit$/,
  97. /.+\.md$/,
  98. /^(\.\.)$/, // see: https://github.com/weseek/growi/issues/3582
  99. /(\/\.\.)\/?/, // see: https://github.com/weseek/growi/issues/3582
  100. /\\/, // see: https://github.com/weseek/growi/issues/7241
  101. /^\/(_search|_private-legacy-pages)(\/.*|$)/,
  102. /^\/(installer|register|login|logout|admin|me|files|trash|paste|comments|tags|share|attachment)(\/.*|$)/,
  103. /^\/user(?:\/[^/]+)?$/, // https://regex101.com/r/9Eh2S1/1
  104. /^(\/.+){130,}$/, // avoid deep layer path. see: https://regex101.com/r/L0kzOD/1
  105. ];
  106. export const isCreatablePage = (path: string): boolean => {
  107. return !restrictedPatternsToCreate.some(pattern => path.match(pattern));
  108. };
  109. /**
  110. * return user's homepage path
  111. * @param user
  112. */
  113. export const userHomepagePath = (user: { username: string } | null | undefined): string => {
  114. if (user?.username == null) {
  115. return '';
  116. }
  117. return `/user/${user.username}`;
  118. };
  119. /**
  120. * return user path
  121. * @param parentPath
  122. * @param childPath
  123. * @param newPath
  124. */
  125. export const convertToNewAffiliationPath = (oldPath: string, newPath: string, childPath: string): string => {
  126. if (newPath == null) {
  127. throw new Error('Please input the new page path');
  128. }
  129. const pathRegExp = new RegExp(`^${escapeStringRegexp(oldPath)}`, 'i');
  130. return childPath.replace(pathRegExp, newPath);
  131. };
  132. /**
  133. * Encode SPACE and IDEOGRAPHIC SPACE
  134. * @param {string} path
  135. * @returns {string}
  136. */
  137. export const encodeSpaces = (path?:string): string | undefined => {
  138. if (path == null) {
  139. return undefined;
  140. }
  141. // Encode SPACE and IDEOGRAPHIC SPACE
  142. return path.replace(/ /g, '%20').replace(/\u3000/g, '%E3%80%80');
  143. };
  144. /**
  145. * Generate editor path
  146. * @param {string} paths
  147. * @returns {string}
  148. */
  149. export const generateEditorPath = (...paths: string[]): string => {
  150. const joinedPath = [...paths].join('/');
  151. if (!isCreatablePage(joinedPath)) {
  152. throw new Error('Invalid characters on path');
  153. }
  154. try {
  155. const url = new URL(joinedPath, 'https://dummy');
  156. return `${url.pathname}#edit`;
  157. }
  158. catch (err) {
  159. throw new Error('Invalid path format');
  160. }
  161. };
  162. /**
  163. * return paths without duplicate area of regexp /^${path}\/.+/i
  164. * ex. expect(omitDuplicateAreaPathFromPaths(['/A', '/A/B', '/A/B/C'])).toStrictEqual(['/A'])
  165. * @param paths paths to be tested
  166. * @returns omitted paths
  167. */
  168. export const omitDuplicateAreaPathFromPaths = (paths: string[]): string[] => {
  169. const uniquePaths = Array.from(new Set(paths));
  170. return uniquePaths.filter((path) => {
  171. const isDuplicate = uniquePaths.filter(p => (new RegExp(`^${p}\\/.+`, 'i')).test(path)).length > 0;
  172. return !isDuplicate;
  173. });
  174. };
  175. /**
  176. * return pages with path without duplicate area of regexp /^${path}\/.+/i
  177. * if the pages' path are the same, it will NOT omit any of them since the other attributes will not be the same
  178. * @param paths paths to be tested
  179. * @returns omitted paths
  180. */
  181. export const omitDuplicateAreaPageFromPages = (pages: any[]): any[] => {
  182. return pages.filter((page) => {
  183. const isDuplicate = pages.some(p => (new RegExp(`^${p.path}\\/.+`, 'i')).test(page.path));
  184. return !isDuplicate;
  185. });
  186. };
  187. /**
  188. * Check if the area of either path1 or path2 includes the area of the other path
  189. * The area of path is the same as /^\/hoge\//i
  190. * @param pathToTest string
  191. * @param pathToBeTested string
  192. * @returns boolean
  193. */
  194. export const isEitherOfPathAreaOverlap = (path1: string, path2: string): boolean => {
  195. if (path1 === path2) {
  196. return true;
  197. }
  198. const path1WithSlash = addTrailingSlash(path1);
  199. const path2WithSlash = addTrailingSlash(path2);
  200. const path1Area = new RegExp(`^${escapeStringRegexp(path1WithSlash)}`, 'i');
  201. const path2Area = new RegExp(`^${escapeStringRegexp(path2WithSlash)}`, 'i');
  202. if (path1Area.test(path2) || path2Area.test(path1)) {
  203. return true;
  204. }
  205. return false;
  206. };
  207. /**
  208. * Check if the area of pathToTest includes the area of pathToBeTested
  209. * The area of path is the same as /^\/hoge\//i
  210. * @param pathToTest string
  211. * @param pathToBeTested string
  212. * @returns boolean
  213. */
  214. export const isPathAreaOverlap = (pathToTest: string, pathToBeTested: string): boolean => {
  215. if (pathToTest === pathToBeTested) {
  216. return true;
  217. }
  218. const pathWithSlash = addTrailingSlash(pathToTest);
  219. const pathAreaToTest = new RegExp(`^${escapeStringRegexp(pathWithSlash)}`, 'i');
  220. if (pathAreaToTest.test(pathToBeTested)) {
  221. return true;
  222. }
  223. return false;
  224. };
  225. /**
  226. * Determine whether can move by fromPath and toPath
  227. * @param fromPath string
  228. * @param toPath string
  229. * @returns boolean
  230. */
  231. export const canMoveByPath = (fromPath: string, toPath: string): boolean => {
  232. return !isPathAreaOverlap(fromPath, toPath);
  233. };
  234. /**
  235. * check if string has '/' in it
  236. */
  237. export const hasSlash = (str: string): boolean => {
  238. return str.includes('/');
  239. };
  240. /**
  241. * Get username from user page path
  242. * @param path string
  243. * @returns string | null
  244. */
  245. export const getUsernameByPath = (path: string): string | null => {
  246. let username: string | null = null;
  247. // https://regex101.com/r/qj4SfD/1
  248. const match = path.match(/^\/user\/([^/]+)\/?/);
  249. if (match) {
  250. username = match[1];
  251. }
  252. return username;
  253. };
  254. export const isGlobPatternPath = (path: string): boolean => {
  255. // https://regex101.com/r/IBy7HS/1
  256. const globPattern = /^(?:\/[^/*?[\]{}]+)*\/\*$/;
  257. return globPattern.test(path);
  258. };
  259. export * from './is-top-page';