hexojs / hexo

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

.j2 file in code_dir causing rendering error #5479

Open lotabout opened 2 months ago

lotabout commented 2 months ago

Check List

Expected behavior

.j2 files in code_dir folder should not breaks hexo generate

Actual behavior

hexo generate will fail. (see the reproducing steps)

How to reproduce?

hexo init hexo-test
cd hexo-test

mkdir -p source/downloads/code
touch source/downloads/code/j2-test.j2

echo '{% include_code j2-test.j2 j2-test.j2 %}' >> source/_posts/hello-world.md

hexo clean && hexo generate --debug

Gives the following error:

06:30:54.928 FATAL Something's wrong. Maybe you can find the solution here: https://hexo.io/docs/troubleshooting.html
Template render error: (unknown path)
  TypeError: Cannot read properties of undefined (reading 'split')
    at Object._prettifyError (/Users/jinzhouz/tmp/hexo-test/node_modules/nunjucks/src/lib.js:32:11)
    at /Users/jinzhouz/tmp/hexo-test/node_modules/nunjucks/src/environment.js:464:19
    at eval (eval at _compile (/Users/jinzhouz/tmp/hexo-test/node_modules/nunjucks/src/environment.js:527:18), <anonymous>:11:11)
    at tryCatcher (/Users/jinzhouz/tmp/hexo-test/node_modules/bluebird/js/release/util.js:16:23)
    at Promise._settlePromiseFromHandler (/Users/jinzhouz/tmp/hexo-test/node_modules/bluebird/js/release/promise.js:547:31)
    at Promise._settlePromise (/Users/jinzhouz/tmp/hexo-test/node_modules/bluebird/js/release/promise.js:604:18)
    at Promise._settlePromiseCtx (/Users/jinzhouz/tmp/hexo-test/node_modules/bluebird/js/release/promise.js:641:10)
    at _drainQueueStep (/Users/jinzhouz/tmp/hexo-test/node_modules/bluebird/js/release/async.js:97:12)
    at _drainQueue (/Users/jinzhouz/tmp/hexo-test/node_modules/bluebird/js/release/async.js:86:9)
    at Async._drainQueues (/Users/jinzhouz/tmp/hexo-test/node_modules/bluebird/js/release/async.js:102:5)
    at Async.drainQueues (/Users/jinzhouz/tmp/hexo-test/node_modules/bluebird/js/release/async.js:15:14)
    at process.processImmediate (node:internal/timers:478:21)

Is the problem still there under Safe mode?

no

Your Node.js & npm version

v21.4.0
10.2.4

Your Hexo and Plugin version

hexo-site@0.0.0 /Users/jinzhouz/tmp/hexo-test
├── hexo-generator-archive@2.0.0
├── hexo-generator-category@2.0.0
├── hexo-generator-index@3.0.0
├── hexo-generator-tag@2.0.0
├── hexo-renderer-ejs@2.0.0
├── hexo-renderer-marked@6.3.0
├── hexo-renderer-stylus@3.0.1
├── hexo-server@3.0.0
├── hexo-theme-landscape@1.0.0
└── hexo@7.2.0

Your package.json

{
  "name": "hexo-site",
  "version": "0.0.0",
  "private": true,
  "scripts": {
    "build": "hexo generate",
    "clean": "hexo clean",
    "deploy": "hexo deploy",
    "server": "hexo server"
  },
  "hexo": {
    "version": "7.2.0"
  },
  "dependencies": {
    "hexo": "^7.0.0",
    "hexo-generator-archive": "^2.0.0",
    "hexo-generator-category": "^2.0.0",
    "hexo-generator-index": "^3.0.0",
    "hexo-generator-tag": "^2.0.0",
    "hexo-renderer-ejs": "^2.0.0",
    "hexo-renderer-marked": "^6.0.0",
    "hexo-renderer-stylus": "^3.0.0",
    "hexo-server": "^3.0.0",
    "hexo-theme-landscape": "^1.0.0"
  }
}

Your site's _config.yml (Optional)

No response

Others

No response

D-Sketon commented 2 months ago

j2 becomes html and loses the content field? (Is this normal behaviour?) image And #5121's modification resulted in an undefined exception: https://github.com/hexojs/hexo/blob/093dc2d1c92dd7fdea74bb1ccc2208556316e2a6/lib/plugins/tag/include_code.ts#L57-L58

stevenjoezhang commented 2 months ago

I think setting code_dir to skip render should solve the problem. This should be feasible during the loading process in Hexo.

stevenjoezhang commented 2 months ago

The current situation is more complicated than I thought. Hexo's current database models Page and Asset are not well-suited for expressing "code" files (including not only j2 but also js or css files):

  1. Assets are not rendered, but their content is also not stored in the database (usually used for handling multimedia resources like images, but not plain text files);
  2. Pages are always rendered, even if they are non-markdown files such as the aforementioned j2, js, or css. Typically, users do not want these files to be rendered as pages or converted to other formats (though there are exceptions, such as plugins used for compressing js files).

I have not found a very elegant solution about how to handle files in code_dir.

uiolee commented 1 month ago

This may be related to version. v7.1.1 does not have this problem

uiolee commented 1 month ago

#5121's modification resulted in an undefined exception: https://github.com/hexojs/hexo/blob/093dc2d1c92dd7fdea74bb1ccc2208556316e2a6/lib/plugins/tag/include_code.ts#L57-L58

I guess that's a problem but not a critical one. code_dir itself should not be rendered