Skip to content
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

Filters do not allow adding new properties to posts #5518

Open
5 tasks done
andrewmoscardino-awh opened this issue Jul 12, 2024 · 3 comments
Open
5 tasks done

Filters do not allow adding new properties to posts #5518

andrewmoscardino-awh opened this issue Jul 12, 2024 · 3 comments

Comments

@andrewmoscardino-awh
Copy link

Check List

  • I have already read Docs page & Troubleshooting page.
  • I have already searched existing issues and they are not help to me.
  • I examined error or warning messages and it's difficult to solve.
  • I am using the latest version of Hexo. (run hexo version to check)
  • My Node.js is matched the required version.

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

[email protected] /Users/andrew/Projects/moscardino.net
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
└── [email protected]

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
Copy link
Author

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
Copy link
Author

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.

@uiolee
Copy link
Member

uiolee commented Oct 7, 2024

could not reproduce. it works in my test

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants