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 16 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
7 changes: 6 additions & 1 deletion packages/astro/components/Code.astro
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import type {
} from 'shikiji';
import { bundledLanguages } from 'shikiji/langs';
import { getCachedHighlighter } from '../dist/core/shiki.js';
import type { HTMLAttributes } from '../types';

interface Props {
/** The code to highlight. Required. */
Expand Down Expand Up @@ -49,6 +50,9 @@ interface Props {
* @default false
*/
inline?: boolean;

/** Additional props to pass to the pre element. */
additionalProps?: HTMLAttributes<any>;
florian-lefebvre marked this conversation as resolved.
Show resolved Hide resolved
}

const {
Expand All @@ -58,6 +62,7 @@ const {
experimentalThemes = {},
wrap = false,
inline = false,
...additionalProps
StandardGage marked this conversation as resolved.
Show resolved Hide resolved
} = Astro.props;

// shiki -> shikiji compat
Expand Down Expand Up @@ -87,7 +92,7 @@ const highlighter = await getCachedHighlighter({

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

<Fragment set:html={html} />
12 changes: 10 additions & 2 deletions packages/markdown/remark/src/shiki.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type { Properties } from 'hast';
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 }, additionalProps?:any): string;
StandardGage marked this conversation as resolved.
Show resolved Hide resolved
}

// TODO: Remove this special replacement in Astro 5
Expand Down Expand Up @@ -41,7 +41,7 @@ export async function createShikiHighlighter({
const loadedLanguages = highlighter.getLoadedLanguages();

return {
highlight(code, lang = 'plaintext', options) {
highlight(code, lang = 'plaintext', options, additionalProps?:any) {
StandardGage marked this conversation as resolved.
Show resolved Hide resolved
if (lang !== 'plaintext' && !loadedLanguages.includes(lang)) {
// eslint-disable-next-line no-console
console.warn(`[Shiki] The language "${lang}" doesn't exist, falling back to "plaintext".`);
Expand All @@ -57,6 +57,14 @@ export async function createShikiHighlighter({
transformers: [
{
pre(node) {
Object.entries(additionalProps).forEach(([key, value]) => {
if (key === 'class') {
node.properties[key] += ` ${value}`; // Append to class instead of replacing it
}
else {
node.properties[key] = value as string;
}
});
// Swap to `code` tag if inline
if (inline) {
node.tagName = 'code';
Expand Down
Loading