-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
Add Shiki as an alternative to Prism #2497
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
3468bc4
[ci] yarn format
JuanM04 d0a7c20
Merge remote-tracking branch 'upstream/main'
JuanM04 39ce4b5
Merge remote-tracking branch 'upstream/main'
JuanM04 cea7403
Merge remote-tracking branch 'upstream/main'
JuanM04 d6a369f
Added shiki to markdown-remark
JuanM04 11fdc59
Upgraded astro shiki
JuanM04 e223e35
Added minimal example
JuanM04 8afd8b9
Changed defaults to match <Code />
JuanM04 60b3cca
Replace `shiki` with `astro` classes
JuanM04 7cf77e3
Added documentation
JuanM04 d3b13a4
Updated Astro code to use new `codeToHtml`
JuanM04 eabd9cf
Added changesets
JuanM04 1444ad8
Added basic test
JuanM04 57eb46a
Updated tests a bit
JuanM04 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/markdown-remark': patch | ||
--- | ||
|
||
Add Shiki as an alternative to Prism |
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 @@ | ||
--- | ||
'astro': patch | ||
--- | ||
|
||
Bumped Shiki version |
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,17 @@ | ||
# build output | ||
dist | ||
|
||
# dependencies | ||
node_modules/ | ||
|
||
# logs | ||
npm-debug.log* | ||
yarn-debug.log* | ||
yarn-error.log* | ||
|
||
# environment variables | ||
.env | ||
.env.production | ||
|
||
# macOS-specific files | ||
.DS_Store |
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 @@ | ||
## force pnpm to hoist | ||
shamefully-hoist = true |
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,6 @@ | ||
{ | ||
"startCommand": "npm start", | ||
"env": { | ||
"ENABLE_CJS_IMPORTS": true | ||
} | ||
} |
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,12 @@ | ||
# Astro Example: Markdown with Shiki | ||
|
||
``` | ||
npm init astro -- --template with-markdown-shiki | ||
``` | ||
|
||
[](https://stackblitz.com/github/withastro/astro/tree/latest/examples/with-markdown) | ||
|
||
This example showcases Astro's [built-in Markdown support](../../docs/markdown.md). | ||
|
||
- `src/pages/index.astro` uses Astro's `<Markdown>` component. | ||
- `src/pages/other.md` is a treated as a page entrypoint and uses a `layout`. |
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,22 @@ | ||
// Full Astro Configuration API Documentation: | ||
// https://docs.astro.build/reference/configuration-reference | ||
|
||
// @type-check enabled! | ||
// VSCode and other TypeScript-enabled text editors will provide auto-completion, | ||
// helpful tooltips, and warnings if your exported object is invalid. | ||
// You can disable this by removing "@ts-check" and `@type` comments below. | ||
import astroRemark from '@astrojs/markdown-remark'; | ||
|
||
// @ts-check | ||
export default /** @type {import('astro').AstroUserConfig} */ ({ | ||
// Enable Custom Markdown options, plugins, etc. | ||
markdownOptions: { | ||
render: [ | ||
astroRemark, | ||
{ | ||
syntaxHighlight: 'shiki', | ||
shikiTheme: 'dracula', | ||
}, | ||
], | ||
}, | ||
}); |
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,14 @@ | ||
{ | ||
"name": "@example/with-markdown-shiki", | ||
"version": "0.0.1", | ||
"private": true, | ||
"scripts": { | ||
"dev": "astro dev", | ||
"start": "astro dev", | ||
"build": "astro build", | ||
"preview": "astro preview" | ||
}, | ||
"devDependencies": { | ||
"astro": "^0.22.20" | ||
} | ||
} |
Binary file not shown.
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,11 @@ | ||
{ | ||
"infiniteLoopProtection": true, | ||
"hardReloadOnChange": false, | ||
"view": "browser", | ||
"template": "node", | ||
"container": { | ||
"port": 3000, | ||
"startScript": "start", | ||
"node": "14" | ||
} | ||
} |
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,20 @@ | ||
--- | ||
const { content } = Astro.props; | ||
--- | ||
|
||
<html lang={content.lang || 'en'}> | ||
<head> | ||
<meta charset="utf-8" /> | ||
|
||
<link rel="icon" type="image/x-icon" href="/favicon.ico" /> | ||
|
||
<title>{content.title}</title> | ||
|
||
<style global> | ||
@import "../styles/global.css"; | ||
</style> | ||
</head> | ||
<body> | ||
<slot /> | ||
</body> | ||
</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,14 @@ | ||
--- | ||
title: Shiki demo | ||
layout: ../layouts/main.astro | ||
--- | ||
|
||
# Shiki demo | ||
|
||
```js | ||
var foo = 'bar'; | ||
|
||
function doSomething() { | ||
return foo; | ||
} | ||
``` |
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,54 @@ | ||
pre, | ||
code { | ||
color: #d4d4d4; | ||
font-size: 14px; | ||
font-family: Consolas, Monaco, 'Andale Mono', 'Ubuntu Mono', monospace; | ||
line-height: 1.5; | ||
direction: ltr; | ||
white-space: pre; | ||
text-align: left; | ||
text-shadow: none; | ||
word-break: normal; | ||
word-spacing: normal; | ||
-moz-tab-size: 4; | ||
-o-tab-size: 4; | ||
tab-size: 4; | ||
-webkit-hyphens: none; | ||
-moz-hyphens: none; | ||
-ms-hyphens: none; | ||
hyphens: none; | ||
} | ||
|
||
pre::selection, | ||
code::selection { | ||
text-shadow: none; | ||
background: #b3d4fc; | ||
} | ||
|
||
@media print { | ||
pre, | ||
code { | ||
text-shadow: none; | ||
} | ||
} | ||
|
||
pre { | ||
margin: 0.5rem 0 16px; | ||
padding: 0.8rem 1rem 0.9rem; | ||
overflow: auto; | ||
background: #282a36; | ||
border-radius: 4px; | ||
} | ||
|
||
:not(pre) > code { | ||
padding: 0.1em 0.3em; | ||
color: #db4c69; | ||
background: #f9f2f4; | ||
border-radius: 0.3em; | ||
white-space: pre-wrap; | ||
} | ||
|
||
body { | ||
max-width: 900px; | ||
margin: auto; | ||
} |
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 @@ | ||
{ | ||
"compilerOptions": { | ||
"moduleResolution": "node" | ||
} | ||
} |
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,53 @@ | ||
import { expect } from 'chai'; | ||
import cheerio from 'cheerio'; | ||
import { loadFixture } from './test-utils.js'; | ||
import markdownRemark from '@astrojs/markdown-remark'; | ||
|
||
describe('Astro Markdown Shiki', () => { | ||
let fixture; | ||
|
||
before(async () => { | ||
fixture = await loadFixture({ | ||
projectRoot: './fixtures/astro-markdown-shiki/', | ||
markdownOptions: { | ||
render: [ | ||
markdownRemark, | ||
{ | ||
syntaxHighlight: 'shiki', | ||
shikiTheme: 'github-light', | ||
}, | ||
], | ||
}, | ||
buildOptions: { | ||
sitemap: false, | ||
}, | ||
}); | ||
await fixture.build(); | ||
}); | ||
|
||
it('Can render markdown with shiki', async () => { | ||
const html = await fixture.readFile('/index.html'); | ||
const $ = cheerio.load(html); | ||
|
||
// There should be no HTML from Prism | ||
expect($('.token')).to.have.lengthOf(0); | ||
|
||
expect($('pre')).to.have.lengthOf(1); | ||
expect($('pre').hasClass('astro-code')).to.equal(true); | ||
expect($('pre').attr().style).to.equal('background-color: #ffffff'); | ||
}); | ||
|
||
it('Can render Astro <Markdown> with shiki', async () => { | ||
const html = await fixture.readFile('/astro/index.html'); | ||
const $ = cheerio.load(html); | ||
|
||
// There should be no HTML from Prism | ||
expect($('.token')).to.have.lengthOf(0); | ||
|
||
expect($('pre')).to.have.lengthOf(2); | ||
|
||
expect($('span.line')).to.have.lengthOf(2); | ||
expect($('span.line').get(0).children).to.have.lengthOf(1); | ||
expect($('span.line').get(1).children).to.have.lengthOf(5); | ||
}); | ||
}); |
10 changes: 10 additions & 0 deletions
10
packages/astro/test/fixtures/astro-markdown-shiki/src/layouts/content.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<html> | ||
<head> | ||
<!-- Head Stuff --> | ||
</head> | ||
<body> | ||
<div class="container"> | ||
<slot></slot> | ||
</div> | ||
</body> | ||
</html> |
18 changes: 18 additions & 0 deletions
18
packages/astro/test/fixtures/astro-markdown-shiki/src/pages/astro.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
--- | ||
import { Markdown } from 'astro/components'; | ||
import Layout from '../layouts/content.astro'; | ||
--- | ||
|
||
<Layout> | ||
<Markdown> | ||
# Hello world | ||
|
||
``` | ||
plaintext | ||
``` | ||
|
||
```js | ||
console.log('JavaScript') | ||
``` | ||
</Markdown> | ||
</Layout> |
24 changes: 24 additions & 0 deletions
24
packages/astro/test/fixtures/astro-markdown-shiki/src/pages/index.md
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,24 @@ | ||
--- | ||
layout: ../layouts/content.astro | ||
--- | ||
|
||
# Hello world | ||
|
||
```yaml | ||
apiVersion: v3 | ||
kind: Pod | ||
metadata: | ||
name: rss-site | ||
labels: | ||
app: web | ||
spec: | ||
containers: | ||
- name: front-end | ||
image: nginx | ||
ports: | ||
- containerPort: 80 | ||
- name: rss-reader | ||
image: nickchase/rss-php-nginx:v1 | ||
ports: | ||
- containerPort: 88 | ||
``` |
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.
Curious why this changed. Was this breaking? Or is this an optional way to pass the lang?
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.
Looks like they are both valid. I am fine with this change given that.
https://github.com/shikijs/shiki/blob/f462aa9bc0fcacd2e86513e7efb267aaa6e17afe/packages/shiki/src/highlighter.ts#L127-L137
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.
It's not breaking, it's just deprecated: https://github.com/shikijs/shiki/blob/e40647c00a036c14ae5e28dfde007f59a6485c37/packages/shiki/src/types.ts#L36