list_archives.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. 'use strict';
  2. function listArchivesHelper(options = {}) {
  3. const config = this.config;
  4. const archiveDir = config.archive_dir;
  5. const timezone = config.timezone;
  6. const lang = this.page.lang || this.page.language || config.language;
  7. let format = options.format;
  8. const type = options.type || 'monthly';
  9. const style = options.hasOwnProperty('style') ? options.style : 'list';
  10. const showCount = options.hasOwnProperty('show_count') ? options.show_count : true;
  11. const transform = options.transform;
  12. const separator = options.hasOwnProperty('separator') ? options.separator : ', ';
  13. const className = options.class || 'archive';
  14. const order = options.order || -1;
  15. let result = '';
  16. const self = this;
  17. if (!format) {
  18. format = type === 'monthly' ? 'MMMM YYYY' : 'YYYY';
  19. }
  20. const posts = this.site.posts.sort('date', order);
  21. if (!posts.length) return result;
  22. const data = [];
  23. let length = 0;
  24. posts.forEach(post => {
  25. // Clone the date object to avoid pollution
  26. let date = post.date.clone();
  27. if (timezone) date = date.tz(timezone);
  28. if (lang) date = date.locale(lang);
  29. const year = date.year();
  30. const month = date.month() + 1;
  31. const name = date.format(format);
  32. const lastData = data[length - 1];
  33. if (!lastData || lastData.name !== name) {
  34. length = data.push({
  35. name,
  36. year,
  37. month,
  38. count: 1
  39. });
  40. } else {
  41. lastData.count++;
  42. }
  43. });
  44. function link(item) {
  45. let url = `${archiveDir}/${item.year}/`;
  46. if (type === 'monthly') {
  47. if (item.month < 10) url += '0';
  48. url += `${item.month}/`;
  49. }
  50. return self.url_for(url);
  51. }
  52. let item, i, len;
  53. if (style === 'list') {
  54. result += `<ul class="${className}-list">`;
  55. for (i = 0, len = data.length; i < len; i++) {
  56. item = data[i];
  57. result += `<li class="${className}-list-item">`;
  58. result += `<a class="${className}-list-link" href="${link(item)}">`;
  59. result += transform ? transform(item.name) : item.name;
  60. result += '</a>';
  61. if (showCount) {
  62. result += `<span class="${className}-list-count">${item.count}</span>`;
  63. }
  64. result += '</li>';
  65. }
  66. result += '</ul>';
  67. } else {
  68. for (i = 0, len = data.length; i < len; i++) {
  69. item = data[i];
  70. if (i) result += separator;
  71. result += `<a class="${className}-link" href="${link(item)}">`;
  72. result += transform ? transform(item.name) : item.name;
  73. if (showCount) {
  74. result += `<span class="${className}-count">${item.count}</span>`;
  75. }
  76. result += '</a>';
  77. }
  78. }
  79. return result;
  80. }
  81. module.exports = listArchivesHelper;