hexojs / hexo

A fast, simple & powerful blog framework, powered by Node.js.
https://hexo.io
MIT License
39k stars 4.79k forks source link

Filters do not allow adding new properties to posts #5518

Open andrewmoscardino-awh opened 3 weeks ago

andrewmoscardino-awh commented 3 weeks ago

Check List

Expected behavior

I have a filter on before_post_render. Previously with Hexo 6.x, I was able to add a property to the post like so:

hexo.extend.filter.register('before_post_render', function (post) {
    post.has_code_block = post.raw.includes('```');

    return post;
});

And then the new property, has_code_block in this case, would be usable in my templates. With Hexo 7, the new property isn't available in the templates. Modifying an existing property works as expected, though.

Actual behavior

The new property is not available within templates. Updated properties are available in templates with their updated values.

How to reproduce?

  1. Create a filter that adds a property to the post object.
  2. Try to access that new property from a template

Is the problem still there under Safe mode?

n/a

Your Node.js & npm version

v18.17.1
10.2.4

Your Hexo and Plugin version

moscardino.net@1.0.0 /Users/andrew/Projects/moscardino.net
├── hexo-autoprefixer@2.0.0
├── hexo-canonical-link@2.0.0
├── hexo-filter-nofollow@2.0.2
├── hexo-generator-alias@1.0.0
├── hexo-generator-archive@2.0.0
├── hexo-generator-feed@3.0.0
├── hexo-generator-index@4.0.0
├── hexo-generator-sitemap@3.0.1
├── hexo-generator-tag@2.0.0
├── hexo-log@4.1.0
├── hexo-renderer-ejs@2.0.0
├── hexo-renderer-markdown-it@7.1.1
├── hexo-server@3.0.0
├── hexo-toc@1.1.0
├── hexo@7.3.0
├── js-yaml@4.1.0
├── lunr@2.3.9
├── sharp@0.33.4
└── striptags@3.2.0

Your package.json

{
  "name": "moscardino.net",
  "version": "1.0.0",
  "private": true,
  "scripts": {
    "build": "hexo clean && hexo g -f",
    "clean": "hexo clean",
    "serve": "hexo clean && hexo s -o"
  },
  "hexo": {
    "version": "7.3.0"
  },
  "dependencies": {
    "hexo": "^7.3.0",
    "hexo-autoprefixer": "^2.0.0",
    "hexo-canonical-link": "^2.0.0",
    "hexo-filter-nofollow": "^2.0.2",
    "hexo-generator-alias": "^1.0.0",
    "hexo-generator-archive": "^2.0.0",
    "hexo-generator-feed": "^3.0.0",
    "hexo-generator-index": "^4.0.0",
    "hexo-generator-sitemap": "^3.0.1",
    "hexo-generator-tag": "^2.0.0",
    "hexo-log": "^4.1.0",
    "hexo-renderer-ejs": "^2.0.0",
    "hexo-renderer-markdown-it": "^7.1.1",
    "hexo-server": "^3.0.0",
    "hexo-toc": "^1.1.0",
    "js-yaml": "^4.1.0",
    "lunr": "^2.3.9",
    "sharp": "^0.33.4",
    "striptags": "^3.2.0"
  }
}

Your site's _config.yml (Optional)

No response

Others

No response

andrewmoscardino-awh commented 3 weeks ago

I should also note that these behave the same:

hexo.extend.filter.register('before_post_render', function (post) {
    post.has_code_block = post.raw.includes('```');

    return post;
});
hexo.extend.filter.register('before_post_render', function (post) {
    return {
        ...post,
        has_code_block: post.raw.includes('```')
    };
});
andrewmoscardino-awh commented 2 weeks ago

Ok, after some more tinkering, I've found that this works:

hexo.extend.filter.register('before_post_render', function (post) {
    post["has_code_block"] = post.raw.includes('```');

    return post;
});

That's very strange.