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

.j2 file in code_dir causing rendering error #5479

Open
5 tasks done
lotabout opened this issue Apr 22, 2024 · 5 comments
Open
5 tasks done

.j2 file in code_dir causing rendering error #5479

lotabout opened this issue Apr 22, 2024 · 5 comments
Labels
bug Something isn't working

Comments

@lotabout
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

.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
Copy link
Member

j2 becomes html and loses the content field? (Is this normal behaviour?)
image
And #5121's modification resulted in an undefined exception:

let code = doc.content;
const lines = code.split('\n');

@D-Sketon D-Sketon added the bug Something isn't working label Apr 22, 2024
@stevenjoezhang
Copy link
Member

stevenjoezhang commented Apr 22, 2024

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

@stevenjoezhang
Copy link
Member

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

This comment was marked as outdated.

@uiolee
Copy link
Member

uiolee commented May 18, 2024

#5121's modification resulted in an undefined exception:

let code = doc.content;
const lines = code.split('\n');

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

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

No branches or pull requests

4 participants