old-reddit-fenced-codeblocks-fix

Fixes triple backquote style code blocks in the old reddit layout and can apply syntax highlighting.

Author
Kerfuffle
Daily installs
0
Total installs
49
Ratings
1 0 0
Version
0.3
Created
2023-03-03
Updated
2023-03-06
License
MIT
Applies to

old-reddit-fenced-codeblocks-fix

Overview

Fixes the display of fenced code blocks when using the old reddit layout. For example, the new reddit will allow people to use markup like:

```somelanguage
code_code();
```

Unfortunately, they never updated the old reddit to handle this so it comes out a garbled mess. This userscript attempts to find those comments and fix them. It can also perform syntax highlighting on the code at the same time.

Before

Before

After

After

Usage

The default settings will run the userscript automatically on page load. Note that currently it does not automatically get reapplied when new comments are loaded or collapsed ones are expanded. It also currently doesn't work in private messages and modmail (not a hard fix, so I can probably do it if someone pokes me).

It should also add an "Apply" action which can be accessed through your userscript manager. This can be used to manually reapply it after loading new content which needs the fix.

Configuration

See the top of the script source code for possible configuration options. You can prevent it from running automatically, change the syntax highlighting theme (defaults to dark github style), etc. The configuration is stored as a setting in the userscript manager. The exact way to modify it will depend on what you're using, but here's an example with ViolentMonkey: Go like you're going to edit the script, then choose the "values" tab at the top. You can edit the settings in JSON format from there. If you make a mistake or want to reset to the defaults, simple delete the config key and the userscript will regenerate from the defaults.

Notes/limitations

  1. Currently only tested on Firefox and ViolentMonkey (there's no particular reason I know of that it should fail on different browsers or userscript managers).
  2. It pulls in two relatively heavy dependencies: a markup to HTML engine and a syntax highlighting engine. If you don't need syntax highlighting, you can potentially just remove the @require line near the top of the script. It will detect highlighting is unavailable regardless of the configuration settings.
  3. It works by completely regenerating the HTML from the comment/post markup using a different engine than reddit. This may cause weird effects. (Appears to work for the testing I've done so far).
  4. Pages in the old reddit design don't contain the original markup, so any comment/submission that needs to be fixed requires loading the markup using an API call. This could be slow if there are a lot of comments requiring the fix (generally they are rare though).
  5. Since this regenerates the markdown, it will clobber changes from other addons/userscripts. For example, RES media preview buttons.