devided-page-path.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. const { pathUtils } = require('growi-commons');
  2. // https://regex101.com/r/BahpKX/2
  3. const PATTERN_INCLUDE_DATE = /^(.+\/[^/]+)\/(\d{4}|\d{4}\/\d{2}|\d{4}\/\d{2}\/\d{2})$/;
  4. // https://regex101.com/r/WVpPpY/1
  5. const PATTERN_DEFAULT = /^((.*)\/)?([^/]+)$/;
  6. class DevidedPagePath {
  7. constructor(path, skipNormalize = false, evalDatePath = false) {
  8. this.isRoot = false;
  9. this.isFormerRoot = false;
  10. this.former = null;
  11. this.latter = null;
  12. // root
  13. if (path == null || path === '' || path === '/') {
  14. this.isRoot = true;
  15. this.latter = '/';
  16. return;
  17. }
  18. const pagePath = skipNormalize ? path : pathUtils.normalizePath(path);
  19. this.latter = pagePath;
  20. // evaluate date path
  21. if (evalDatePath) {
  22. const matchDate = pagePath.match(PATTERN_INCLUDE_DATE);
  23. if (matchDate != null) {
  24. this.former = matchDate[1];
  25. this.latter = matchDate[2];
  26. return;
  27. }
  28. }
  29. const matchDefault = pagePath.match(PATTERN_DEFAULT);
  30. if (matchDefault != null) {
  31. this.isFormerRoot = matchDefault[1] === '/';
  32. this.former = matchDefault[2];
  33. this.latter = matchDefault[3];
  34. }
  35. }
  36. }
  37. module.exports = DevidedPagePath;