diff --git a/src/unindent/index.ts b/src/unindent/index.ts new file mode 100644 index 0000000..8a28e15 --- /dev/null +++ b/src/unindent/index.ts @@ -0,0 +1,39 @@ +import { ExtensionWebpackModule, Patch } from "@moonlight-mod/types"; + +export const patches: Patch[] = [ + // codeblocks + { + find: "/^(¯\\\\_\\(ツ\\)_\\/¯)/.exec", + replace: { + match: /return{lang:(.+?),content:(.+?),inQuote:/, + replacement: (_, lang, content) => `const lang=${lang},content=${content}; +return { + lang, + content:lang.toLowerCase()==="ansi"?content:require("unindent_unindent").default(content), + inQuote:` + } + }, + + // file preview + { + find: ".openFullPreviewSection,", + replace: { + match: /(?<=let{text:(\i),language:(\i)}=\i),(?=\i=\(\)=>)/, + replacement: (_, text, language) => `; +if(${language}.toLowerCase()!=="ansi") + ${text}=require("unindent_unindent").default(${text}); +let ` + } + } +]; + +export const webpackModules: Record = { + unindent: {}, + replacer: { + dependencies: [ + { ext: "commands", id: "commands" }, + { ext: "unindent", id: "unindent" } + ], + entrypoint: true + } +}; diff --git a/src/unindent/manifest.json b/src/unindent/manifest.json new file mode 100644 index 0000000..2e12fe8 --- /dev/null +++ b/src/unindent/manifest.json @@ -0,0 +1,15 @@ +{ + "$schema": "https://moonlight-mod.github.io/manifest.schema.json", + "id": "unindent", + "version": "1.0.0", + "meta": { + "name": "Unindent", + "tagline": "Fixes indentation in codeblocks", + "authors": ["Cynosphere"], + "tags": ["chat"], + "source": "https://github.com/Cynosphere/moonlight-extensions", + "donate": "https://ko-fi.com/Cynosphere" + }, + "dependencies": ["commands"], + "apiLevel": 2 +} diff --git a/src/unindent/webpackModules/replacer.ts b/src/unindent/webpackModules/replacer.ts new file mode 100644 index 0000000..ac33f6b --- /dev/null +++ b/src/unindent/webpackModules/replacer.ts @@ -0,0 +1,25 @@ +import Commands from "@moonlight-mod/wp/commands_commands"; +import unindent from "./unindent"; + +const REGEX_CODEBLOCK = /```(?:([a-z0-9_+\-.]+?)\n)?\n*([^\n][^]*?)(\n*)```/i; +const REGEX_CODEBLOCK_GLOBAL = new RegExp(REGEX_CODEBLOCK.source, "gi"); + +Commands.registerLegacyCommand("unindent", { + match: REGEX_CODEBLOCK, + action: (content) => { + content = content.replace(REGEX_CODEBLOCK_GLOBAL, (block) => { + const match = block.match(REGEX_CODEBLOCK)!; + const lang = match[1] ?? ""; + if (lang.toLowerCase() === "ansi") return block; + + const newline = match[3] ?? ""; + + let blockContent = match[2]; + blockContent = unindent(blockContent); + + return `\`\`\`${lang}\n${blockContent}${newline}\`\`\``; + }); + + return { content }; + } +}); diff --git a/src/unindent/webpackModules/unindent.ts b/src/unindent/webpackModules/unindent.ts new file mode 100644 index 0000000..0967b33 --- /dev/null +++ b/src/unindent/webpackModules/unindent.ts @@ -0,0 +1,10 @@ +export default function unindent(str: string) { + // remove tabs (bots + file preview) + str = str.replaceAll("\t", " "); + + const minIndent = + str.match(/^ *(?=\S)/gm)?.reduce((previous, current) => Math.min(previous, current.length), Infinity) ?? 0; + + if (!minIndent) return str; + return str.replace(new RegExp(`^ {${minIndent}}`, "gm"), ""); +}