iissnan / hexo-theme-next

Elegant theme for Hexo.

Home Page:http://notes.iissnan.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Theme configurations using Hexo data files

iissnan opened this issue · comments

commented

Currently, it is not smooth to update NexT theme from pulling or downloading new releases. It is quite often running into conflict status when updating NexT theme via git pull, or need to merge configurations manually when upgrading to new releases.

At present, NexT encourages users to store some options in site's _config.yml and other options in theme's _config.yml. This approach is applicable, but has some drawbacks:

  1. Configurations are splited into two pieces
  2. Users maybe confuse which place should be for options

In order to resolve this issue, NexT will take advantage of Hexo Data files. Because Data files is introduced in Hexo 3, so you need upgrade Hexo to 3.0 (or above) to use this feature.

If you prefer Hexo 2.x, you can still use the old approach for configurations. NexT is still compatible with Hexo 2.x.

Benefits

With this feature, now you can put all your configurations into one place (source/_data/next.yml), you don't need to touch next/_config.yml. If there are any new options in new releases, you just need to copy those options from next/_config.yml, paste into _data/next.yml and set their values to whatever you want.

How to use this feature

  1. Please ensure you are using Hexo 3 (or above)
  2. Create an file named next.yml in site's source/_data directory (create _data directory if it did not exist)
  3. Copy NexT theme options both in site's _config.yml and theme's _config.yml into next.yml.

目前升级 NexT 主题的时候并不是非常的流畅。若使用 git pull 的方式,很多时候可能会产生冲突;而下载新版本覆盖安装的方式又需要手动合并主题的 _config.yml 文件。

在此修改之前, NexT 建议将配置分离,一部分在 站点的配置文件中,另外一部分在主题的配置文件中。将需要自定的选项放置在 站点配置文件中,从而脱离避免更新主题时可能遇到的麻烦。这种方式是可行,但是有一些缺点:

  1. 配置分离成了两个部分
  2. 用户可能会疑惑一些选项该放置在哪里比较合适

为了解决这个问题, NexT 将会使用 Hexo 的 Data Files 。然而由于 Data Files 是在 Hexo 3 版本时引进的,所以要使用这个特性,需要 Hexo 的版本不低于 3。

若你比较喜欢 Hexo 2.x 版本,可以继续使用原先的配置方式。 NexT 保持着向下兼容。

特性

通过这个特性,你可以将所有的主题配置放置在站点的 source/_data/next.yml 文件中。原先放置在 站点配置文件 中的选项可以迁移到新的位置,同时,主题配置文件可以不用做任何修改。若后续版本有配置相关的改动时,你仅需在 next.yml 中做相应调整即可。

使用

  1. 请先确保你所使用的 Hexo 版本在 3 以上
  2. 在站点的 source/_data 目录下新建 next.yml 文件(_data目录可能需要新建)
  3. 迁移站点配置文件和主题配置文件中的配置到 next.yml

也就是说可以把自己的一些私人定制的配置直接剪切 到 source/_data/next.yml 中来么

commented

是的,所有主题相关的配置都可以写到 next.yml 文件中。 主题目录下的 _config.yml 可以不用做任何修改。

  1. 若你是新安装的,可以将主题的 _config.yml 里面的内容整个拷贝过去,然后修改即可
  2. 若是之前有将配置写在站点的 _config.yml 里,把这个文件里主题相关的配置剪切到 next.yml 里去;同时将主题下 _config.yml 里面的配置也拷贝过去,相当于是全部合并到 next.yml 中去了。

嗯,这个想法还是挺不错的。改天我升级试一下。

commented

这个方法好,谢了。

commented

有一个缺点:每次修改 _next.yml 需要重启 hexo server 才能生效。

用这种方法真的太方便了,除了iissnan说的那个缺点以外,其他都是好处啊。
把头像也放在站点source目录下,这样(如果不对主题做什么自定义的话)整个next目录都不需要修改了,后面如果主题有什么更新,可以放心地直接删掉旧的next目录,直接下载一个最新的。

commented

嗯,这种方式还是可以适用很大范围的用户。如果自定义部分比较复杂,就需要 fork 一份

commented

@iissnan 这个方案有缺陷:在主题的 _config.yml 里面打开的选项,无法在 source/_data/next.yml 中关闭。

比如,主题的 _config.yml 中默认开启 tags menu;这样,即使在 source/_data/next.yml 中关闭 tags menu,最终结果依然有,因为只有当后者也开启某项时,其值才能覆盖前者。

如何能让 hexo 放弃主题的中 _config.yml 呢?我试了,即使指定配置文件:hexo s --config source/_data/next.yml,依然会读取主题的中 _config.yml

有点没看明白,这样做的话如果更新后不还是要自己手动配置吗?

啊,原来还要把override设置为true,很方便!

我用临时构建git主题仓库的方式,感觉还好,写了篇分享。
https://zhangnai.xin/2018/11/11/hexo-theme-upgrade/