全局更改图标

这是一种全局更改 Discourse 图标的简便方法。

  1. 右键单击要更改的图标,然后选择“检查元素”或“检查”(具体取决于浏览器)。

  2. 找到图标名称

  3. 在此处搜索新图标 Find Icons with the Perfect Look & Feel | Font Awesome external-link-alt

  4. admin > customize > themes > edit code -> JS 选项卡中自定义并添加代码

// {theme}/javascripts/discourse/api-initializers/init-theme.gjs

import { apiInitializer } from "discourse/lib/api";

export default apiInitializer((api) => {
  api.replaceIcon("link", "external-link-tab");
});
  1. 如果 Discourse 默认未使用某些图标,则必须在站点设置 svg icon subset 中添加它们,然后强制刷新浏览器以查看更改。
    结果:
    image
    所有“link”图标都将替换为“external-link-tab”。
    如果某个图标在其他页面的多个元素中使用(例如徽章),则该图标也会在这些地方被替换。

例外情况

_请注意,已经有一个主题组件允许您更改“点赞”图标。我在这里以这种情况为例。

用于“点赞”的“heart”图标是硬编码的,并带有其他名称('d-liked''d-unliked'),因此其处理方式应与其他图标不同。要将 :heart: 图标更改为 :+1: 图标:

api.replaceIcon("d-liked", "thumbs-up");
api.replaceIcon("d-unliked", "thumbs-o-up");

like
firefox_2018-04-24_18-37-02
但在徽章页面上,图标仍然是“heart”:


因此,要在该页面上更改它,请添加:

api.replaceIcon("heart", "thumbs-up");

另一个示例:

api.replaceIcon("d-watching", "eye");

更改了“关注”图标:

在此处查看其他涵盖跟踪状态、展开/折叠、通知和点赞的例外情况。
const REPLACEMENTS = {
  "d-tracking": "bell",
  "d-muted": "discourse-bell-slash",
  "d-regular": "far-bell",
  "d-watching": "discourse-bell-exclamation",
  "d-watching-first": "discourse-bell-one",
  "d-drop-expanded": "caret-down",
  "d-drop-collapsed": "caret-right",
  "d-unliked": "far-heart",
  "d-liked": "heart",
  "d-post-share": "link",
  "d-topic-share": "link",
  "notification.mentioned": "at",
  "notification.group_mentioned": "users",
  "notification.quoted": "quote-right",
  "notification.replied": "reply",
  "notification.posted": "reply",
  "notification.edited": "pencil-alt",
  "notification.bookmark_reminder": "discourse-bookmark-clock",
  "notification.liked": "heart",
  "notification.liked_2": "heart",
  "notification.liked_many": "heart",
  "notification.liked_consolidated": "heart",
  "notification.private_message": "far-envelope",
  "notification.invited_to_private_message": "far-envelope",
  "notification.invited_to_topic": "hand-point-right",
  "notification.invitee_accepted": "user",
  "notification.moved_post": "sign-out-alt",
  "notification.linked": "link",
  "notification.granted_badge": "certificate",
  "notification.topic_reminder": "far-clock",
  "notification.watching_first_post": "discourse-bell-one",
  "notification.group_message_summary": "users",
  "notification.post_approved": "check",
  "notification.membership_request_accepted": "user-plus",
  "notification.membership_request_consolidated": "users",
  "notification.reaction": "bell",
  "notification.votes_released": "plus",
  "notification.chat_quoted": "quote-right",
};

参考:discourse/icon-library.js at main · discourse/discourse (github.com)


欢迎创建其他主题组件,并在我们的 #theme-component 类别中分享!


本文档受版本控制 - 请在 github 上建议更改。

38 个赞

Your post does help in changing icons globally but how to change the share icon in posts only? I do not want to change the link icon in the editor but, only in the posts. I checked the other posts in the forum but none of those work in the current build of Discourse, thus I am asking it here.

1 个赞

