hexojs / hexo

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

在 PUG 使用 each 时会同时获取 hexo 目录下主题配置文件和主题目录下配置文件的配置数组值 #5296

Closed Tikas closed 1 year ago

Tikas commented 1 year ago

Check List

Please check followings before submitting a new issue.

Expected behavior

原文:

当在 hexo 目录内使用 _config.[theme].yml 时,理论上使用 each 遍历 YAML 的数组时只会获取到 _config.[theme].yml 的值。

====

translate:

When using _config.[theme].yml within the Hexo directory, theoretically, when using each to iterate over an array in YAML, it should only retrieve the values from _config.[theme].yml.

Actual behavior

原文:

实际输出数组时会连同主题的 _config.yml 值一起输出,等于是输出了 _config.[theme].yml 和主题内的 _config.yml 两个值总输出。

====

translate:

In actual output, the values from both _config.[theme].yml and _config.yml in the theme will be output together when iterating over the array. This means that both values are included in the final output.

How to reproduce?

原文:

当在 hexo 目录内使用 _config.[theme].yml 时,理论上此配置文件的优先级会高于主题里的 _config.yml,我在使用 pug 开发主题时,使用遍历会把 hexo 目录内的 _config.[theme].yml 和主题目录内的 _config.yml 配置一起读取。

具体代码如下:

====

translate:

When using _config.[theme].yml within the Hexo directory, theoretically, the priority of this configuration file should be higher than the _config.yml in the theme. However, when developing a Pug theme, the iteration will read both _config.[theme].yml in the Hexo directory and _config.yml in the theme directory together.

Here is the specific code:

====

hexo > _config.[theme].yml

test:
  - a
  - b
  - c

theme > _config.yml

test:
  - d
  - e
  - f

pug

each item, index in theme.test
  =item
  br

html

a
<br />
b
<br />
c
<br />
d
<br />
e
<br />
f
<br />

Is the problem still there under "Safe mode"?

我尝试过使用命令进入安全模式,由于没学习 js ,网上也找不到相关的教程教我如何使用安全模式,摸索了半天之后,放弃了安全模式。

Environment & Settings

Node.js & npm version(node -v && npm -v)

node v18.16.0
npm 9.5.1
pnpm 8.7.6

Your site _config.yml (Optional)

Hexo and Plugin version(npm ls --depth 0)

├── hexo-generator-archive@2.0.0 -> .\node_modules\.pnpm\hexo-generator-archive@
2.0.0\node_modules\hexo-generator-archive
├── hexo-generator-category@2.0.0 -> .\node_modules\.pnpm\hexo-generator-categor
y@2.0.0\node_modules\hexo-generator-category
├── hexo-generator-index@3.0.0 -> .\node_modules\.pnpm\hexo-generator-index@3.0.
0\node_modules\hexo-generator-index
├── hexo-generator-tag@2.0.0 -> .\node_modules\.pnpm\hexo-generator-tag@2.0.0\no
de_modules\hexo-generator-tag
├── hexo-renderer-marked@6.1.1 -> .\node_modules\.pnpm\hexo-renderer-marked@6.1.
1\node_modules\hexo-renderer-marked
├── hexo-renderer-pug@3.0.0 -> .\node_modules\.pnpm\hexo-renderer-pug@3.0.0\node
_modules\hexo-renderer-pug
├── hexo-renderer-stylus@3.0.0 -> .\node_modules\.pnpm\hexo-renderer-stylus@3.0.
0\node_modules\hexo-renderer-stylus
├── hexo-server@3.0.0 -> .\node_modules\.pnpm\hexo-server@3.0.0\node_modules\hex
o-server
└── hexo@6.3.0 -> .\node_modules\.pnpm\hexo@6.3.0\node_modules\hexo

Your package.json package.json

Others

原文:

我尝试过在全新的 hexo 里去测试,依旧会存在此问题。

但是,不管是新的 hexo 环境,还是在原来的 hexo 里,使用有指定项的数组,都不会出现此问题。

具体请看以下示例:

====

translate:

I have tried testing this issue in a fresh Hexo environment, and the problem still persists.

However, whether it is in a new Hexo environment or the existing one, when using an array with specified items, the issue does not occur.

Please refer to the following example for more details:

====

hexo > _config.[theme].yml

test:
  - text: a
  - text: b
  - text: c

theme > _config.yml

test:
  - text: d
  - text: e
  - text: f

pug

each item, index in theme.test
  =item.text
  br

html

a
<br />
b
<br />
c
<br />
stevenjoezhang commented 1 year ago

这是目前使用的 deepMerge 库的默认行为。为避免问题,请将 _config.[theme].yml 中的相关内容注释掉。

Tikas commented 1 year ago

非常感谢,看来我得全改主题的配置,用户会更依赖 _config.[theme].yml 中的内容,感谢 @stevenjoezhang 告知此问题的原因

这个问题已经困扰了我一天了,主要是反复测试此问题导致的,毕竟提问前我得先看看能不能找到办法解决问题。

非常感谢!