index.ts 7.7 KB

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