index.ts 7.6 KB

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