editor: generate code languages in build step

This commit is contained in:
Abdullah Atta
2022-10-20 14:07:26 +05:00
parent 9ee13df74e
commit 7dbcea2663
6 changed files with 35 additions and 363 deletions

View File

@@ -1,3 +1,4 @@
node_modules/
*.tsbuildinfo
dist
dist
languages/

View File

@@ -47,7 +47,7 @@
"react-colorful": "^5.5.1",
"react-modal": "^3.15.1",
"redent": "^4.0.0",
"refractor": "^4.7.0",
"refractor": "^4.8.0",
"strip-indent": "^4.0.0",
"tinycolor2": "^1.4.2",
"unfurl.js": "^5.7.0",

View File

@@ -47,7 +47,7 @@
"react-colorful": "^5.5.1",
"react-modal": "^3.15.1",
"redent": "^4.0.0",
"refractor": "^4.7.0",
"refractor": "^4.8.0",
"strip-indent": "^4.0.0",
"tinycolor2": "^1.4.2",
"unfurl.js": "^5.7.0",

View File

@@ -18,6 +18,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import "zx/globals";
import { langen } from "./langen.mjs";
const ROOT_DIR = path.resolve(path.join(__dirname, ".."));
@@ -37,4 +38,11 @@ for (const name in pathsToCopy) {
});
}
const languagesList = await langen(ROOT_DIR, path.join(ROOT_DIR, "languages"));
fs.writeFileSync(
path.join(ROOT_DIR, "src", "extensions", "code-block", "languages.json"),
JSON.stringify(languagesList)
);
await $`cd ${ROOT_DIR} && npx tsc ${process.argv.slice(3)}`;

View File

@@ -23,16 +23,32 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
require("isomorphic-fetch");
async function main() {
export async function langen(rootDirectory, saveDirectory) {
if (process.env.CI) return;
const response = await fetch(
"https://github.com/PrismJS/prism/raw/master/components.json"
);
if (!response.ok) return;
if (!fs.existsSync(saveDirectory))
fs.mkdirSync(saveDirectory, { recursive: true });
const json = await response.json();
let output = [];
for (const key in json.languages) {
if (key === "meta") continue;
const language = json.languages[key];
const languagePath = path.join(
rootDirectory,
"node_modules",
"refractor",
"lang",
`${key}.js`
);
if (!fs.existsSync(languagePath)) continue;
output.push({
filename: key,
title: language.title,
@@ -42,7 +58,10 @@ async function main() {
: [language.alias]
: undefined
});
fs.copyFileSync(languagePath, path.join(saveDirectory, `${key}.js`));
}
console.log(JSON.stringify(output));
return output;
}
main();
// main();

File diff suppressed because one or more lines are too long