Article

How a Userscript Adds Reddit-Style Nested Comments to Any Discourse Forum — No Admin Required

How a Userscript Adds Reddit-Style Nested Comments to Any Discourse Forum — No Admin Required

If you run a Discourse forum, 2026 finally gave you an official answer to the oldest feature request in the book: Discourse announced native nested replies in May, with admin-gated settings for default sort modes and max depth. But if you merely use someone else's Discourse forum, that announcement changed nothing for you — the feature is off unless your admin turns it on, and pre-existing topics stay flat even then.

A developer on LINUX DO, China's largest developer forum (itself a Discourse instance), decided not to wait. They published a Tampermonkey userscript, now at version 4.2.0, that re-renders any thread on the site as nested 楼中楼 ("building-within-building") comments — Reddit-style reply trees — entirely in the browser, with zero admin involvement. The script ships with its full source in the forum thread, and the techniques it uses form a portable pattern anyone can adapt to their own Discourse community. Here's what it does and how it works.

Why nesting was always the missing feature

Discourse's flat, chronological post stream was a deliberate design choice, not an oversight. The official announcement put it plainly: a flat, linear timeline had "always been the best way" to keep everyone in the conversation on the same page. For over a decade, Discourse's answer to "where are the nested comments?" was essentially "that's against the philosophy" — replies-to-replies were discoverable through expandable "replies" buttons, but the stream stayed flat.

That stance softened only this year, and the new native feature is cautious: it's opt-in per category, configured by administrators, still early in its roadmap, and — per the announcement — pre-existing topics remain in flat mode with no way to bulk-convert a category. So the interesting gap is obvious: the majority of Discourse threads on the internet, including every historical thread, will never get nested views from the official feature. That's exactly the gap a client-side userscript fills. It doesn't change the forum; it changes how your browser draws it.

What the script actually does

Per the author's thread on LINUX DO, the script (shared as v4.2.0 with complete source) adds a toggle to each thread's stats bar switching between Discourse's native flat view and a threaded view. In threaded mode it:

  • Rebuilds the reply tree. Posts are reordered into nested groups following reply-to relationships. Rather than indenting deeper and deeper, the design indents each discussion only once and labels each reply with who it's answering — a readability choice borrowed from Chinese forum culture, where 楼中楼 (nested "floor-within-floor" comments, popularized by Baidu Tieba) keeps long sub-conversations skimmable.
  • Toggles quotes per comment. In threaded mode, quoted text blocks are hidden by default and can be expanded individually, cutting the visual noise that makes long quote-chains unreadable.
  • Replaces the progress indicator. Discourse's signature right-side timeline tracks post numbers, but once posts are reordered those numbers stop mapping to scroll position. The script swaps it for a custom reading-progress rail showing actual scroll percentage, including how many posts have loaded out of the total in long threads — with a compact mobile variant.
  • Keeps the native machinery intact. The original post, the "load more" lazy-loading, reply/like/share buttons, and Discourse's own styling variables all survive — the script restyles rather than replaces, so theme variables like the site's color palette carry through automatically.

All of this is reported from the author's own thread, which is worth reading directly if you read Chinese — including the version history showing how the script evolved from basic nesting to mobile adaptation.

Under the hood: three tricks that make it work

You don't need the author's exact code to learn from the approach; the architecture is the interesting part, and it transfers to any Discourse instance.

Trick 1: Reconstruct the reply tree from Discourse's own JSON API. Discourse exposes every topic as JSON — append .json to a topic URL and you get the post stream with each post's reply-to metadata. The script fetches these endpoints (e.g. /t/<topic-id>/<post-number>.json) to learn which post answers which, then re-sorts the already-loaded DOM nodes into tree order. No scraping of visible text required; the forum's own data model does the heavy lifting. This is the same public API surface Discourse documents for integrations, used here entirely read-only from inside the browser.

Trick 2: Re-render, don't rebuild. The script runs at document-idle and works with Discourse's existing post-stream DOM — reordering and annotating the real post elements with depth attributes rather than constructing a parallel fake thread. That means likes, reply buttons, embedded polls, and oneboxes keep working, because they're the actual forum components, just rearranged. The tradeoff: it's coupled to Discourse's CSS class names, so a major Discourse front-end update can break the layout until the script is adjusted.

Trick 3: Persist preferences with userscript APIs. The script's header requests standard Tampermonkey grants — GM_addStyle for injecting theme-aware CSS, GM_getValue/GM_setValue for remembering your view preference across visits, and GM_registerMenuCommand for a settings entry. A minimal version of the pattern looks like this:

// ==UserScript==
// @name        Nested view for my Discourse forum
// @match       https://forum.example.com/*
// @run-at      document-idle
// @grant       GM_addStyle
// @grant       GM_getValue
// @grant       GM_setValue
// ==/UserScript==

(async () => {
  'use strict';
  // 1. Fetch the topic JSON to get reply-to relationships
  const topicId = location.pathname.match(/\/t\/[^/]+\/(\d+)/)[1];
  const data = await fetch(`/t/${topicId}.json`).then(r => r.json());
  const childrenOf = {};
  for (const p of data.post_stream.posts) {
    const parent = p.reply_to_post_number || 0;
    (childrenOf[parent] ||= []).push(p.post_number);
  }
  // 2. Reorder DOM nodes into tree order, annotating depth
  // 3. Inject CSS via GM_addStyle using the site's own variables
  GM_addStyle(`.threaded .topic-post { border-left: 2px solid var(--primary-low); }`);
})();

Change the @match line and the selectors, and you have a starter for any Discourse site.

Official feature vs. userscript: when each wins

Official nested replies (2026)Client-side userscript
Who enables itForum admin, per categoryEach reader, in their own browser
Existing threadsStay flat; no bulk conversionAny thread, old or new
Depth controlAdmin setting (max depth)Whatever the script implements
MaintenanceDiscourse teamBreaks if Discourse renames DOM classes
Works on forums you don't runOnly if the admin opted inYes — that's the whole point

The two aren't really competitors. The official feature is the right answer for community owners; the userscript is the right answer for everyone else — readers, moderators, and power users of forums whose admins will never touch the setting.

The part built with AI

The author notes the script was developed with AI coding assistance, which is worth a moment: this is exactly the class of project where AI agents shine — a large, fiddly DOM-manipulation task with hundreds of CSS selectors to tune against a live site, where the feedback loop is "reload the page and look." It's grunt work that humans find exhausting and agents find straightforward. (Chinese devs have been pushing these agent workflows hard — see our piece on plan-mode and disciplined AI coding agents.) The version history in the thread reads like a changelog of that loop: basic nesting first, then quote toggling, then the custom progress rail, then mobile — each iteration visibly scoped to one pain point.

Take it with you

If your favorite Discourse community still shows you a thousand-post flat wall, you now know the recipe: fetch the topic JSON, rebuild the reply tree, reorder the real DOM nodes, and style with the site's own CSS variables. Fork the LINUX DO script, change the @match line, and adjust the selectors to your forum's theme — or build from the skeleton above. The flat timeline had a good decade-long run as Discourse's philosophy; your browser is allowed to disagree.

Keep reading
RustDesk: The Self-Hosted Remote Desktop Chinese Devs Use Instead of TeamViewer
Read next →