|
| 1 | +// // This code is from prism-react-renderer: |
| 2 | +// // https://github.com/FormidableLabs/prism-react-renderer/blob/master/packages/generate-prism-languages/index.ts |
| 3 | +// // Thanks @FormidableLabs. |
| 4 | + |
| 5 | +// // @ts-ignore |
| 6 | +// import flowRight from 'lodash.flowright'; |
| 7 | +// import * as pc from 'picocolors'; |
| 8 | +// import { readFile, writeFile, access } from 'node:fs/promises'; |
| 9 | +// import { constants } from 'node:fs'; |
| 10 | +// import { join, dirname } from 'node:path'; |
| 11 | +// import { languages as prismLanguages } from 'prismjs/components'; |
| 12 | +// import * as uglify from 'uglify-js'; |
| 13 | + |
| 14 | +// export const languagesToBundle = <const>[ |
| 15 | +// 'markup', |
| 16 | +// 'jsx', |
| 17 | +// 'tsx', |
| 18 | +// 'swift', |
| 19 | +// 'kotlin', |
| 20 | +// 'objectivec', |
| 21 | +// 'js-extras', |
| 22 | +// 'reason', |
| 23 | +// 'rust', |
| 24 | +// 'graphql', |
| 25 | +// 'yaml', |
| 26 | +// 'go', |
| 27 | +// 'cpp', |
| 28 | +// 'markdown', |
| 29 | +// 'python', |
| 30 | +// 'json' |
| 31 | +// ]; |
| 32 | + |
| 33 | +// /** |
| 34 | +// * We need to disable typechecking on this generated file as it's just concatenating JS code |
| 35 | +// * that starts off assuming Prism lives in global scope. We also need to provide Prism as that |
| 36 | +// * gets passed into an iffe preventing us from needing to use global scope. |
| 37 | +// */ |
| 38 | +// const header = `// eslint-disable-next-line @typescript-eslint/ban-ts-comment\n// @ts-nocheck\nimport * as Prism from "prismjs";\nexport { Prism };`; |
| 39 | +// const prismPath = dirname(require.resolve('prismjs')); |
| 40 | + |
| 41 | +// const readLanguageFile = async (language: string): Promise<string> => { |
| 42 | +// const pathToLanguage = join(prismPath, `components/prism-${language}.js`); |
| 43 | +// await access(pathToLanguage, constants.R_OK); |
| 44 | +// const buffer = await readFile(pathToLanguage, { encoding: 'utf-8' }); |
| 45 | +// return buffer.toString(); |
| 46 | +// }; |
| 47 | + |
| 48 | +// const strArrayFromUnknown = (input: unknown) => (array: string[]) => { |
| 49 | +// if (typeof input === 'string') array.push(input); |
| 50 | +// else if (Array.isArray(input)) array = array.concat(input); |
| 51 | +// return array; |
| 52 | +// }; |
| 53 | + |
| 54 | +// const main = async () => { |
| 55 | +// let output = ''; |
| 56 | +// const bundledLanguages = new Set<keyof typeof prismLanguages>(); |
| 57 | +// const orderBundled = new Set<keyof typeof prismLanguages>(); |
| 58 | +// const outputPath = join(__dirname, '../prism-langs.ts'); |
| 59 | + |
| 60 | +// const addLanguageToOutput = async (language?: string) => { |
| 61 | +// if (bundledLanguages.has(language)) { |
| 62 | +// return; |
| 63 | +// } |
| 64 | +// if (language == null || prismLanguages[language] == null) { |
| 65 | +// return; |
| 66 | +// } |
| 67 | +// bundledLanguages.add(language); |
| 68 | + |
| 69 | +// /** |
| 70 | +// * We need to ensure any language dependencies are bundled first |
| 71 | +// */ |
| 72 | +// const prismLang = prismLanguages[language]; |
| 73 | +// const deps = flowRight( |
| 74 | +// strArrayFromUnknown(prismLang.require), |
| 75 | +// strArrayFromUnknown(prismLang.optional) |
| 76 | +// )([]); |
| 77 | +// const peerDeps = strArrayFromUnknown(prismLang.peerDependencies)([]); |
| 78 | + |
| 79 | +// for await (const language of deps) { |
| 80 | +// await addLanguageToOutput(language); |
| 81 | +// } |
| 82 | + |
| 83 | +// output += await readLanguageFile(language); |
| 84 | +// orderBundled.add(language); |
| 85 | + |
| 86 | +// for await (const language of peerDeps) { |
| 87 | +// await addLanguageToOutput(language); |
| 88 | +// } |
| 89 | +// }; |
| 90 | + |
| 91 | +// for await (const language of languagesToBundle) { |
| 92 | +// await addLanguageToOutput(language); |
| 93 | +// } |
| 94 | + |
| 95 | +// console.info(pc.bold(pc.bgYellow(pc.black('Prism Renderer'))), '\n'); |
| 96 | +// console.info( |
| 97 | +// pc.bgBlue(`Generated TypeScript output at:`), |
| 98 | +// pc.cyan(outputPath) |
| 99 | +// ); |
| 100 | +// console.info( |
| 101 | +// pc.bgGreen(`Included language definitions in the following order:`), |
| 102 | +// Array.from(orderBundled.values()).join(', ') |
| 103 | +// ); |
| 104 | + |
| 105 | +// await writeFile(outputPath, header + uglify.minify(output).code); |
| 106 | +// }; |
| 107 | + |
| 108 | +// main(); |
0 commit comments