devided-page-path.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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. // https://regex101.com/r/HJNvMW/1
  5. const PATTERN_DEFAULT = /^((.*)(?<!<)\/)?(.+)$/gm;
  6. // https://regex101.com/r/4J4JuR/1
  7. const PATTERN_PATH_WITH_ANY_HTML_TAGS = /<("[^"]*"|'[^']*'|[^'">])*>/g;
  8. export class DevidedPagePath {
  9. constructor(path, skipNormalize = false, evalDatePath = false) {
  10. this.isRoot = false;
  11. this.isFormerRoot = false;
  12. this.former = null;
  13. this.latter = null;
  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. // highlighted path
  32. const regex = new RegExp(PATTERN_PATH_WITH_ANY_HTML_TAGS);
  33. // testing whether the html tags exists in the path or not
  34. if (regex.test(pagePath)) {
  35. const matchDefault = pagePath.match(PATTERN_DEFAULT);
  36. this.isFormerRoot = matchDefault[1] === '/';
  37. this.former = matchDefault[2];
  38. this.latter = matchDefault[3];
  39. return;
  40. }
  41. const matchDefault = pagePath.match(PATTERN_DEFAULT);
  42. if (matchDefault != null) {
  43. this.isFormerRoot = matchDefault[1] === '/';
  44. this.former = matchDefault[2];
  45. this.latter = matchDefault[3];
  46. }
  47. }
  48. }