aboutsummaryrefslogtreecommitdiff
path: root/src/main/themes/index.ts
diff options
context:
space:
mode:
authormegumin <megumin.bakaretsurie@gmail.com>2023-08-04 18:52:20 +0100
committerGitHub <noreply@github.com>2023-08-04 19:52:20 +0200
commitd6c43986fd665b60a8a83d41ef907dab22e990e7 (patch)
treeff9eda6794ef371568662b389a5e71e32bbbc16c /src/main/themes/index.ts
parentbb7deeb09cb263d158de0d99e873c3d3f43a73bb (diff)
downloadVencord-d6c43986fd665b60a8a83d41ef907dab22e990e7.tar.gz
Vencord-d6c43986fd665b60a8a83d41ef907dab22e990e7.tar.bz2
Vencord-d6c43986fd665b60a8a83d41ef907dab22e990e7.zip
Add proper user-friendly theme manager (#635)
Co-authored-by: Justice Almanzar <superdash993@gmail.com> Co-authored-by: Nuckyz <61953774+Nuckyz@users.noreply.github.com> Co-authored-by: V <vendicated@riseup.net>
Diffstat (limited to 'src/main/themes/index.ts')
-rw-r--r--src/main/themes/index.ts81
1 files changed, 81 insertions, 0 deletions
diff --git a/src/main/themes/index.ts b/src/main/themes/index.ts
new file mode 100644
index 0000000..f660e50
--- /dev/null
+++ b/src/main/themes/index.ts
@@ -0,0 +1,81 @@
+/* eslint-disable header/header */
+
+/*!
+ * BetterDiscord addon meta parser
+ * Copyright 2023 BetterDiscord contributors
+ * Copyright 2023 Vendicated and Vencord contributors
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+const splitRegex = /[^\S\r\n]*?\r?(?:\r\n|\n)[^\S\r\n]*?\*[^\S\r\n]?/;
+const escapedAtRegex = /^\\@/;
+
+export interface UserThemeHeader {
+ fileName: string;
+ name: string;
+ author: string;
+ description: string;
+ version?: string;
+ license?: string;
+ source?: string;
+ website?: string;
+ invite?: string;
+}
+
+function makeHeader(fileName: string, opts: Partial<UserThemeHeader> = {}): UserThemeHeader {
+ return {
+ fileName,
+ name: opts.name ?? fileName.replace(/\.css$/i, ""),
+ author: opts.author ?? "Unknown Author",
+ description: opts.description ?? "A Discord Theme.",
+ version: opts.version,
+ license: opts.license,
+ source: opts.source,
+ website: opts.website,
+ invite: opts.invite
+ };
+}
+
+export function stripBOM(fileContent: string) {
+ if (fileContent.charCodeAt(0) === 0xFEFF) {
+ fileContent = fileContent.slice(1);
+ }
+ return fileContent;
+}
+
+export function getThemeInfo(css: string, fileName: string): UserThemeHeader {
+ if (!css) return makeHeader(fileName);
+
+ const block = css.split("/**", 2)?.[1]?.split("*/", 1)?.[0];
+ if (!block) return makeHeader(fileName);
+
+ const header: Partial<UserThemeHeader> = {};
+ let field = "";
+ let accum = "";
+ for (const line of block.split(splitRegex)) {
+ if (line.length === 0) continue;
+ if (line.charAt(0) === "@" && line.charAt(1) !== " ") {
+ header[field] = accum.trim();
+ const l = line.indexOf(" ");
+ field = line.substring(1, l);
+ accum = line.substring(l + 1);
+ }
+ else {
+ accum += " " + line.replace("\\n", "\n").replace(escapedAtRegex, "@");
+ }
+ }
+ header[field] = accum.trim();
+ delete header[""];
+ return makeHeader(fileName, header);
+}