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

Allow Code Component to have custom props and extra class styles #9960

Merged
merged 30 commits into from
Mar 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
e24c9ff
Create simple react element if element has no children
StandardGage Feb 2, 2024
51b8e95
Fix for when element has text
StandardGage Feb 2, 2024
c0d456a
add changeset
StandardGage Feb 3, 2024
38d3157
Merge branch 'main' into main
StandardGage Feb 3, 2024
5714a5c
Add additionalProps to Code component and ShikiHighlighter.highlight()
StandardGage Feb 3, 2024
a1d2531
Add changeset
StandardGage Feb 3, 2024
1a2b68f
Create simple react element if element has no children
StandardGage Feb 2, 2024
da2dd6f
Fix for when element has text
StandardGage Feb 2, 2024
fd2dda6
add changeset
StandardGage Feb 3, 2024
d2b73e0
Add additionalProps to Code component and ShikiHighlighter.highlight()
StandardGage Feb 3, 2024
7ee9553
Add changeset
StandardGage Feb 3, 2024
ad722cd
Merge branch '9009' of https://github.com/StandardGage/astro into 9009
StandardGage Feb 3, 2024
27c6dd1
reverted accidental changes
StandardGage Feb 3, 2024
bd8e2c0
remove unnecessary parts
StandardGage Feb 3, 2024
9bfc65b
Add HTMLAttributes type to additionalProps
StandardGage Feb 3, 2024
ada4126
Update .changeset/calm-bags-deliver.md
StandardGage Feb 3, 2024
e6b100c
extend HTMLAtts instead
StandardGage Feb 4, 2024
f3bc875
Merge branch '9009' of https://github.com/StandardGage/astro into 9009
StandardGage Feb 4, 2024
6e7e9f7
Merge branch 'main' of https://github.com/withastro/astro into 9009
StandardGage Feb 5, 2024
45d14c7
add suggestions
StandardGage Feb 5, 2024
60a01c9
Merge branch 'main' into 9009
florian-lefebvre Feb 5, 2024
7fe8bf2
Merge branch 'withastro:main' into 9009
StandardGage Feb 26, 2024
96ca67a
Merge branch 'main' into 9009
florian-lefebvre Feb 26, 2024
1a5c155
feat: address reviews
florian-lefebvre Feb 26, 2024
9c7323c
chore: remove empty line
florian-lefebvre Feb 26, 2024
34e38a7
Merge branch 'main' into 9009
florian-lefebvre Feb 26, 2024
fe40448
Merge branch 'main' into 9009
florian-lefebvre Feb 26, 2024
beb1786
feat: move attributes to options
florian-lefebvre Feb 26, 2024
ab44e59
Merge branch 'main' into 9009
ematipico Mar 8, 2024
c3ca52d
Merge branch 'main' into 9009
ematipico Mar 8, 2024
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
6 changes: 6 additions & 0 deletions .changeset/calm-bags-deliver.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@astrojs/markdown-remark": minor
"astro": minor
---

Allows passing any props to the `<Code />` component
5 changes: 4 additions & 1 deletion packages/astro/components/Code.astro
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ import type {
} from 'shiki';
import { bundledLanguages } from 'shiki/langs';
import { getCachedHighlighter } from '../dist/core/shiki.js';
import type { HTMLAttributes } from '../types';

interface Props {
interface Props extends Omit<HTMLAttributes<'pre'>, 'lang'> {
/** The code to highlight. Required. */
code: string;
/**
Expand Down Expand Up @@ -58,6 +59,7 @@ const {
themes = {},
wrap = false,
inline = false,
...rest
} = Astro.props;

// shiki 1.0 compat
Expand Down Expand Up @@ -87,6 +89,7 @@ const highlighter = await getCachedHighlighter({

const html = highlighter.highlight(code, typeof lang === 'string' ? lang : lang.name, {
inline,
attributes: rest,
});
---

Expand Down
17 changes: 14 additions & 3 deletions packages/markdown/remark/src/shiki.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ import { visit } from 'unist-util-visit';
import type { ShikiConfig } from './types.js';

export interface ShikiHighlighter {
highlight(code: string, lang?: string, options?: { inline?: boolean }): string;
highlight(
code: string,
lang?: string,
options?: { inline?: boolean; attributes?: Record<string, string> }
): string;
}

// TODO: Remove this special replacement in Astro 5
Expand Down Expand Up @@ -60,8 +64,15 @@ export async function createShikiHighlighter({
node.tagName = 'code';
}

const classValue = normalizePropAsString(node.properties.class) ?? '';
const styleValue = normalizePropAsString(node.properties.style) ?? '';
const { class: attributesClass, style: attributesStyle, ...rest } = options?.attributes ?? {};
Object.assign(node.properties, rest);

const classValue =
(normalizePropAsString(node.properties.class) ?? '') +
(attributesClass ? ` ${attributesClass}` : '');
const styleValue =
(normalizePropAsString(node.properties.style) ?? '') +
(attributesStyle ? `; ${attributesStyle}` : '');

// Replace "shiki" class naming with "astro-code"
node.properties.class = classValue.replace(/shiki/g, 'astro-code');
Expand Down
Loading