Also patch codeblocks of others

This commit is contained in:
Vendicated 2021-05-31 01:52:35 +02:00
parent eb801f9e22
commit c801fb8f6b
No known key found for this signature in database
GPG Key ID: EC781ADFB93EFFA3

121
index.js

@ -15,68 +15,93 @@
const { Plugin } = require("powercord/entities");
const { inject, uninject } = require("powercord/injector");
const { messages } = require("powercord/webpack");
const { messages, getModule } = require("powercord/webpack");
const indents = [, " ", "\t"];
const injectionId = "unindentCodeblocks";
const sendMessageInjectionId = "unindentSendMessage";
const codeblockInjectionId = "unindentCodeblocks";
module.exports = class Unindent extends Plugin {
startPlugin() {
inject(injectionId, messages, "sendMessage", async args => {
const msg = args[1];
msg.content = msg.content.replace(/```(.|\n)*?```/g, m => {
const lines = m.split("\n");
async startPlugin() {
inject(
sendMessageInjectionId,
messages,
"sendMessage",
args => {
const msg = args[1];
msg.content = msg.content.replace(/```(.|\n)*?```/g, m => this.unindent(m, 1));
return args;
},
true
);
// First line sometimes (?) has no indent (thanks discord), so use the second line as reference in these cases
let choseSecondLine = false;
let firstIndentedLine;
if (indents.indexOf(lines[1].charAt(0)) === -1) {
firstIndentedLine = lines[2];
choseSecondLine = true;
} else {
firstIndentedLine = lines[1];
}
const parser = await getModule(["parse", "parseTopic"]);
if (!firstIndentedLine) return m;
inject(
codeblockInjectionId,
parser.defaultRules.codeBlock,
"react",
args => {
args[0].content = this.unindent(args[0].content, 0);
return args;
},
true
);
}
let indentAmount = 0;
for (const char of firstIndentedLine) {
const idx = indents.indexOf(char);
if (idx === -1) break;
indentAmount += idx;
}
unindent(str, firstLineIdx) {
const lines = str.split("\n");
// First line sometimes (?) has no indent (thanks discord), so use the second line as reference in these cases
let choseSecondLine = false;
let firstIndentedLine;
if (indents.indexOf(lines[firstLineIdx].charAt(0)) === -1) {
firstIndentedLine = lines[firstLineIdx + 1];
choseSecondLine = true;
} else {
firstIndentedLine = lines[firstLineIdx];
}
// FIXME (Find less hacky solution): If indent amount is based on second line, try to figure out it is in block (thus indented further) and if so leave additional indent
if (
choseSecondLine &&
(lines[1].includes("{") || lines[1].endsWith(":") || lines[1].endsWith("; then") || firstIndentedLine.indexOf("{") === indentAmount + 1)
)
indentAmount -= 2;
if (!firstIndentedLine) return str;
if (indentAmount <= 0) return m;
let indentAmount = 0;
for (const char of firstIndentedLine) {
const idx = indents.indexOf(char);
if (idx === -1) break;
indentAmount += idx;
}
for (let i = 1; i < lines.length; i++) {
const line = lines[i];
let unindentedIdx = 0;
for (let j = 0, unindentedAmount = 0; j < line.length; j++) {
const char = line[j];
const idx = indents.indexOf(char);
if (idx === -1) break;
unindentedIdx++;
unindentedAmount += idx;
if (unindentedAmount >= indentAmount) break;
}
lines[i] = line.slice(unindentedIdx);
}
// FIXME (Find less hacky solution): If indent amount is based on second line, try to figure out it is in block (thus indented further) and if so leave additional indent
if (
choseSecondLine &&
(lines[firstLineIdx].includes("{") ||
lines[firstLineIdx].endsWith(":") ||
lines[firstLineIdx].endsWith("; then") ||
firstIndentedLine.indexOf("{") === indentAmount + 1)
)
indentAmount -= 2;
return lines.join("\n");
});
return args;
});
if (indentAmount <= 0) return str;
for (let i = firstLineIdx; i < lines.length; i++) {
const line = lines[i];
let unindentedIdx = 0;
for (let j = 0, unindentedAmount = 0; j < line.length; j++) {
const char = line[j];
const idx = indents.indexOf(char);
if (idx === -1) break;
unindentedIdx++;
unindentedAmount += idx;
if (unindentedAmount >= indentAmount) break;
}
lines[i] = line.slice(unindentedIdx);
}
return lines.join("\n");
}
pluginWillUnload() {
uninject(injectionId);
uninject(sendMessageInjectionId);
uninject(codeblockInjectionId);
}
};