generated from moonlight-mod/sample-extension
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b53d1c0
commit 52b09fc
Showing
4 changed files
with
89 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"), ""); | ||
} |