index.ts 7.7 KB

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