CLUB1 Server Side Highlight

License Latest Stable Version Total Downloads Coverage Build Status

A Flarum extension. Server-side code highlighting with cached results.

banner
banner

For each code block of the posts, a hash is computed while parsing them, then on render the code highlighting is done in the backend once, and stored in the cache for future renderings.

It provides the following advantages:

  1. The highlighting is done on the server so it is not up to the clients to do it.
  2. The server uses the cache to save highlighted blocks to only do the processing once.
  3. It works without any JS so even on the worst web browser the highlight will be there and there is no delay before the highlighting is applied.
  4. The theme adapts itself to the "dark mode" setting of Flarum.
  5. It works even with BBCode extension disabled.
  6. Admins can choose the highlight theme for both "Light" and "Dark" Flarum variants.
  7. Admins can download highlight themes with highlight:download Flarum console command.
  8. It is compatible with fof/nightmode by switching the highlight theme accordingly.

Client-side highlighting is still used, but only for the post preview.

Installation

Install with composer:

composer require club-1/flarum-ext-server-side-highlight:"*"

Recommendation

This extension alone does not apply the formatting changes to previously posted comments. I you want to reparse all the comments posts of the database it is recommended to install and enable the club-1/flarum-ext-chore-commands extension and use its chore:reparse command.

Updating

composer update club-1/flarum-ext-server-side-highlight:"*"
php flarum migrate
php flarum cache:clear
php flarum assets:publish

Acknowledgement

This extension is based on the following libraries:

Links

This is very interesting, I'm not gonna lie.

Well done!

18 days later

v1.1.0 - 2023-05-17

Added

  • Add support links in the metadata of the package.
  • Test more PHP versions.
  • Setup PHPStan for static code analysis.
  • Set 7.3 as the minimum required PHP version.

Fixed

  • Fix a crash when start or end tag are omitted.

Changelog

10 days later

v1.2.1 - 2023-05-27

Fixed

  • Fix default highlight theme values.

v1.2.0 - 2023-05-27

Added

  • Add settings to change code-block's background and text colors. This allows to keep higlight.js themes unchanged.
  • Allow to choose the highlight theme for dark and light Flarum styles.
  • Add base16/monokai theme.

Changelog

P.S. Make sure to run php flarum assets:publish as assets have been updated in v1.2.0.

7 days later

v1.3.0 - 2023-06-03

Added

  • Add a Flarum console command to download and register themes from highlight.js releases.
  • Add integration with fof/nightmode >= 1.5.2 by switching the highlight theme accordingly.

Fixed

  • Support Flarum installations that have a separated assets storage by using the filesystem interface as described in the docs.

Changelog

P.S. Make sure to run php flarum migrate as a migration has been added in this version.

6 days later

v1.4.0 - 2023-06-09

Added

  • Allow copy/pasting color code by using color-preview instead of color setting types.
  • Add code highlight preview for current settings in admin page.
  • Debounce code highlight in preview, using data-s9e-livepreview-hash. This way the highlighting is not computed again if the code did not change, which should improve performances.

Changelog