Don’t apply Starlight code blocks in non-starlight routes #2820
Replies: 4 comments 7 replies
-
Thanks for the issue @jpal91 — this is due to Expressive Code running for all Markdown/MDX content in a project, which is semi expected, but I understand sometimes doesn't work if a site has distinct theming for different sections. First up a couple of workarounds:
Regarding the general issue: it may be possible for Expressive Code to skip certain files with an added configuration options. During Markdown processing, plugins like this have access to the current file path, so in theory it could be possible for a plugin to have an option e.g. to only apply to |
Beta Was this translation helpful? Give feedback.
-
Thank you for bringing this up. I understand the issue - the current implementation of Starlight's Expressive Code integration doesn't account for "hybrid projects" like this. Also, the last time I had a look at such projects, they were not fully supported by Starlight either and the recommendation was to set up a separate Starlight project for docs. Has this changed and limiting Starlight to a subpath of an existing site fully works and is supported? The solution of setting If you want to keep using Expressive Code in the Starlight part of your site, but use something else for the rest of your site, I'd need to change the implementation of both |
Beta Was this translation helpful? Give feedback.
-
Hmm, as far as I know, we’ve always supported this! There’s no special code involved, but we document it here: https://starlight.astro.build/manual-setup/#use-starlight-at-a-subpath If EC offered a path matching option, then Starlight could in theory tell EC to only apply to Given this, I think I’ll move this issue to our feature requests as it will likely rely on some bigger upstream work. |
Beta Was this translation helpful? Give feedback.
-
I want to thank everyone that responded back to this to provide feedback/insight. I don't have the time tonight to try out some of these suggestions and report back, but I should be able to tomorrow. I do appreciate everyone sharing their expertise. Again, thank you for all the hard work! The limited experience I have with a Starlight page was pretty amazing, so I know this is definitely something I want to try to get integrated into a project. |
Beta Was this translation helpful? Give feedback.
-
What version of
starlight
are you using?0.31.1
What version of
astro
are you using?5.1.7
What package manager are you using?
pnpm
What operating system are you using?
Linux
What browser are you using?
Chrome
Describe the Bug
Expected
Starlight will only apply styling to code blocks in Starlight routes when integrated in an existing project.
Actual
When attempting to integrate Starlight in an existing project, Starlight overrides existing styling/
mdx
in non-Starlight routes.Explanation
In the included example, I added a new page in the blog collection which has a code block. The code block is derived from a custom
mdx
component and included in thecomponents
props insrc/pages/blog/[...slug].astro
. This should override anypre
tags and render the custom component instead.After the integration is added, the code block gets wrapped in the
.expressive-code
div which appears to strip all existing styling and add a figure wrapper. You could theoretically dodocument.querySelector(...).replaceWith(...)
, but even then, the code block has already lost anyshiki
styling that would have been applied before.Reproduce
blog/example
. You will see a code block that has defaultshiki
styling and adaisyui
utility class.astro.config
, uncomment the starlight integration.blog/example
and you'll see the changes.script
blocksrc/components/Code.astro
to see the parent Starlight block replaced with the child. It looks back to normal, except noshiki
styling.Suggestions
Although I tried to look through the source code and figure out how and when Starlight's
Code
was being injected, or if it had something to do with overriding themdx
integration, I couldn't quite figure it out. Maybe if I had more time later, I could look through the source code if any of my suggestions are too difficult to implement as is. However, if any of the following are possible, that would be great!Code
interacts with the content collections in a different way than normal or overrides themdx
integration somehow.Code
component override-able. It appears thatCode
is the only component you can't override unfortunately. This could be an easy fix...pre
. This would require devs to apply everything manually, but it could work.I would really love to avoid having to create a separate project and also have to deal with subdomains to add docs to a site, but if Starlight is going to impact other routes unrelated to it, it seems like there isn't any other option. If I'm missing something totally obvious, though, please let me know as that would make me very happy!
Lastly, I am a big fan of the project and really appreciate all the hard work every one is doing on it!
Link to Minimal Reproducible Example
https://codesandbox.io/p/sandbox/goofy-wood-xnw3py
Participation
Beta Was this translation helpful? Give feedback.
All reactions