Sélecteur de thème Codeblock

:discourse2: Summary Codeblock Theme Picker will allow you to easily change the theme used in code blocks.
:eyeglasses: Preview Preview on Discourse Theme Creator
:hammer_and_wrench: Repository Link https://github.com/discourse/hljs-theme-picker
:open_book: New to Discourse Themes? Beginner’s guide to using Discourse Themes

Install this theme component

Features

This is just a handful of the available options. This component includes all 90 options offered in the hljs repository.

:information_source: While all the options are included in this component, it will only load the one you pick. So, there’s no overhead.

How to use it

Due to the way this component is wired up, it will require a small change to one of your site settings.

  1. Head to your.site.com/admin/site_settings/
  2. Search for theme_authorized_extensions
  3. Add css to that list.

Like so:

:warning: You have to do this before you install the component, otherwise it won’t work.

Once that’s done, you can install the component just like any other component.

Settings

Name Description
hljs theme Select the theme you want code blocks to use. You can visit the hljs demo page to preview the themes
hljs dark match Load dark color scheme if available

If you need to see what the options look like, have a look here We're not a CDN - highlight.js

Once you decide on a hljs theme to use, you can search the dropdown for the one you want and save the setting. You will need to refresh the page in order for everything to start working.

Credits

All of the hljs themes included in this component have copyright information in their respective files. The list of all of those authors (:heart:) is too big to add here, but you can see them here.


:discourse2: Hosted by us? Theme components are available to use on our Standard, Business, and Enterprise plans.

Last edited by @JammyDodger 2024-06-11T13:25:37Z

Check documentPerform check on document:
42 « J'aime »

This theme is very useful and running in my site for a long time. How ever I suddenly found it has a problem on firefox. The custom css file blocked by the following header X-Content-Type-Options: nosniff. Console shows that css file MIME type is text/html. No matter which theme I choise the problem still happen on firefox but chrome is fine.

This super theme component would be even better if the selected codeblock style was also selectable as a user preference.

5 « J'aime »

Seconded. As user preference would be very cool.

Anyway to show line numbers?

Great theme component.

One question, there are 242 themes on highlight.js demo. For example, “Github Dark Dimmed” as attached image.

Is there any way to solve this issue? Thanks for advice.

Unfortunately, this is a little bit involved.

Discourse runs version 10.6.0 of highlight-js. Most of the extra themes missing right now - base16 - require version 11.0

The issue is that that version introduces some breaking changes and deprecations. Have a look here

Release Version 11 · highlightjs/highlight.js · GitHub

So, this means that the current options are going to stay the same for a while. First, the highlight-js version needs to be updated in tests-passed in Discourse. Then we’d need to wait for a stable release. Updating the themes (even the current ones) would put us in a bad state where it’s guesswork whether the theme works properly on your site or not.

4 « J'aime »

Ce composant semble être défectueux dans les dernières bêtas - la mise en surbrillance ne fonctionne plus correctement lorsqu’il est installé. J’ai supprimé le composant des thèmes et cela a recommencé à fonctionner - je ne peux juste pas choisir le thème de mise en surbrillance.

2 « J'aime »

la coloration syntaxique est également cassée ici dans la dernière version bêta

@Falco vient de fusionner un correctif pour ce composant : DEV: Update highlight.js to version 11 by xfalcox · Pull Request #3 · discourse/hljs-theme-picker · GitHub

Il inclut une mise à jour vers les thèmes de la version 11 de highlight.js (le cœur a été mis à jour vers la version 11 de highlight.js également) et des correctifs pour les styles qui manquaient de la bonne spécificité.

2 « J'aime »

J’ai obtenu cette erreur lorsque j’ai essayé d’installer le composant

Erreur lors de la création de l'élément de téléversement : a11y-dark. Nom de fichier d'origine Désolé, le fichier que vous essayez de téléverser n'est pas autorisé (extensions autorisées : wasm, jpg, jpeg, png, woff, woff2, svg, eot, ttf, otf, gif, webp, avif, js).

Avez-vous fait cette partie :point_down:

1 « J'aime »

Oh non, je vais faire ça :hugs:

1 « J'aime »

Je viens de trouver ce composant de thème — je suis tellement content qu’il existe ! Je remarque que le réglage hljs dark match ne semble pas fonctionner.

Par exemple, nous avons hljs theme réglé sur atom-one-light (pour lequel il existe également atom-one-dark).

Après actualisation, le atom-one-light s’applique en mode clair, mais le bloc de code ne passe pas au mode sombre lors du passage en mode sombre — il reste clair.


J’ai reçu ce message d’erreur lors de l’installation de composants de thème, j’espère que quelqu’un pourra guider la communauté sur la façon de les corriger.

Avez-vous ajouté css à la liste des extensions de thème autorisées ?

1 « J'aime »

Merci, ça marche ^^

1 « J'aime »

Note :

Cela devrait être :