I have customized the colors of my Discourse instance to use a dark background color, however syntax highlighting does not look good anymore.
What is the easiest way to tell highlight.js to use a different style?
Users are discussing how to customize syntax highlighting styles in Discourse using highlight.js, particularly for dark-themed instances. dictoon initially tried copying CSS from the Agate theme into Admin > Customize > CSS but found styles were overridden due to CSS specificity issues — specifically, .hljs-tag .hljs-string had higher priority than .hljs-string alone riking. Attempts to use !important failed, and manually tweaking each rule proved impractical dictoon. Others, like pacharanero, echoed the desire for easier, modern-looking syntax themes. sam acknowledged the difficulty and suggested a howto guide to adapt themes from highlight.js’s official repo. Ultimately, Johani provided a solution: a theme component that lets admins pick from a dropdown of pre-configured highlight.js themes, available at hljs-theme-picker.
I have customized the colors of my Discourse instance to use a dark background color, however syntax highlighting does not look good anymore.
What is the easiest way to tell highlight.js to use a different style?
Admin, customize, css
Oh right, thanks Jeff.
Unfortunately it looks like it isn’t working correctly.
I copy/pasted the CSS from the Agate theme to a new section in Admin / Customize / CSS:
However I get the following results:
(Link to the page: https://forum.appleseedhq.net/t/some-thoughts-on-beta-1-4/144)
Notice that the .hljs-string color is #f99 which does not match the declared style of this class in the Agate theme:
.hljs-string,
.hljs-bullet {
color: #a2fca2;
}
Not sure what I’m doing wrong. Any clue?
Looks like I would need to add !important to each CSS property…
Is anyone aware of a better solution?
Edit: it doesn’t look like the !important solution would even work…
Nope, it’s because the default has a higher specificity - .hljs-tag .hljs-string . You need to replicate the theme’s rule with that selector.
Thanks @riking, I hadn’t seen this.
Unfortunately that means it’s basically impossible to try highlight.js themes (from their demo page) to find one that fits the color palette of my Discourse instance without inspecting and tweaking them, one rule at a time…
So I guess my original question remains: what’s the (second) easiest way to try an highlight.js theme with Discourse?
أود تذكير هذا الموضوع في حال كان أي شخص آخر قد وجد طريقة جيدة لاستيراد أنماط أجمل وأكثر ألوانًا للتمييز النحوي للأكواد؟ شيء يبدو أكثر مثل عرض محرر أكواد حديث.
قد يكون استخدام CSS مخصصًا حلاً مقبولاً، وتساءلت عما إذا كان أي شخص قد وضع CSS مناسبًا في شبكة CDN للاستيراد، أو مكون سمة؟
أوافق على أن تجاوز ما لدينا أمر معقد بعض الشيء. تمييز الأكواد مع مجموعة كبيرة من السمات في:
يمكن لموضوع مخصص بالتأكيد منح موقعك مظهرًا فريدًا:
@Johani ربما يجب أن ننشئ دليلًا يشرح كيفية أخذ CSS من المستودع الرسمي وتحويله وجعله يعمل مع CSS الخاص بنا؟
لقد قمت بإنشاء مكون سمة يسمح للمسؤولين باختيار سمة hljs من قائمة منسدلة. مزيد من التفاصيل هنا