thanks for the clear guide. a small point:

is it possible to change d-watching to fas fa-eye and d-tracking to far fa-eye together?

if yes, how would one add the fas icon to the svg; as I tried adding fa-eye and this header:

<script type="text/discourse-plugin" version="0.8">
api.replaceIcon('d-watching', 'fa-eye');
api.replaceIcon('d-tracking', 'eye');
</script>

but the watching icon is not working.


also I was checking other icons such as bells, and I see even when I add bells to the svg icon subset it’s not loaded:

is there anything else I should do in order to be able to use bells?

Bells is a Pro icon, so it can’t be included, our default FA5 implementation only covers the free icons.

The solid eye icon is a bit of a special case: the FontAwesome 4 eye icon was moved to the regular bundle, so in Discourse we have a matcher for it that converts ‘eye’ to ‘far-eye’. Unfortunately, this stops you from using the solid ‘eye’ icon in Discourse, because Discourse will redirect eye to far-eye. If you absolutely must use that icon, you can copy the svg into a custom icon that you can rename to something of your liking, and add that to a theme or plugin.

3 个赞

thanks for the complete information :+1: , meanwhile I’ve tried several other icons, e.g. user-shield.

here is the header:
<script type="text/discourse-plugin" version="0.8">
api.replaceIcon('d-watching', 'fa-user-shield');
</script>

and I added fa-user-shield to the svg icon subset, but it still shows empty icon.

Try without the “fa-” prefix for user-shield (both in the JS code and the site setting).

still empty (tried with flag for easier snapshot):

That works locally for me. Did you refresh the page after making these changes?

yes, refreshed with F5 and ctrl+R just to be sure!

is there anyway to look for possible errors?

If you are familiar with the rails console, I would try logging in to the console and running SvgSprite.expire_cache and then refreshing the page to see if that does it.

2 个赞

How to change the solid (fas fa-bookmark) to regular version (far fa-bookmark)?

@dax Is there a way to change the default heart icon to an emoji icon?

1 个赞

Currently, no, you can only replace svg icons with other svg icons, not emojis.

4 个赞

我们能否使用自定义的 SVG 图标,而不仅仅是 Font Awesome 图标?

是的,详情请见 https://meta.discourse.org/t/introducing-font-awesome-5-and-svg-icons/101643。

2 个赞

谢谢!我还找到了这篇帖子:在主题中用自定义图标替换 Discourse 的默认 SVG 图标

最终,我直接将 SVG 精灵嵌入到 Header 文件中,因为按照那篇文章所述上传并重命名对我无效(当然,也可能是我操作有误)。

所以我的 Header 是这样的:

<svg width="0" height="0" class="hidden" style="display: none;">
    <symbol id="myicon-search" ..... </symbol>  
    <!-- 我所有的符号都在这里... -->
</svg>
<script type="text/discourse-plugin" version="0.8">
    api.replaceIcon('search', 'myicon-search'); 
</script>

这样管理起来很方便。

我使用 https://svgsprit.es 上的 SVG 精灵生成器来确保格式正确。

3 个赞

我们可以使用微软的 Fabric 图标 配合此方法吗?

是的,您应该可以这样做。我没有看到这些图标的官方 SVG 精灵分发版本,但根据 https://github.com/OfficeDev/office-ui-fabric-core/issues/1008,您可以使用它们的名称逐个自行提取 SVG 文件。

4 个赞

我也在尝试做同样的事情。我已经成功更改了帖子中的“分享”图标,但发现它同时也更改了编辑器中的“链接”图标。有什么办法可以只更改帖子的图标吗?

3 个赞

有没有办法将 FontAwesome 图标全局替换为在主题上传部分上传的 SVG 图标?我正在开发一个网站,希望将 discourse-solved 插件中标题已解决时显示的 FontAwesome 图标替换为我网站的标志。我很难直接通过插件代码来实现这一修改,想知道通过头部(header)方法是否可行。