全局更改图标

\n更改 Discourse 图标的全局设置的简单方法。\n\n1. 右键单击要更改的图标,然后选择“检查元素”或“检查”(取决于浏览器)\n\n2. 查找图标名称\n image\n3. 在此处搜索新图标 https://fontawesome.com/icons?d=gallery,例如 external-link-alt\n\n4. 在 admin > customize > themes > edit code - > JS 选项卡中自定义并添加代码\n\ngjs\n// {theme}/javascripts/discourse/api-initializers/init-theme.gjs\n\nimport { apiInitializer } from \"discourse/lib/api\";\n\nexport default apiInitializer((api) => {\n api.replaceIcon(\"link\", \"external-link-tab\");\n});\n\n\n5. 默认情况下未被 Discourse 使用的图标必须添加到站点设置 svg icon subset 中,然后强制刷新浏览器以查看更改生效。\n 结果:\n image\n 所有“link”图标都将替换为“external-link-tab”。\n 如果一个图标在其他页面上被多个元素使用,例如徽章,该图标也会在那里被替换。\n\n—\n\n## 例外情况\n\n_请注意,已经有一个主题组件允许您更改“赞”图标。我将此用作示例_\n\n用于点赞的“heart”图标与其它名称('d-liked''d-unliked')硬编码在一起,应与其它图标区别对待,因此要将 :heart: 图标更改为 :+1: 图标:\n\njs\napi.replaceIcon(\"d-liked\", \"thumbs-up\");\napi.replaceIcon(\"d-unliked\", \"thumbs-o-up\");\n\n\nlike\nfirefox_2018-04-24_18-37-02\n但在徽章页面上,图标仍然是“heart”:\nfirefox_2018-04-24_18-38-15\n因此,要在该页面上更改它,我们添加:\n\njs\napi.replaceIcon(\"heart\", \"thumbs-up\");\n\n\nfirefox_2018-04-24_18-47-50\n\n另一个例子:\n\njs\napi.replaceIcon(\"d-watching\", \"eye\");\n\n\n更改“watching”图标:\nwatching-original watching\n[details="在此处查看涵盖跟踪状态、展开/折叠、通知和点赞的其他例外情况。"] \n\njs\nconst REPLACEMENTS = {\n \"d-tracking\": \"bell\",\n \"d-muted\": \"discourse-bell-slash\",\n \"d-regular\": \"far-bell\",\n \"d-watching\": \"discourse-bell-exclamation\",\n \"d-watching-first\": \"discourse-bell-one\",\n \"d-drop-expanded\": \"caret-down\",\n \"d-drop-collapsed\": \"caret-right\",\n \"d-unliked\": \"far-heart\",\n \"d-liked\": \"heart\",\n \"d-post-share\": \"link\",\n \"d-topic-share\": \"link\",\n \"notification.mentioned\": \"at\",\n \"notification.group_mentioned\": \"users\",\n \"notification.quoted\": \"quote-right\",\n \"notification.replied\": \"reply\",\n \"notification.posted\": \"reply\",\n \"notification.edited\": \"pencil-alt\",\n \"notification.bookmark_reminder\": \"discourse-bookmark-clock\",\n \"notification.liked\": \"heart\",\n \"notification.liked_2\": \"heart\",\n \"notification.liked_many\": \"heart\",\n \"notification.liked_consolidated\": \"heart\",\n \"notification.private_message\": \"far-envelope\",\n \"notification.invited_to_private_message\": \"far-envelope\",\n \"notification.invited_to_topic\": \"hand-point-right\",\n \"notification.invitee_accepted\": \"user\",\n \"notification.moved_post\": \"sign-out-alt\",\n \"notification.linked\": \"link\",\n \"notification.granted_badge\": \"certificate\",\n \"notification.topic_reminder\": \"far-clock\",\n \"notification.watching_first_post\": \"discourse-bell-one\",\n \"notification.group_message_summary\": \"users\",\n \"notification.post_approved\": \"check\",\n \"notification.membership_request_accepted\": \"user-plus\",\n \"notification.membership_request_consolidated\": \"users\",\n \"notification.reaction\": \"bell\",\n \"notification.votes_released\": \"plus\",\n \"notification.chat_quoted\": \"quote-right\",\n};\n\n\n参考:discourse/icon-library.js at main · discourse/discourse (github.com)\n[/details]\n\n—\n\n欢迎在我们的 Theme component 类别中创建其他主题组件并分享!\n\n\n—\n\n此文档受版本控制 - 在 github 上 建议更改。\n\n"

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:

image

image

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):

image

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)?

https://fontawesome.com/icons/bookmark?style=regular

@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 个赞

Is it possible to change to custom SVG icons we add, rather than just FA icons?

Yes, see Introducing Font Awesome 5 and SVG icons for details.

2 个赞

Thank you! I also found Replace Discourse’s default SVG icons with custom icons in a theme

I ended up embedding the SVG sprite directly into the Header file, as uploading it and naming it as that article says to do did not work for me (could be operator error, of course).

So my Header is:

<svg width="0" height="0" class="hidden" style="display: none;">
    <symbol id="myicon-search" ..... </symbol>  
    <!-- all of my symbols here... -->
</svg>
<script type="text/discourse-plugin" version="0.8">
    api.replaceIcon('search', 'myicon-search'); 
</script>

And that is easy to manage.

I use the SVG sprite generator at https://svgsprit.es to make sure the format is correct.

3 个赞

Can we use Microsoft’s Fabric Icons with this method?

You should be able to, yes. I don’t see an official SVG sprite distribution of those icons, but according to Where can I find the svg files of the icons? · Issue #1008 · OfficeDev/office-ui-fabric-core · GitHub you can extract SVGs yourself, one by one, using their names.

4 个赞

I’m trying to do the same thing. I’ve changed the ‘share’ icon successfully in posts but it appears it also changes the ‘link’ icon in the editor. Any ideas how to change just the posts icon?

3 个赞

Is there a way to change a fontawesome icon globally to an SVG icon uploaded in the theme upload section? I am working on a site and would like to specifically change the fontawesome icon for when a topic is solved (using the discourse-solved plugin) to my site’s logo. I am having a hard time approaching this via the actual plugin code and I was wondering if this header method works for something like that.