Plugin

Unindent

Trims leading indentation from codeblocks

Chat Utility
index.ts
Download

Source

src/plugins/unindent/index.ts
1/*
2 * FiveCord — a Discord client mod
3 * Copyright (c) 2025 FiveCord
4 * SPDX-License-Identifier: GPL-3.0-or-later
5 */
6
7import { MessageObject } from "@api/MessageEvents";
8import { Devs } from "@utils/constants";
9import definePlugin from "@utils/types";
10
11export default definePlugin({
12 name: "Unindent",
13 description: "Trims leading indentation from codeblocks",
14 tags: ["Chat", "Utility"],
15 authors: [Devs.Ven],
16
17 patches: [
18 {
19 find: "inQuote:",
20 replacement: {
21 match: /,content:([^,]+),inQuote/,
22 replace: (_, content) => `,content:$self.unindent(${content}),inQuote`
23 }
24 }
25 ],
26
27 unindent(str: string) {
28 // Users cannot send tabs, they get converted to spaces. However, a bot may send tabs, so convert them to 4 spaces first
29 str = str.replace(/\t/g, " ");
30 const minIndent = str.match(/^ *(?=\S)/gm)
31 ?.reduce((prev, curr) => Math.min(prev, curr.length), Infinity) ?? 0;
32
33 if (!minIndent) return str;
34 return str.replace(new RegExp(`^ {${minIndent}}`, "gm"), "");
35 },
36
37 unindentMsg(msg: MessageObject) {
38 msg.content = msg.content.replace(/```(.|\n)*?```/g, m => {
39 const lines = m.split("\n");
40 if (lines.length < 2) return m; class="ts-cmt">// Do not affect inline codeblocks
41 let suffix = "";
42 if (lines[lines.length - 1] === "```") suffix = lines.pop()!;
43 return `${lines[0]}\n${this.unindent(lines.slice(1).join("\n"))}\n${suffix}`;
44 });
45 },
46
47 onBeforeMessageSend(_, msg) {
48 return this.unindentMsg(msg);
49 },
50
51 onBeforeMessageEdit(_cid, _mid, msg) {
52 return this.unindentMsg(msg);
53 }
54});
55