editor: import & use language grammars from refractor directly

This commit is contained in:
Abdullah Atta
2024-09-23 15:13:39 +05:00
parent 99c77e6c5f
commit 7c257cfbd2
4 changed files with 52 additions and 22 deletions

View File

@@ -1,4 +1,2 @@
* *
!dist/**/* !dist/**/*
!styles/**/*
!languages/**/*

View File

@@ -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( fs.writeFileSync(
path.join(ROOT_DIR, "src", "extensions", "code-block", "languages.json"), path.join(ROOT_DIR, "src", "extensions", "code-block", "languages.json"),
JSON.stringify(languagesList) JSON.stringify(languages)
); );

View File

@@ -21,33 +21,52 @@ import * as Mjs from "@mdi/js";
import path from "path"; import path from "path";
import fs from "fs"; import fs from "fs";
import { fileURLToPath } from "url"; import { fileURLToPath } from "url";
import { readFile, writeFile } from "fs/promises"; import { cp, readFile, writeFile } from "fs/promises";
const __filename = fileURLToPath(import.meta.url); const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename); const __dirname = path.dirname(__filename);
const ROOT_DIR = path.resolve(path.join(__dirname, "..")); const ROOT_DIR = path.resolve(path.join(__dirname, ".."));
const DIST_DIR = path.resolve(ROOT_DIR, "dist"); 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."); 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"); let ICON_FILE = await readFile(bundle.path, "utf-8");
const icons = ICON_FILE.matchAll(/: (mdi.+),/g); const icons =
for (const icon of icons) { bundle.type === "cjs"
const iconPath = Mjs[icon[1]]; ? ICON_FILE.matchAll(/: _js\.(mdi.+),/g)
if (!iconPath) throw new Error(`Could not find path for icon: ${icon[1]}.`); : ICON_FILE.matchAll(/: (mdi.+),/g);
ICON_FILE = ICON_FILE.replace(icon[0], `: "${iconPath}",`); 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...");
ICON_FILE = ICON_FILE.replace("var _js = require('@mdi/js');", "");
console.log("Saving file...");
await writeFile(bundle.path, ICON_FILE);
} }
console.log("Removing @mdi/js import..."); console.log("copying styles...");
ICON_FILE = ICON_FILE.replace(/^import \{.+ } from "@mdi\/js";/gm, ""); await cp(path.resolve(ROOT_DIR, "styles"), path.resolve(DIST_DIR, "styles"), {
recursive: true
console.log("Saving file..."); });
await writeFile(ICONS_FILE_PATH, ICON_FILE);
console.log("Done."); console.log("Done.");

View File

@@ -3,8 +3,7 @@
"compilerOptions": { "compilerOptions": {
"lib": ["DOM", "DOM.Iterable", "ESNext"], "lib": ["DOM", "DOM.Iterable", "ESNext"],
"jsx": "react-jsx", "jsx": "react-jsx",
"outDir": "./dist/", "outDir": "./dist/"
"incremental": true
}, },
"exclude": ["src/**/*.test.ts"], "exclude": ["src/**/*.test.ts"],
"include": ["src/**/*"] "include": ["src/**/*"]