Skip to content

Commit

Permalink
fix: generation fail with no tags #1
Browse files Browse the repository at this point in the history
  • Loading branch information
bennyxguo committed Aug 2, 2023
1 parent e24ef72 commit 3b17b95
Showing 1 changed file with 31 additions and 32 deletions.
63 changes: 31 additions & 32 deletions lib/generators/tag.js
Original file line number Diff line number Diff line change
@@ -1,35 +1,34 @@
const pagination = require('hexo-pagination')
const {
tagMapper,
tagPageMapper,
postListMapper
} = require('../helpers/mapper')
const pagination = require('hexo-pagination');
const { tagMapper, tagPageMapper, postListMapper } = require('../helpers/mapper');

class TagGenerator {
data = []
posts = []
configs = {}
data = [];
posts = [];
configs = {};

constructor(tags, posts, configs) {
this.data = tags
this.posts = posts
this.configs = configs
this.reduceTags()
for(let cat of this.data) {
cat.data.postlist.sort(function (a, b) {
return a.date< b.date? 1 : -1;
})
this.data = tags ?? [];
this.posts = posts;
this.configs = configs;

if (this.data.length < 1) return;

this.reduceTags();
for (let tag of this.data) {
tag.data.postlist.sort(function (a, b) {
return a.date < b.date ? 1 : -1;
});
}
}

reduceTags() {
if (this.count() <= 0) return
const tags = this.data
const posts = this.posts
const configs = this.configs
if (this.count() <= 0) return;
const tags = this.data;
const posts = this.posts;
const configs = this.configs;

this.data = tags.reduce(function (result, item) {
if (!item.length) return result
if (!item.length) return result;

return result.concat(
pagination(item.path, posts, {
Expand All @@ -40,34 +39,34 @@ class TagGenerator {
count: item.posts.length,
path: 'api/tags/' + item.slug + '.json',
postlist: item.posts.map((post) => {
return postListMapper(post, configs)
return postListMapper(post, configs);
})
}
})
)
}, [])
);
}, []);
}

addTags(data) {
if (this.count <= 0) {
if (this.count() <= 0) {
data.push({
path: 'api/tags.json',
data: JSON.stringify([])
})
});
} else {
data.push({
path: 'api/tags.json',
data: JSON.stringify(this.data.map(tagMapper))
})
data = data.concat(this.data.map(tagPageMapper))
});
data = data.concat(this.data.map(tagPageMapper));
}

return data
return data;
}

count() {
return this.data.length
return this.data.length;
}
}

module.exports = TagGenerator
module.exports = TagGenerator;

0 comments on commit 3b17b95

Please sign in to comment.