path-utils.ts 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. export const userPageRoot = (user: any): string => {
  70. if (!user || !user.username) {
  71. return '';
  72. }
  73. return `/user/${user.username}`;
  74. };
  75. /**
  76. * return user path
  77. * @param parentPath
  78. * @param childPath
  79. * @param newPath
  80. */
  81. export const convertToNewAffiliationPath = (oldPath: string, newPath: string, childPath: string): string => {
  82. if (newPath === null) {
  83. throw new Error('Please input the new page path');
  84. }
  85. const pathRegExp = new RegExp(`^${escapeStringRegexp(oldPath)}`, 'i');
  86. return childPath.replace(pathRegExp, newPath);
  87. };
  88. /**
  89. * Encode SPACE and IDEOGRAPHIC SPACE
  90. * @param {string} path
  91. * @returns {string}
  92. */
  93. export const encodeSpaces = (path?:string): string | undefined => {
  94. if (path == null) {
  95. return undefined;
  96. }
  97. // Encode SPACE and IDEOGRAPHIC SPACE
  98. return path.replace(/ /g, '%20').replace(/\u3000/g, '%E3%80%80');
  99. };
  100. /**
  101. * Generate editor path
  102. * @param {string} paths
  103. * @returns {string}
  104. */
  105. export const generateEditorPath = (...paths) => {
  106. const joinedPath = [...paths].join('/');
  107. if (!isCreatablePage(joinedPath)) {
  108. throw new Error('Invalid characters on path');
  109. }
  110. try {
  111. const url = new URL(joinedPath, 'https://dummy');
  112. return `${url.pathname}#edit`;
  113. }
  114. catch (err) {
  115. throw new Error('Invalid path format');
  116. }
  117. };