Skip to content

A fully open source & end-to-end encrypted note taking alternative to Evernote.

editor: import & use language grammars from refractor directly

Abdullah Atta7c257cfbd2ea
diff --git a/packages/editor/.npmignore b/packages/editor/.npmignore
index e7cf8384a..9ff305ca1 100644
--- a/packages/editor/.npmignore
+++ b/packages/editor/.npmignore
@@ -1,4 +1,2 @@
*
!dist/**/*
-!styles/**/*
-!languages/**/*
\ No newline at end of file
diff --git a/packages/editor/scripts/build.mjs b/packages/editor/scripts/build.mjs
index c276c5475..785563e62 100755
--- a/packages/editor/scripts/build.mjs
+++ b/packages/editor/scripts/build.mjs
@@ -42,9 +42,23 @@ for (const name in pathsToCopy) {
});
}
-const languagesList = await langen(ROOT_DIR, path.join(ROOT_DIR, "languages"));
+const { languageIndex, languages } = await langen(ROOT_DIR);
+
+if (!languageIndex || !languages) throw new Error("No language index found.");
+
+fs.writeFileSync(
+ path.join(
+ ROOT_DIR,
+ "src",
+ "extensions",
+ "code-block",
+ "languages",
+ "index.ts"
+ ),
+ languageIndex
+);
fs.writeFileSync(
path.join(ROOT_DIR, "src", "extensions", "code-block", "languages.json"),
- JSON.stringify(languagesList)
+ JSON.stringify(languages)
);
diff --git a/packages/editor/scripts/postbuild.mjs b/packages/editor/scripts/postbuild.mjs
index 9b0a32e1a..5ab0ccad8 100644
--- a/packages/editor/scripts/postbuild.mjs
+++ b/packages/editor/scripts/postbuild.mjs
@@ -21,33 +21,52 @@ import * as Mjs from "@mdi/js";
import path from "path";
import fs from "fs";
import { fileURLToPath } from "url";
-import { readFile, writeFile } from "fs/promises";
+import { cp, readFile, writeFile } from "fs/promises";
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const ROOT_DIR = path.resolve(path.join(__dirname, ".."));
const DIST_DIR = path.resolve(ROOT_DIR, "dist");
-const ICONS_FILE_PATH = path.join(DIST_DIR, "toolbar", "icons.js");
+const CJS_BUNDLE_PATH = path.resolve(ROOT_DIR, "dist", "index.js");
+const MJS_BUNDLE_PATH = path.resolve(ROOT_DIR, "dist", "index.mjs");
-if (!fs.existsSync(DIST_DIR) || !fs.existsSync(ICONS_FILE_PATH))
+if (
+ !fs.existsSync(DIST_DIR) ||
+ !fs.existsSync(CJS_BUNDLE_PATH) ||
+ !fs.existsSync(MJS_BUNDLE_PATH)
+)
throw new Error("Please build the editor before running this script.");
-console.log("Replacing icons with their path...");
+for (const bundle of [
+ { type: "cjs", path: CJS_BUNDLE_PATH },
+ { type: "mjs", path: MJS_BUNDLE_PATH }
+]) {
+ console.log("Replacing icons with their path...");
-let ICON_FILE = await readFile(ICONS_FILE_PATH, "utf-8");
-const icons = ICON_FILE.matchAll(/: (mdi.+),/g);
-for (const icon of icons) {
- const iconPath = Mjs[icon[1]];
- if (!iconPath) throw new Error(`Could not find path for icon: ${icon[1]}.`);
- ICON_FILE = ICON_FILE.replace(icon[0], `: "${iconPath}",`);
-}
+ let ICON_FILE = await readFile(bundle.path, "utf-8");
+ const icons =
+ bundle.type === "cjs"
+ ? ICON_FILE.matchAll(/: _js\.(mdi.+),/g)
+ : ICON_FILE.matchAll(/: (mdi.+),/g);
+ for (const icon of icons) {
+ const iconPath = Mjs[icon[1]];
+ if (!iconPath) throw new Error(`Could not find path for icon: ${icon[1]}.`);
+ ICON_FILE = ICON_FILE.replace(icon[0], `: "${iconPath}",`);
+ }
+
+ console.log("Removing @mdi/js import...");
-console.log("Removing @mdi/js import...");
+ ICON_FILE = ICON_FILE.replace("var _js = require('@mdi/js');", "");
-ICON_FILE = ICON_FILE.replace(/^import \{.+ } from "@mdi\/js";/gm, "");
+ console.log("Saving file...");
+
+ await writeFile(bundle.path, ICON_FILE);
+}
-console.log("Saving file...");
+console.log("copying styles...");
-await writeFile(ICONS_FILE_PATH, ICON_FILE);
+await cp(path.resolve(ROOT_DIR, "styles"), path.resolve(DIST_DIR, "styles"), {
+ recursive: true
+});
console.log("Done.");
diff --git a/packages/editor/tsconfig.json b/packages/editor/tsconfig.json
index b47f68260..7f364ad4c 100644
--- a/packages/editor/tsconfig.json
+++ b/packages/editor/tsconfig.json
@@ -3,8 +3,7 @@
"compilerOptions": {
"lib": ["DOM", "DOM.Iterable", "ESNext"],
"jsx": "react-jsx",
- "outDir": "./dist/",
- "incremental": true
+ "outDir": "./dist/"
},
"exclude": ["src/**/*.test.ts"],
"include": ["src/**/*"]