helpers.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. var get_posts = function(pages){
  2. var posts = []
  3. for (let i = 0; i < pages.data.length; i++) {
  4. const page = pages.data[i];
  5. if(page.layout == "post") posts.push(page);
  6. }
  7. return posts;
  8. }
  9. var get_projects = function(pages){
  10. var projects = [];
  11. pages.forEach(page => {
  12. if(page.layout == "project") projects.push(page);
  13. });
  14. return projects;
  15. }
  16. hexo.extend.helper.register('project_count', function(pages){
  17. return get_projects(pages).length;
  18. });
  19. hexo.extend.helper.register('blog_count', function(pages){
  20. return get_posts(pages).length;
  21. });
  22. hexo.extend.helper.register('is_video_provider', function(url, provider){
  23. url = url.replace('http://', '')
  24. url = url.replace('https://', '')
  25. url = url.replace('www', '')
  26. return url.includes(provider)
  27. });
  28. hexo.extend.helper.register('youtube_embed_link', function(url){
  29. url = url.replace('http://', '')
  30. url = url.replace('https://', '')
  31. url = url.replace('watch?v=', '')
  32. url = url.replace(' ', '')
  33. var parts = url.split('/');
  34. return `https://www.youtube.com/embed/${parts[1]}`;
  35. });
  36. hexo.extend.helper.register('vimeo_embed_link', function(url){
  37. url = url.replace('http://', '')
  38. url = url.replace('https://', '')
  39. url = url.replace('vimeo.com/', '')
  40. return `https://player.vimeo.com/video/${url}`;
  41. });
  42. hexo.extend.helper.register('order_projects', function(pages){
  43. var projects = get_projects(pages);
  44. projects.sort(function(a,b){
  45. return new Date(b.date) - new Date(a.date);
  46. });
  47. return projects;
  48. });
  49. hexo.extend.helper.register('order_posts', function(pages){
  50. var posts = get_posts(pages)
  51. posts.sort(function(a,b){
  52. return new Date(b.date) - new Date(a.date);
  53. });
  54. return posts;
  55. });
  56. hexo.extend.helper.register('next_post', function(current_post, pages){
  57. var posts = get_posts(pages);
  58. posts.sort(function(a,b){
  59. return new Date(b.date) - new Date(a.date);
  60. });
  61. for (let i = 0; i < posts.length; i++) {
  62. if(posts[i].title == current_post.title){
  63. if(i + 1 < posts.length) return posts[i + 1];
  64. else return posts[0];
  65. }
  66. }
  67. });
  68. hexo.extend.helper.register('featured_projects', function(pages){
  69. var projects = get_projects(pages);
  70. projects.sort(function(a,b){
  71. return new Date(b.date) - new Date(a.date);
  72. });
  73. var featured = [];
  74. projects.forEach(project => {
  75. if(project.featured == true) featured.push(project);
  76. });
  77. return featured;
  78. });
  79. hexo.extend.helper.register('tagged', function(pages, search_tag){
  80. var tagged = []
  81. pages.data.forEach(page => {
  82. for (let i = 0; i < page.tags.data.length; i++) {
  83. if(page.tags.data[i].name == search_tag) {
  84. tagged.push(page);
  85. break;
  86. }
  87. }
  88. });
  89. //console.log(`Found ${tagged.length} posts with tag \'${search_tag}\' in ${pages.length} posts`);
  90. return tagged;
  91. });
  92. hexo.extend.helper.register('limit', function(pages, limit){
  93. var list = []
  94. for (let i = 0; i < pages.length; i++) {
  95. if(i < limit) list.push(pages[i]);
  96. }
  97. return list;
  98. });
  99. hexo.extend.helper.register('remove_from_list', function(pages, page){
  100. var list = { data : [], length : 0 }
  101. for (let i = 0; i < pages.length; i++) {
  102. if(pages.data[i].title != page.title) list.data.push(pages.data[i]);
  103. }
  104. list.length = list.data.length;
  105. return list;
  106. });