devided-page-path.ts 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. import * as pathUtils from '../utils/path-utils';
  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. export class DevidedPagePath {
  5. isRoot: boolean;
  6. isFormerRoot: boolean;
  7. former: string;
  8. latter: string;
  9. constructor(path: string, skipNormalize = false, evalDatePath = false) {
  10. this.isRoot = false;
  11. this.isFormerRoot = false;
  12. this.former = '';
  13. // root
  14. if (path == null || path === '' || path === '/') {
  15. this.isRoot = true;
  16. this.latter = '/';
  17. return;
  18. }
  19. const pagePath = skipNormalize ? path : pathUtils.normalizePath(path);
  20. this.latter = pagePath;
  21. // evaluate date path
  22. if (evalDatePath) {
  23. const matchDate = pagePath.match(PATTERN_INCLUDE_DATE);
  24. if (matchDate != null) {
  25. this.former = matchDate[1];
  26. this.latter = matchDate[2];
  27. return;
  28. }
  29. }
  30. let PATTERN_DEFAULT = /^((.*)\/(?!em>))?(.+)$/; // this will ignore em's end tags
  31. try { // for non-chrome browsers
  32. // eslint-disable-next-line regex/invalid
  33. PATTERN_DEFAULT = new RegExp('^((.*)(?<!<)\\/)?(.+)$'); // https://regex101.com/r/HJNvMW/1
  34. }
  35. catch (err) {
  36. // lookbehind regex is not supported on non-chrome browsers
  37. }
  38. const matchDefault = pagePath.match(PATTERN_DEFAULT);
  39. if (matchDefault != null) {
  40. this.isFormerRoot = matchDefault[1] === '/';
  41. this.former = matchDefault[2];
  42. this.latter = matchDefault[3];
  43. }
  44. }
  45. }