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

"before_post_render", "after_post_render" executed on non-article page #4468

Closed
5 tasks done
SukkaW opened this issue Aug 8, 2020 · 5 comments · Fixed by #5118
Closed
5 tasks done

"before_post_render", "after_post_render" executed on non-article page #4468

SukkaW opened this issue Aug 8, 2020 · 5 comments · Fixed by #5118
Labels
bug Something isn't working discussion help wanted

Comments

@SukkaW
Copy link
Member

SukkaW commented Aug 8, 2020

Check List

Please check followings before submitting a new issue.

Expected behavior

before_post_render and after_post_render should only executed on posts and pages.

Actual behavior

Non-article files (like css) being processed by before_post_render & after_post_render well.

Is the problem still there under "Safe mode"?

Yes.

Other Information

Add following content to scripts/test.js:

hexo.on('generateBefore', () => {
    hexo.model('Page').toArray().forEach(i => console.log(i.path));
})

And non-article paths will show up.


hexojs/site#1506

cc @curbengh

@SukkaW SukkaW added help wanted question Needs help in usage labels Aug 8, 2020
@stevenjoezhang
Copy link
Member

stevenjoezhang commented Aug 8, 2020

See also #1591 #2675

A possible reason is that all the js and css files in the source folder are judged as pages

process: function assetProcessor(file) {
if (file.params.renderable) {
return processPage(file);
}
return processAsset(file);
}

@SukkaW
Copy link
Member Author

SukkaW commented Aug 8, 2020

Interesting:

$ hexo list page

The list command is also affected.

@jiangtj
Copy link
Member

jiangtj commented Aug 12, 2020

pattern: new Pattern(path => {
if (isExcludedFile(path, ctx.config)) return;
return {
renderable: ctx.render.isRenderable(path) && !isMatch(path, ctx.config.skip_render)
};
}),

renderable is defined in the pattern, maybe we can add some default value for skip_render

@curbengh
Copy link
Contributor

curbengh commented Aug 12, 2020

maybe we can add some default value for skip_render

Tested working. I add a test css in source/css/test.css.

skip_render:
  - '**.css'
// should not show css
hexo.on('generateBefore', () => {
  hexo.model('Page').toArray().forEach(i => console.log(i.path));
})

hexo.extend.filter.register('before_post_render', function(data){
  if (data.source.endsWith('.md') === false) console.log(data.content.substring(0, 50))
  return data;
});

hexo.extend.filter.register('after_post_render', function(data){
  if (data.source.endsWith('.md') === false) console.log(data.content.substring(0, 50))
  return data;
});

after_render:css still works:

// should show css
hexo.extend.filter.register('after_render:css', function(str, data){
  console.log(str.substring(0, 50))
  console.log(data.path)
  return str
});

This approach is not practical, every non-post patterns (**.js, **.css, **.styl, etc) have to be added. Another approach is to include _posts/** only.

@curbengh
Copy link
Contributor

curbengh commented Aug 12, 2020

hexo/lib/hexo/post.js

Lines 245 to 249 in b7d15b9

return promise.then(content => {
data.content = content;
// Run "before_post_render" filters
return ctx.execFilter('before_post_render', data, { context: ctx });
}).then(() => {

I tried:

    return promise.then(content => {
      data.content = content;
      // Run "before_post_render" filters
      if (data.source && data.source.startsWith('_posts/')) return ctx.execFilter('before_post_render', data, { context: ctx });
      return
    }).then(() => {

Then I realised backtick_code_block.js is a before_post_render filter, so if I use that workaround, "backtick_code_block.js" wouldn't execute on page (files outside of "posts/" folder).

To ensure "backtick_code_block.js" still works, we may need a new filter "before_render".


hexo.post.render(source, data);

Looking at the docs, I notice source is optional, so it seems before/after_post_render was never designed to be limited to _posts/ folder only. If that's the case, we can mention in the docs (hexojs/site#1506).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working discussion help wanted
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants