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