-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
Refactor @astrojs/prism, fix Prism component import not working #4114
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
274ad9c
Upgrade @astrojs/prism to a real package, fix component import not wo…
Princesseuh 002e9f9
Remove `@astrojs/prism` as a dependency of `astro`
Princesseuh 5cef996
Update lock file
Princesseuh 9046cec
Refactor to multiple files
Princesseuh 066538c
Oops, can't have astro imports run inside node
Princesseuh e115824
Follow Nate's suggestion on being minors instead of patchs
Princesseuh cd98a1c
Merge branch 'main' into fix-prism-package
Princesseuh ac6d609
Update lockfile
Princesseuh 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 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,8 @@ | ||
--- | ||
'astro': patch | ||
'@astrojs/prism': minor | ||
'@astrojs/mdx': minor | ||
'@astrojs/markdown-remark': minor | ||
--- | ||
|
||
Refactor `@astrojs/mdx` and `@astrojs/markdown-remark` to use `@astrojs/prism` instead of duplicating the code |
This file contains 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 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,31 @@ | ||
# @astrojs/prism | ||
|
||
Supports Prism highlighting in Astro projects | ||
|
||
## Component | ||
|
||
This package exports a component to support highlighting inside an Astro file. Example: | ||
|
||
```astro | ||
--- | ||
import { Prism } from "@astrojs/prism" | ||
--- | ||
|
||
<Prism lang="js" code={`const foo = 'bar';`} /> | ||
``` | ||
|
||
## Internal | ||
|
||
This package exports a `runHighlighterWithAstro` function to highlight while making sure the Astro language is loaded beforehand | ||
|
||
```typescript | ||
import { runHighlighterWithAstro } from '@astrojs/prism'; | ||
|
||
runHighlighterWithAstro(` | ||
--- | ||
const helloAstro = 'Hello, Astro!'; | ||
--- | ||
|
||
<div>{helloAstro}</div> | ||
`, 'astro'); | ||
``` |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains 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 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,42 @@ | ||
import Prism from 'prismjs'; | ||
import loadLanguages from 'prismjs/components/index.js'; | ||
import { addAstro } from './plugin.js'; | ||
|
||
const languageMap = new Map([['ts', 'typescript']]); | ||
|
||
export function runHighlighterWithAstro(lang: string | undefined, code: string) { | ||
let classLanguage = `language-${lang}`; | ||
|
||
if (!lang) { | ||
lang = 'plaintext'; | ||
} | ||
|
||
const ensureLoaded = (language: string) => { | ||
if (language && !Prism.languages[language]) { | ||
loadLanguages([language]); | ||
} | ||
}; | ||
|
||
if (languageMap.has(lang)) { | ||
ensureLoaded(languageMap.get(lang)!); | ||
} else if (lang === 'astro') { | ||
ensureLoaded('typescript'); | ||
addAstro(Prism); | ||
} else { | ||
ensureLoaded('markup-templating'); // Prism expects this to exist for a number of other langs | ||
ensureLoaded(lang); | ||
} | ||
|
||
if (lang && !Prism.languages[lang]) { | ||
// eslint-disable-next-line no-console | ||
console.warn(`Unable to load the language: ${lang}`); | ||
} | ||
|
||
const grammar = Prism.languages[lang]; | ||
let html = code; | ||
if (grammar) { | ||
html = Prism.highlight(code, grammar, lang); | ||
} | ||
|
||
return { classLanguage, html }; | ||
} |
This file contains 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,2 @@ | ||
// @ts-expect-error | ||
export { default as Prism } from '../Prism.astro'; |
This file contains 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 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,10 @@ | ||
{ | ||
"extends": "../../tsconfig.base.json", | ||
"include": ["src"], | ||
"compilerOptions": { | ||
"allowJs": true, | ||
"target": "ES2020", | ||
"module": "ES2020", | ||
"outDir": "./dist" | ||
} | ||
} |
This file contains 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 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 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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
While we're at it, is there a place that makes more sense than
packages/astro-prism
? I think that's leftover from when the package was literally namedastro-prism
. Maybe justpackages/prism
?