ppoffice / hexo-component-inferno

A collection of Inferno.js layout components and utility scripts for Hexo
https://ppoffice.github.io/hexo-component-inferno/
24 stars 30 forks source link

如何自定义 gitalk 的样式 ? #47

Closed GanZhiXiong closed 2 years ago

GanZhiXiong commented 2 years ago

目前我是在 themes/icarus/source/css/ 目录下创建一个 gitalk.css 文件,然后修改 gitalk.js 中的 cssUrl 的值为 /css/gitalk.css,来实现的。但是这样得修改依赖包。

还有没有其他不修改依赖包的方法。

ppoffice commented 2 years ago

https://hexo.io/zh-cn/api/injector.html

GanZhiXiong commented 2 years ago

通过 注入器(Injector) | Hexo 使用自定义的 gitalk.css,无论注入到哪个位置,都无法实现完全使用自定义的 gitalk.css,其主要原因是页面依旧会从 cdn 获取的原 gitalk.css,自定义的 gitalk.css 和原 gitalk.css同时在页面中存在,导致 css 样式可能还会从原 gitalk.css 中取。

以我想将 gitalk 的样式修为和我主题 dark 模式相匹配为例。

注入代码如下:

const css = hexo.extend.helper.get('css').bind(hexo);

// hexo.extend.injector.register('head_begin', () => {
//     return css('/css/gitalk.css');
// });

// hexo.extend.injector.register('head_end', () => {
//     return css('/css/gitalk.css');
// });

// hexo.extend.injector.register('body_begin', () => {
//     return css('/css/gitalk.css');
// });

hexo.extend.injector.register('body_end', () => {
    return css('/css/gitalk.css');
});

而修改 gitalk.js 中的 cssUrl 的值为 /css/gitalk.css后,才是我最终想要的效果: https://github.com/ppoffice/hexo-component-inferno/blob/2d28bb09109e73e5b5e7806ff7cf59627b2602a4/src/view/comment/gitalk.jsx#L137

image-20220525002058727

GanZhiXiong commented 2 years ago

因此我打算发起一个 PR,在 PR 将在 _config.icarus.yml 中的 comment 下添加一个 cssUrl`,用于配置自定义的 CSS 文件。

其主要作用:

  1. 即使使用注入器也可能导致还会使用原 CSS 的样式。
  2. 使用注入器,并不能避免从 CDN 中获取原 CSS 文件,不仅浪费了流量,而且 CDN 可能较慢,影响页面加载。
GanZhiXiong commented 2 years ago

大佬请审核下,如果可以的话,我将发起一个 PR。

ppoffice commented 2 years ago

Icarus现在的设计满足大多数普通人的使用需求,对于这种进阶的自定义,建议自行修改代码来完成。

GanZhiXiong commented 2 years ago

好的