helpers.js 3.4 KB

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