index.ts 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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. /^(\/.+){130,}$/, // avoid deep layer path. see: https://regex101.com/r/L0kzOD/1
  104. ];
  105. export const isCreatablePage = (path: string): boolean => {
  106. return !restrictedPatternsToCreate.some(pattern => path.match(pattern));
  107. };
  108. /**
  109. * return user's homepage path
  110. * @param user
  111. */
  112. export const userHomepagePath = (user: IUser | null | undefined): string => {
  113. if (user?.username == null) {
  114. return '';
  115. }
  116. return `/user/${user.username}`;
  117. };
  118. /**
  119. * return user path
  120. * @param parentPath
  121. * @param childPath
  122. * @param newPath
  123. */
  124. export const convertToNewAffiliationPath = (oldPath: string, newPath: string, childPath: string): string => {
  125. if (newPath == null) {
  126. throw new Error('Please input the new page path');
  127. }
  128. const pathRegExp = new RegExp(`^${escapeStringRegexp(oldPath)}`, 'i');
  129. return childPath.replace(pathRegExp, newPath);
  130. };
  131. /**
  132. * Encode SPACE and IDEOGRAPHIC SPACE
  133. * @param {string} path
  134. * @returns {string}
  135. */
  136. export const encodeSpaces = (path?:string): string | undefined => {
  137. if (path == null) {
  138. return undefined;
  139. }
  140. // Encode SPACE and IDEOGRAPHIC SPACE
  141. return path.replace(/ /g, '%20').replace(/\u3000/g, '%E3%80%80');
  142. };
  143. /**
  144. * Generate editor path
  145. * @param {string} paths
  146. * @returns {string}
  147. */
  148. export const generateEditorPath = (...paths: string[]): string => {
  149. const joinedPath = [...paths].join('/');
  150. if (!isCreatablePage(joinedPath)) {
  151. throw new Error('Invalid characters on path');
  152. }
  153. try {
  154. const url = new URL(joinedPath, 'https://dummy');
  155. return `${url.pathname}#edit`;
  156. }
  157. catch (err) {
  158. throw new Error('Invalid path format');
  159. }
  160. };
  161. /**
  162. * return paths without duplicate area of regexp /^${path}\/.+/i
  163. * ex. expect(omitDuplicateAreaPathFromPaths(['/A', '/A/B', '/A/B/C'])).toStrictEqual(['/A'])
  164. * @param paths paths to be tested
  165. * @returns omitted paths
  166. */
  167. export const omitDuplicateAreaPathFromPaths = (paths: string[]): string[] => {
  168. const uniquePaths = Array.from(new Set(paths));
  169. return uniquePaths.filter((path) => {
  170. const isDuplicate = uniquePaths.filter(p => (new RegExp(`^${p}\\/.+`, 'i')).test(path)).length > 0;
  171. return !isDuplicate;
  172. });
  173. };
  174. /**
  175. * return pages with path without duplicate area of regexp /^${path}\/.+/i
  176. * if the pages' path are the same, it will NOT omit any of them since the other attributes will not be the same
  177. * @param paths paths to be tested
  178. * @returns omitted paths
  179. */
  180. export const omitDuplicateAreaPageFromPages = (pages: any[]): any[] => {
  181. return pages.filter((page) => {
  182. const isDuplicate = pages.some(p => (new RegExp(`^${p.path}\\/.+`, 'i')).test(page.path));
  183. return !isDuplicate;
  184. });
  185. };
  186. /**
  187. * Check if the area of either path1 or path2 includes the area of the other path
  188. * The area of path is the same as /^\/hoge\//i
  189. * @param pathToTest string
  190. * @param pathToBeTested string
  191. * @returns boolean
  192. */
  193. export const isEitherOfPathAreaOverlap = (path1: string, path2: string): boolean => {
  194. if (path1 === path2) {
  195. return true;
  196. }
  197. const path1WithSlash = addTrailingSlash(path1);
  198. const path2WithSlash = addTrailingSlash(path2);
  199. const path1Area = new RegExp(`^${escapeStringRegexp(path1WithSlash)}`, 'i');
  200. const path2Area = new RegExp(`^${escapeStringRegexp(path2WithSlash)}`, 'i');
  201. if (path1Area.test(path2) || path2Area.test(path1)) {
  202. return true;
  203. }
  204. return false;
  205. };
  206. /**
  207. * Check if the area of pathToTest includes the area of pathToBeTested
  208. * The area of path is the same as /^\/hoge\//i
  209. * @param pathToTest string
  210. * @param pathToBeTested string
  211. * @returns boolean
  212. */
  213. export const isPathAreaOverlap = (pathToTest: string, pathToBeTested: string): boolean => {
  214. if (pathToTest === pathToBeTested) {
  215. return true;
  216. }
  217. const pathWithSlash = addTrailingSlash(pathToTest);
  218. const pathAreaToTest = new RegExp(`^${escapeStringRegexp(pathWithSlash)}`, 'i');
  219. if (pathAreaToTest.test(pathToBeTested)) {
  220. return true;
  221. }
  222. return false;
  223. };
  224. /**
  225. * Determine whether can move by fromPath and toPath
  226. * @param fromPath string
  227. * @param toPath string
  228. * @returns boolean
  229. */
  230. export const canMoveByPath = (fromPath: string, toPath: string): boolean => {
  231. return !isPathAreaOverlap(fromPath, toPath);
  232. };
  233. /**
  234. * check if string has '/' in it
  235. */
  236. export const hasSlash = (str: string): boolean => {
  237. return str.includes('/');
  238. };
  239. /**
  240. * Generate RegExp instance for one level lower path
  241. */
  242. export const generateChildrenRegExp = (path: string): RegExp => {
  243. // https://regex101.com/r/laJGzj/1
  244. // ex. /any_level1
  245. if (isTopPage(path)) return new RegExp(/^\/[^/]+$/);
  246. // https://regex101.com/r/mrDJrx/1
  247. // ex. /parent/any_child OR /any_level1
  248. return new RegExp(`^${path}(\\/[^/]+)\\/?$`);
  249. };
  250. /**
  251. * Get username from user page path
  252. * @param path string
  253. * @returns string | null
  254. */
  255. export const getUsernameByPath = (path: string): string | null => {
  256. let username: string | null = null;
  257. // https://regex101.com/r/qj4SfD/1
  258. const match = path.match(/^\/user\/([^/]+)\/?/);
  259. if (match) {
  260. username = match[1];
  261. }
  262. return username;
  263. };
  264. export const isGrobPatternPath = (path: string): boolean => {
  265. // https://regex101.com/r/IBy7HS/1
  266. const globPattern = /^(?:\/[^/*?[\]{}]+)*\/\*$/;
  267. return globPattern.test(path);
  268. };
  269. export * from './is-top-page';