Skip to content
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

fix: code component was missing support for meta string #11605

Merged
merged 6 commits into from
Aug 14, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions .changeset/odd-buttons-pay.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
---
"astro": minor
---

Adds a new property `meta` to Astro's built-in `<Code />` component.
ematipico marked this conversation as resolved.
Show resolved Hide resolved

This allows you to provide a value for [Shiki's `meta` attribute](https://shiki.style/guide/transformers#meta) to pass options to transformers.

The following example passes an option to highlight lines 1 and 3 to Shiki's `tranformerMetaHighlight`:

```astro
// src/pages/index.astro
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This probably would not be a page, since it doesn't have a layout and just calls a component, no?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure! We could fix this by changing it to be a component URL, or by adding some HTML comment for "missing stuff". I'll update to a component name, but the other would be fine if you want to suggest it!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is invalid Astro markup. Comments above the --- mark are not allowed.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

D'oh, great catch! I'm so spoiled living in the docs repo... (we always just use title= there)

---
sarah11918 marked this conversation as resolved.
Show resolved Hide resolved
import { Code } from "astro:components";
import { transformerMetaHighlight } from '@shikijs/transformers';
---
<Code
code={code}
lang="js"
transformers={[transformerMetaHighlight()]}
meta="{1,3}" />
```
9 changes: 9 additions & 0 deletions packages/astro/components/Code.astro
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@ interface Props extends Omit<HTMLAttributes<'pre'>, 'lang'> {
* @default "plaintext"
*/
lang?: BuiltinLanguage | SpecialLanguage | LanguageRegistration;
/**
* A metastring to pass to the highlighter.
* Allows passing information to transformers: https://shiki.style/guide/transformers#meta
*
* @default undefined
*/
meta?: string;
/**
* The styling theme.
* Supports all themes listed here: https://shiki.style/themes
Expand Down Expand Up @@ -72,6 +79,7 @@ interface Props extends Omit<HTMLAttributes<'pre'>, 'lang'> {
const {
code,
lang = 'plaintext',
meta,
theme = 'github-dark',
themes = {},
defaultColor = 'light',
Expand Down Expand Up @@ -110,6 +118,7 @@ const highlighter = await getCachedHighlighter({

const html = await highlighter.highlight(code, typeof lang === 'string' ? lang : lang.name, {
inline,
meta,
attributes: rest as any,
});
---
Expand Down
Loading