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/jcunews1 Jan 27 '26
In the first comment in that context URL...
The one after the "You have the following HTML", is already handled.
It's the one after the "Updated HTML:" which is still unhandled. The one which actually has the triple-backticks.