index.ts 7.8 KB

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