is.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. 'use strict';
  2. function isCurrentHelper(path = '/', strict) {
  3. const currentPath = this.path.replace(/^[^/].*/, '/$&');
  4. if (strict) {
  5. if (path.endsWith('/')) path += 'index.html';
  6. path = path.replace(/^[^/].*/, '/$&');
  7. return currentPath === path;
  8. }
  9. path = path.replace(/\/index\.html$/, '/');
  10. if (path === '/') return currentPath === '/index.html';
  11. path = path.replace(/^[^/].*/, '/$&');
  12. return currentPath.startsWith(path);
  13. }
  14. function isHomeHelper() {
  15. return Boolean(this.page.__index);
  16. }
  17. function isPostHelper() {
  18. return Boolean(this.page.__post);
  19. }
  20. function isPageHelper() {
  21. return Boolean(this.page.__page);
  22. }
  23. function isArchiveHelper() {
  24. return Boolean(this.page.archive);
  25. }
  26. function isYearHelper(year) {
  27. const { page } = this;
  28. if (!page.archive) return false;
  29. if (year) {
  30. return page.year === year;
  31. }
  32. return Boolean(page.year);
  33. }
  34. function isMonthHelper(year, month) {
  35. const { page } = this;
  36. if (!page.archive) return false;
  37. if (year) {
  38. if (month) {
  39. return page.year === year && page.month === month;
  40. }
  41. return page.month === year;
  42. }
  43. return Boolean(page.year && page.month);
  44. }
  45. function isCategoryHelper(category) {
  46. if (category) {
  47. return this.page.category === category;
  48. }
  49. return Boolean(this.page.category);
  50. }
  51. function isTagHelper(tag) {
  52. if (tag) {
  53. return this.page.tag === tag;
  54. }
  55. return Boolean(this.page.tag);
  56. }
  57. exports.current = isCurrentHelper;
  58. exports.home = isHomeHelper;
  59. exports.post = isPostHelper;
  60. exports.page = isPageHelper;
  61. exports.archive = isArchiveHelper;
  62. exports.year = isYearHelper;
  63. exports.month = isMonthHelper;
  64. exports.category = isCategoryHelper;
  65. exports.tag = isTagHelper;