-
-
Notifications
You must be signed in to change notification settings - Fork 4.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
plugin issue: Tags removed by setTags
in before_post_render filter still exists in the tag list but the page is not generated
#5380
Comments
I guess I can run this code once by myself to solve the problem for now. Here is my solution code, but I'm not entirely know how it works. When I use only the hexo.extend.filter.register('after_post_render', filterSiteTag);
hexo.locals.set('tags', reloadTag);
let _filterSiteTagOnce = false;
function filterSiteTag(data) {
if (!_filterSiteTagOnce) {
_filterSiteTagOnce = true;
this.locals.set('tags', reloadTag);
}
return data;
}
function reloadTag() {
const logger = hexo.log;
// Ignore tags with zero posts
const model = hexo.database.model('Tag');
const filtered = model.filter(tag => tag.length);
logger.info('Tag size: ' + model.length + ' -> ' + filtered.length);
return filtered;
} |
I've run into a similar problem when I tried to remove posts dynamically in filters. Here's some code to demonstrate it: // FILE: scripts/test.js
hexo.extend.filter.register("before_generate", function () {
this._bindLocals();
// Delete the "hello-world" post which has the "removed" tag.
const posts = this.locals.get("posts");
this.locals.set("posts", posts.filter((post) => post.slug !== "hello-world"));
// The "removed" tag should contain zero posts now, since there is no post tagged with "removed".
const tags = this.locals.get("tags");
const removedTag = tags.findOne({ name: "removed" });
// The behavior in Hexo 7.0:
console.log(removedTag.length); // 1
// Which equals to:
console.log(this.database.model('PostTag').find({ tag_id: removedTag._id }).length); // 1
// The behavior in Hexo 6.3:
console.log(removedTag.posts.length); // 0
// To workaround it, undoing the change introduced in #5119 could help:
// this.locals.set("tags", () => {
// return this.database.model("Tag").filter((tag) => {
// return tag.posts.length;
// });
// });
}); I would like to know if this is working as intended or not. Should we use some other methods to manipulate posts/tags/categories in filters? |
I guess 5119 is necessary, it does reduce traversal. Maybe hexo needs a better way to deal with idle tags and categories. Or, announce to users that if they want to use accurate tags and categories list, they should filter the list. However, revert is a temporary solution, too. |
Check List
hexo version
to check)Expected behavior
I need to change the posts' tags with my own filter. I find out the
setTags
method when using hexo 6.3.0 and it always works before I upgrade to hexo 7.0.This is an example code:
In this case, some of the tags are removed completely from my website. I expect those tags to disappear.
I test this problem in my repo branch.
Actual behavior
The tag list remain the removed tags and I can not jump into the tag's page(the page was not generated).
How to reproduce?
simply a post with tag and run with the example filter code above (using the
setTags
method to remove tag(s) in before_post_render filter).Is the problem still there under
Safe mode
?this is a plugin usage problem, and I disabled my theme and my other plugins, I just using the example code, the problem still there.
Your Node.js & npm version
Your Hexo and Plugin version
Your
package.json
Others
No response
The text was updated successfully, but these errors were encountered: