devided-page-path.ts 1.5 KB

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