Skip to content

Commit

Permalink
Add Unindent
Browse files Browse the repository at this point in the history
  • Loading branch information
Cynosphere committed Jan 21, 2025
1 parent b53d1c0 commit 52b09fc
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 0 deletions.
39 changes: 39 additions & 0 deletions src/unindent/index.ts
Original file line number Diff line number Diff line change
@@ -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<string, ExtensionWebpackModule> = {
unindent: {},
replacer: {
dependencies: [
{ ext: "commands", id: "commands" },
{ ext: "unindent", id: "unindent" }
],
entrypoint: true
}
};
15 changes: 15 additions & 0 deletions src/unindent/manifest.json
Original file line number Diff line number Diff line change
@@ -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
}
25 changes: 25 additions & 0 deletions src/unindent/webpackModules/replacer.ts
Original file line number Diff line number Diff line change
@@ -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 };
}
});
10 changes: 10 additions & 0 deletions src/unindent/webpackModules/unindent.ts
Original file line number Diff line number Diff line change
@@ -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"), "");
}

0 comments on commit 52b09fc

Please sign in to comment.