-
Notifications
You must be signed in to change notification settings - Fork 151
New Prism Language file generation script for v2 #186
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| import flowRight from "lodash.flowright" | ||
| import pc from "picocolors" | ||
| import { readFile, writeFile, access } from "node:fs/promises" | ||
| import { constants } from "node:fs" | ||
| import { join, dirname } from "node:path" | ||
| import { languages as prismLanguages } from "prismjs/components" | ||
| import uglify from "uglify-js" | ||
|
|
||
| export const languagesToBundle = <const>[ | ||
| "jsx", | ||
| "tsx", | ||
| "swift", | ||
| "kotlin", | ||
| "objectivec", | ||
| "rust", | ||
| "graphql", | ||
| "yaml", | ||
| "go", | ||
| "cpp", | ||
| "markdown", | ||
| ] | ||
|
|
||
| /** | ||
| * We need to disable typechecking on this generated file as it's just concatenating JS code | ||
| * that starts off assuming Prism lives in global scope. We also need to provide Prism as that | ||
| * gets passed into an iffe preventing us from needing to use global scope. | ||
| */ | ||
| const header = `// eslint-disable-next-line @typescript-eslint/ban-ts-comment\n// @ts-nocheck\nimport Prism from "prismjs"\n` | ||
| const prismPath = dirname(require.resolve("prismjs")) | ||
|
|
||
| const readLanguageFile = async (language: string): Promise<string> => { | ||
| const pathToLanguage = join(prismPath, `components/prism-${language}.js`) | ||
| await access(pathToLanguage, constants.R_OK) | ||
| const buffer = await readFile(pathToLanguage, { encoding: "utf-8" }) | ||
| return buffer.toString() | ||
| } | ||
|
|
||
| const strArrayFromUnknown = (input: unknown) => (array: string[]) => { | ||
| if (typeof input === "string") array.push(input) | ||
| else if (Array.isArray(input)) array = array.concat(input) | ||
| return array | ||
| } | ||
|
|
||
| const main = async () => { | ||
| let output = "" | ||
| const bundledLanguages = new Set<keyof typeof prismLanguages>() | ||
| const orderBundled = new Set<keyof typeof prismLanguages>() | ||
| const outputPath = join( | ||
| __dirname, | ||
| "../prism-react-renderer/src/prism-langs.ts" | ||
| ) | ||
|
|
||
| const addLanguageToOutput = async (language?: string) => { | ||
| if (bundledLanguages.has(language)) { | ||
| return | ||
| } | ||
| if (language == null || prismLanguages[language] == null) { | ||
| return | ||
| } | ||
| bundledLanguages.add(language) | ||
|
|
||
| /** | ||
| * We need to ensure any language dependencies are bundled first | ||
| */ | ||
| const prismLang = prismLanguages[language] | ||
| const deps = flowRight( | ||
| strArrayFromUnknown(prismLang.require), | ||
| strArrayFromUnknown(prismLang.optional) | ||
| )([]) | ||
| const peerDeps = strArrayFromUnknown(prismLang.peerDependencies)([]) | ||
|
|
||
| for await (const language of deps) { | ||
| await addLanguageToOutput(language) | ||
| } | ||
|
|
||
| output += await readLanguageFile(language) | ||
| orderBundled.add(language) | ||
|
|
||
| for await (const language of peerDeps) { | ||
| await addLanguageToOutput(language) | ||
| } | ||
| } | ||
|
|
||
| for await (const language of languagesToBundle) { | ||
| await addLanguageToOutput(language) | ||
| } | ||
|
|
||
| console.info( | ||
| pc.bold(pc.bgYellow(pc.black("Formidable Prism React Renderer"))), | ||
| "\n" | ||
| ) | ||
| console.info( | ||
| pc.bgBlue(`Generated TypeScript output at:`), | ||
| pc.cyan(outputPath) | ||
| ) | ||
| console.info( | ||
| pc.bgGreen(`Included language definitions in the following order:`), | ||
| Array.from(orderBundled.values()).join(", ") | ||
| ) | ||
|
|
||
| await writeFile(outputPath, header + uglify.minify(output).code) | ||
| } | ||
|
|
||
| main() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| { | ||
| "name": "generate-prism-languages", | ||
| "private": true, | ||
| "scripts": { | ||
| "generate": "ts-node ./index.ts" | ||
| }, | ||
| "peerDependencies": { | ||
| "react": ">=16.0.0" | ||
| }, | ||
| "devDependencies": { | ||
| "@types/lodash.flowright": "^3.5.7", | ||
| "@types/node": "^18.15.11", | ||
| "@types/prismjs": "^1.26.0", | ||
| "@types/uglify-js": "^3.17.1", | ||
| "picocolors": "^1.0.0", | ||
| "prismjs": "*", | ||
| "ts-node": "^10.9.1", | ||
| "tslib": "^2.5.0", | ||
| "typescript": "*", | ||
| "uglify-js": "^3.17.4", | ||
| "lodash.flowright": "^3.5.0" | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| { | ||
| "compilerOptions": { | ||
| "esModuleInterop": true, | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
100 changes: 0 additions & 100 deletions
100
packages/prism-react-renderer/scripts/generate-base-languages.ts
This file was deleted.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.