-
-
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
[MDX] Add Prism and Shiki support #4002
Merged
Merged
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
ebb2232
deps: add rehype-prism, shiki, rehype-pretty-code
bholmesdev 39f48dd
wip: apply rehype plugins depending on config
bholmesdev b4958f6
wip: cherry-pick jsx-runtime fix?
natemoo-re deecec2
deps: rehype-pretty-code -> shiki-twoslash, add rehype-raw
bholmesdev 07f4528
wip: add jsx-runtime fix
bholmesdev 435c84d
feat: get shiki working!
bholmesdev fd1922c
deps: add @astrojs/prism, prismjs, unist-util-visit
bholmesdev 67485e2
feat: add prism support
bholmesdev e0f5132
example: add small syntax highlight demo to with-mdx
bholmesdev 92a4a60
deps: remove rehype-prism
bholmesdev 515d851
chore: remove unused async
bholmesdev 2a7ac3d
Merge branch 'main' into feat/mdx-shiki
bholmesdev d0056a8
chore: add .test.js to all mdx tests
bholmesdev 65889fb
test: shiki, shikiConfig, prism
bholmesdev e732f01
fix: remove "is:raw" from prism output
bholmesdev 18cee51
docs: add syntax highlighting section
bholmesdev d05d846
chore: add changeset
bholmesdev 2a51360
nit: "Shiki config" -> Shiki config
bholmesdev af08eb8
Revert "wip: add jsx-runtime fix"
bholmesdev 90b7a83
Merge branch 'main' into feat/mdx-shiki
bholmesdev 7ab7794
docs: link to integration README from example
bholmesdev 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,5 @@ | ||
--- | ||
'@astrojs/mdx': minor | ||
--- | ||
|
||
Support Prism and Shiki syntax highlighting based on project config |
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
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,59 @@ | ||
// TODO: discuss extracting this file to @astrojs/prism | ||
import { addAstro } from '@astrojs/prism/internal'; | ||
import Prism from 'prismjs'; | ||
import loadLanguages from 'prismjs/components/index.js'; | ||
import { visit } from 'unist-util-visit'; | ||
|
||
const languageMap = new Map([['ts', 'typescript']]); | ||
|
||
function runHighlighter(lang: string, code: string) { | ||
let classLanguage = `language-${lang}`; | ||
|
||
if (lang == null) { | ||
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 }; | ||
} | ||
|
||
/** */ | ||
export default function remarkPrism() { | ||
return (tree: any) => visit(tree, 'code', (node: any) => { | ||
let { lang, value } = node; | ||
node.type = 'html'; | ||
|
||
let { html, classLanguage } = runHighlighter(lang, value); | ||
let classes = [classLanguage]; | ||
node.value = `<pre class="${classes.join( | ||
' ' | ||
)}"><code class="${classLanguage}">${html}</code></pre>`; | ||
return node; | ||
}); | ||
} |
9 changes: 9 additions & 0 deletions
9
...ges/integrations/mdx/test/fixtures/mdx-syntax-hightlighting/src/pages/index.mdx
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,9 @@ | ||
# Syntax highlighting | ||
|
||
```astro | ||
--- | ||
const handlesAstroSyntax = true | ||
--- | ||
|
||
<h1>{handlesAstroSyntax}</h1> | ||
``` |
File renamed without changes.
67 changes: 67 additions & 0 deletions
67
packages/integrations/mdx/test/mdx-syntax-highlighting.test.js
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,67 @@ | ||
import mdx from '@astrojs/mdx'; | ||
|
||
import { expect } from 'chai'; | ||
import { parseHTML } from 'linkedom'; | ||
import { loadFixture } from '../../../astro/test/test-utils.js'; | ||
|
||
const FIXTURE_ROOT = new URL('./fixtures/mdx-syntax-hightlighting/', import.meta.url); | ||
|
||
describe('MDX syntax highlighting', () => { | ||
describe('shiki', () => { | ||
it('works', async () => { | ||
const fixture = await loadFixture({ | ||
root: FIXTURE_ROOT, | ||
markdown: { | ||
syntaxHighlight: 'shiki', | ||
}, | ||
integrations: [mdx()], | ||
}); | ||
await fixture.build(); | ||
|
||
const html = await fixture.readFile('/index.html'); | ||
const { document } = parseHTML(html); | ||
|
||
const shikiCodeBlock = document.querySelector('pre.shiki'); | ||
expect(shikiCodeBlock).to.not.be.null; | ||
}); | ||
|
||
it('respects markdown.shikiConfig.theme', async () => { | ||
const fixture = await loadFixture({ | ||
root: FIXTURE_ROOT, | ||
markdown: { | ||
syntaxHighlight: 'shiki', | ||
shikiConfig: { | ||
theme: 'dracula', | ||
}, | ||
}, | ||
integrations: [mdx()], | ||
}); | ||
await fixture.build(); | ||
|
||
const html = await fixture.readFile('/index.html'); | ||
const { document } = parseHTML(html); | ||
|
||
const shikiCodeBlock = document.querySelector('pre.shiki.dracula'); | ||
expect(shikiCodeBlock).to.not.be.null; | ||
}); | ||
}); | ||
|
||
describe('prism', () => { | ||
it('works', async () => { | ||
const fixture = await loadFixture({ | ||
root: FIXTURE_ROOT, | ||
markdown: { | ||
syntaxHighlight: 'prism', | ||
}, | ||
integrations: [mdx()], | ||
}); | ||
await fixture.build(); | ||
|
||
const html = await fixture.readFile('/index.html'); | ||
const { document } = parseHTML(html); | ||
|
||
const prismCodeBlock = document.querySelector('pre.language-astro'); | ||
expect(prismCodeBlock).to.not.be.null; | ||
}); | ||
}); | ||
}); |
File renamed without changes.
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.
Tagging @sarah11918 for proofing!