page-path-utils.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. import escapeStringRegexp from 'escape-string-regexp';
  2. /**
  3. * Whether path is the top page
  4. * @param path
  5. */
  6. export const isTopPage = (path: string): boolean => {
  7. return path === '/';
  8. };
  9. /**
  10. * Whether path belongs to the trash page
  11. * @param path
  12. */
  13. export const isTrashPage = (path: string): boolean => {
  14. // https://regex101.com/r/BSDdRr/1
  15. if (path.match(/^\/trash(\/.*)?$/)) {
  16. return true;
  17. }
  18. return false;
  19. };
  20. /**
  21. * Whether path belongs to the user page
  22. * @param path
  23. */
  24. export const isUserPage = (path: string): boolean => {
  25. // https://regex101.com/r/SxPejV/1
  26. if (path.match(/^\/user(\/.*)?$/)) {
  27. return true;
  28. }
  29. return false;
  30. };
  31. /**
  32. * Whether path belongs to the shared page
  33. * @param path
  34. */
  35. export const isSharedPage = (path: string): boolean => {
  36. // https://regex101.com/r/ZjdOiB/1
  37. if (path.match(/^\/share(\/.*)?$/)) {
  38. return true;
  39. }
  40. return false;
  41. };
  42. const restrictedPatternsToDelete: Array<RegExp> = [
  43. /^\/user\/[^/]+$/, // user page
  44. ];
  45. export const isDeletablePage = (path: string): boolean => {
  46. return !restrictedPatternsToDelete.some(pattern => path.match(pattern));
  47. };
  48. const restrictedPatternsToCreate: Array<RegExp> = [
  49. /\^|\$|\*|\+|#|%|\?/,
  50. /^\/-\/.*/,
  51. /^\/_r\/.*/,
  52. /^\/_apix?(\/.*)?/,
  53. /^\/?https?:\/\/.+$/, // avoid miss in renaming
  54. /\/{2,}/, // avoid miss in renaming
  55. /\s+\/\s+/, // avoid miss in renaming
  56. /.+\/edit$/,
  57. /.+\.md$/,
  58. /^(\.\.)$/, // see: https://github.com/weseek/growi/issues/3582
  59. /(\/\.\.)\/?/, // see: https://github.com/weseek/growi/issues/3582
  60. /^\/(installer|register|login|logout|admin|me|files|trash|paste|comments|tags|share)(\/.*|$)/,
  61. ];
  62. export const isCreatablePage = (path: string): boolean => {
  63. return !restrictedPatternsToCreate.some(pattern => path.match(pattern));
  64. };
  65. /**
  66. * return user path
  67. * @param user
  68. */
  69. // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
  70. export const userPageRoot = (user: any): string => {
  71. if (!user || !user.username) {
  72. return '';
  73. }
  74. return `/user/${user.username}`;
  75. };
  76. /**
  77. * return user path
  78. * @param parentPath
  79. * @param childPath
  80. * @param newPath
  81. */
  82. export const convertToNewAffiliationPath = (oldPath: string, newPath: string, childPath: string): string => {
  83. if (newPath === null) {
  84. throw new Error('Please input the new page path');
  85. }
  86. const pathRegExp = new RegExp(`^${escapeStringRegexp(oldPath)}`, 'i');
  87. return childPath.replace(pathRegExp, newPath);
  88. };
  89. /**
  90. * Encode SPACE and IDEOGRAPHIC SPACE
  91. * @param {string} path
  92. * @returns {string}
  93. */
  94. export const encodeSpaces = (path?:string): string | undefined => {
  95. if (path == null) {
  96. return undefined;
  97. }
  98. // Encode SPACE and IDEOGRAPHIC SPACE
  99. return path.replace(/ /g, '%20').replace(/\u3000/g, '%E3%80%80');
  100. };
  101. /**
  102. * Generate editor path
  103. * @param {string} paths
  104. * @returns {string}
  105. */
  106. export const generateEditorPath = (...paths: string[]): string => {
  107. const joinedPath = [...paths].join('/');
  108. if (!isCreatablePage(joinedPath)) {
  109. throw new Error('Invalid characters on path');
  110. }
  111. try {
  112. const url = new URL(joinedPath, 'https://dummy');
  113. return `${url.pathname}#edit`;
  114. }
  115. catch (err) {
  116. throw new Error('Invalid path format');
  117. }
  118. };