-
-
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
feat: Added the ability to add custom themes/languages to Shiki #2518
Conversation
🦋 Changeset detectedLatest commit: 321a468 The changes in this PR will be included in the next version bump. This PR includes changesets to release 2 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
✔️ Deploy Preview for astro-docs-2 ready! 🔨 Explore the source changes: ffbe290 🔍 Inspect the deploy log: https://app.netlify.com/sites/astro-docs-2/deploys/61fc0fd6fa3f020008a82611 😎 Browse the preview: https://deploy-preview-2518--astro-docs-2.netlify.app/en/guides/markdown-content |
It seems like there are some timeout errors that aren't related to this PR. The difference between the latest two commits is in the indentation, and in the first commit windows fails and in the second macos fails |
@JuanM04 the OSX test can be a little flakey, so I've rerun it. |
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.
The changesets have Shiki probably misspelled as "Shika"
@obnoxiousnerd thanks! @matthewp In the latest re-run that you've made, Windows threw this error:
Edit: Edit 2: no, it was a race condition. |
Nice! Thanks @JuanM04 |
@@ -6,6 +6,9 @@ | |||
// 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'; | |||
import fs from 'fs'; | |||
|
|||
const riGrammar = JSON.parse(fs.readFileSync('./src/shiki/ri.tmLanguage.json')); |
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.
this feels like a really advanced case, I feel like most users just want to change the theme, and not add a custom lang. I'd recommend removing all changes from this example.
If you wanted to mention something about this, you could maybe instead link to the docs with something like:
{
syntaxHighlight: 'shiki',
shikiTheme: 'dracula',
+ // full documentation: ASTRO_DOCS_URL
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.
Yeah, that's a good appreciation
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.
I've made the changes. What do you think about them?
packages/astro/components/Code.astro
Outdated
@@ -43,7 +50,10 @@ function repairShikiTheme(html: string): string { | |||
} | |||
|
|||
const highlighter = await shiki.getHighlighter({ theme }); | |||
const _html = highlighter.codeToHtml(code, { lang }); | |||
if(typeof lang !== 'string') await highlighter.loadLanguage(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.
is it okay if this runs on every codeblock render? If not, can you do a check that the language hasn't already been loaded?
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.
The language is loaded to the highlighter instance. Because the instance is being generated on every render, so does the loadLanguage
function. I'll see if I can optimize it a bit more.
Edit: I've found a faster way to load only one language
const highlighter = await shiki.getHighlighter({ | ||
theme, | ||
// Load custom lang if passed an object, otherwise load the default | ||
langs: typeof lang !== 'string' ? [lang] : undefined |
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.
nice!
@@ -12,6 +44,13 @@ const remarkShiki = async (theme: shiki.Theme) => { | |||
html = html.replace('<pre class="shiki"', '<pre class="astro-code"'); | |||
// Replace "shiki" css variable naming with "astro". | |||
html = html.replace(/style="(background-)?color: var\(--shiki-/g, 'style="$1color: var(--astro-code-'); | |||
// Handle code wrapping | |||
// if wrap=null, do nothing. | |||
if (wrap === false) { |
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.
I would really like to not mess with the default style by default, unless we consider them broken. I would expect shiki's default output without changes to be fine / not overflow. Is that not the case / do we consider the default shiki output broken?
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.
Honestly, I don't know. I've just mimicked <Code />
behavior
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.
Right, but we don't currently add overflow-x
in <Code>
today, right? That's what I'm wondering about, since I hadn't heard this complaint / that a fix for this was needed and I generally like us to match default behaviors when we can.
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.
Right, but we don't currently add overflow-x in
<Code>
today, right?
Actually, yes. I double checked with a brand new app.
That's what I'm wondering about, since I hadn't heard this complaint
I'm as confused as you. So, I did a little research (thanks Linus for making git blame) and I found this comment: #1208 (comment)
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.
lol I did this??? Welp, nevermind then 🤦 I'm suprised that I didn't file this as a bug of Shiki then, maybe its trapped in one of my other issues on that repo.
Either way, thanks for digging! this LGTM then
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.
lol hahaha
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.
LGTM
uh oh, tests failing! |
@FredKSchott I've been fighting Windows tests for so long, and I don't know why some times they fail and some times they pass |
hmm, race condition? Did a recent commit introduce or has this always been happening? I'm about to spin up my own PR, will verify if its an issue on |
@FredKSchott I think it's because I do multiple builds with the same folder. But this isn't the only test that does that. Is there a way to force mocha/chai to run a suit synchronously? Also, it never happened on Linux nor Mac (although Mac sometimes throw an error in |
In fact, was a race problem. @FredKSchott, could you re-run the test another time just to be sure? Btw, I've read somewhere that Vitest lets you control concurrency in a test-by-test basis |
Hey @JuanM04. Like I’m mentioning in your other PR, we recently migrated our docs site over to https://github.com/withastro/docs I have another PR to remove our old version of the docs from this monorepo, #2517. I can make sure your work gets added into that repository. Would you be interested in making that PR? You don’t have to, but I would like you to get the commit credit. I can also attribute the improvement to you, but it would not be the same as a signed commit from your machine. |
LGTM! |
…astro#2518) * Replaced `shikiTheme` with `shikiConfig` * Code.astro now accepts custom themes/langs * Updated docs * Updated tests * Fixed language loading * Added customization examples * Updated documentation * Added more tests * Changelogs * Changed some spaces to tabs * Fixed typo in changesets * Moved tests fixtures * Rolled back changes to with-markdown-shiki * Removed lang example in docs * Optimized Code component * Try to fix windows errors * Try to see if this new tests work
Changes
wrap
to Astro Markdown shiki config<Code />
and Markdown plugin)Follow up to #2497
Testing
yarn test:match "Shiki|Code"
Docs
<Code />