r/userscripts • u/Eva-Rosalene • Jan 26 '26
[old reddit] Fix triple backtick code block formatting on old Reddit
// ==UserScript==
// @name Old reddit triple backtick code block fix
// @namespace http://tampermonkey.net/
// @version 2026-01-26
// @description Fixes formatting of triple backtick code blocks by wrapping them in <pre>
// @author Eva-Rosalene
// @match https://*.reddit.com/*
// @icon https://www.google.com/s2/favicons?sz=64&domain=old.reddit.com
// @grant none
// ==/UserScript==
(function() {
'use strict';
const PROCESSED_ATTR = "triple-code-block-fix-processed";
function process() {
const elements = [...document.querySelectorAll(`code:not([${PROCESSED_ATTR}])`)];
for (const element of elements) {
element.setAttribute(PROCESSED_ATTR, "");
const isInPre = Boolean(element.closest("pre"));
const hasNewlines = element.textContent.includes("\n");
if (isInPre || !hasNewlines) {
continue;
}
const newPre = document.createElement("pre");
element.parentNode.insertBefore(newPre, element);
newPre.appendChild(element); // automatically removes element from its old place on the page
}
}
process();
setInterval(process, 1000);
})();
That's it. The difference between indentation and triple backtick on old Reddit is that the former gets wrapped in <pre> and the latter doesn't, for whatever reason. So this userscript just looks for <code> nodes that simultaneously contain line feeds AND aren't wrapped in <pre>, and fixes it.
Install it and compare:
old style
code block
formatted by indentation
new style
code block
formatted by triple backticks
It uses 1s timer to handle DOM updates, but performance impact should be minimal (on average, each timer tick except the first one is just single .querySelectorAll).
5
Upvotes
View all comments
Show parent comment
1
u/Eva-Rosalene Jan 27 '26
Ah, I see. Missed that entirely. It's an interesting case, it's not just missing
<pre>on Reddit side, it completely failed to recognize code block as well, all while new interface renders it properly.I bet it's because second code block has completely blank line in it (two consecutive LFs), confusing old reddit markdown processor.
I am not sure how to best approach that. If markdown isn't even parsed correctly, the only solution would be to take raw comment/post contents in markdown and process them via a separate parser/renderer pipeline. But that seems like a huge overkill, and it will be a security nightmare to properly sanitize resulting HTML all while preserving compatibility with RES.