2
0

index.ts 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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. /**
  8. * Whether path is the top page of users
  9. * @param path
  10. */
  11. export const isUsersTopPage = (path: string): boolean => {
  12. return path === '/user';
  13. };
  14. /**
  15. * Whether the path is permalink
  16. * @param path
  17. */
  18. export const isPermalink = (path: string): boolean => {
  19. const pageIdStr = path.substring(1);
  20. return isValidObjectId(pageIdStr);
  21. };
  22. /**
  23. * Whether path is user's homepage
  24. * @param path
  25. */
  26. export const isUsersHomepage = (path: string): boolean => {
  27. // https://regex101.com/r/utVQct/1
  28. if (path.match(/^\/user\/[^/]+$/)) {
  29. return true;
  30. }
  31. return false;
  32. };
  33. /**
  34. * Whether path is the protected pages for systems
  35. * @param path
  36. */
  37. export const isUsersProtectedPages = (path: string): boolean => {
  38. return isUsersTopPage(path) || isUsersHomepage(path);
  39. };
  40. /**
  41. * Whether path is movable
  42. * @param path
  43. */
  44. export const isMovablePage = (path: string): boolean => {
  45. return !isTopPage(path) && !isUsersProtectedPages(path);
  46. };
  47. /**
  48. * Whether path belongs to the user page
  49. * @param path
  50. */
  51. export const isUserPage = (path: string): boolean => {
  52. // https://regex101.com/r/MwifLR/1
  53. if (path.match(/^\/user\/.*?$/)) {
  54. return true;
  55. }
  56. return false;
  57. };
  58. /**
  59. * Whether path is the top page of users
  60. * @param path
  61. */
  62. export const isTrashTopPage = (path: string): boolean => {
  63. return path === '/trash';
  64. };
  65. /**
  66. * Whether path belongs to the trash page
  67. * @param path
  68. */
  69. export const isTrashPage = (path: string): boolean => {
  70. // https://regex101.com/r/BSDdRr/1
  71. if (path.match(/^\/trash(\/.*)?$/)) {
  72. return true;
  73. }
  74. return false;
  75. };
  76. /**
  77. * Whether path belongs to the shared page
  78. * @param path
  79. */
  80. export const isSharedPage = (path: string): boolean => {
  81. // https://regex101.com/r/ZjdOiB/1
  82. if (path.match(/^\/share(\/.*)?$/)) {
  83. return true;
  84. }
  85. return false;
  86. };
  87. const restrictedPatternsToCreate: Array<RegExp> = [
  88. /\^|\$|\*|\+|#|<|>|%|\?/,
  89. /^\/-\/.*/,
  90. /^\/_r\/.*/,
  91. /^\/_apix?(\/.*)?/,
  92. /^\/?https?:\/\/.+$/, // avoid miss in renaming
  93. /\/{2,}/, // avoid miss in renaming
  94. /\s+\/\s+/, // avoid miss in renaming
  95. /.+\/edit$/,
  96. /.+\.md$/,
  97. /^(\.\.)$/, // see: https://github.com/weseek/growi/issues/3582
  98. /(\/\.\.)\/?/, // see: https://github.com/weseek/growi/issues/3582
  99. /\\/, // see: https://github.com/weseek/growi/issues/7241
  100. /^\/(_search|_private-legacy-pages)(\/.*|$)/,
  101. /^\/(installer|register|login|logout|admin|me|files|trash|paste|comments|tags|share|attachment)(\/.*|$)/,
  102. /^\/user(?:\/[^/]+)?$/, // https://regex101.com/r/9Eh2S1/1
  103. ];
  104. export const isCreatablePage = (path: string): boolean => {
  105. return !restrictedPatternsToCreate.some(pattern => path.match(pattern)) && path.length <= 2_000_000;
  106. };
  107. /**
  108. * return user's homepage path
  109. * @param user
  110. */
  111. export const userHomepagePath = (user: IUser | null | undefined): string => {
  112. if (user?.username == null) {
  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. /**
  250. * Get username from user page path
  251. * @param path string
  252. * @returns string | null
  253. */
  254. export const getUsernameByPath = (path: string): string | null => {
  255. let username: string | null = null;
  256. // https://regex101.com/r/qj4SfD/1
  257. const match = path.match(/^\/user\/([^/]+)\/?/);
  258. if (match) {
  259. username = match[1];
  260. }
  261. return username;
  262. };
  263. export * from './is-top-page';