-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor @astrojs/prism, fix Prism component import not working (#4114)
* Upgrade @astrojs/prism to a real package, fix component import not working * Remove `@astrojs/prism` as a dependency of `astro` * Update lock file * Refactor to multiple files * Oops, can't have astro imports run inside node * Follow Nate's suggestion on being minors instead of patchs * Update lockfile
- Loading branch information
1 parent
59aa8d4
commit 64432bc
Showing
17 changed files
with
148 additions
and
174 deletions.
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.