diff --git a/.changeset/nine-geckos-act.md b/.changeset/nine-geckos-act.md new file mode 100644 index 0000000000..164dbbfd56 --- /dev/null +++ b/.changeset/nine-geckos-act.md @@ -0,0 +1,5 @@ +--- +'astro': minor +--- + +Support `` to output inline code HTML (no `pre` tag) diff --git a/packages/astro/components/Code.astro b/packages/astro/components/Code.astro index 0c5f946afc..40f99bcd19 100644 --- a/packages/astro/components/Code.astro +++ b/packages/astro/components/Code.astro @@ -1,5 +1,6 @@ --- import type * as shiki from 'shiki'; +import { renderToHtml } from 'shiki'; import { getHighlighter } from './Shiki.js'; export interface Props { @@ -30,36 +31,60 @@ export interface Props { * @default false */ wrap?: boolean | null; + /** + * Generate inline code element only, without the pre element wrapper. + * + * @default false + */ + inline?: boolean; } -const { code, lang = 'plaintext', theme = 'github-dark', wrap = false } = Astro.props; - -/** Replace the shiki class name with a custom astro class name. */ -function repairShikiTheme(html: string): string { - // Replace "shiki" class naming with "astro" - html = html.replace(/
${children}`;
+		},
+		code({ children }) {
+			return inline ? children : `${children}`;
+		},
+	},
 });
-const html = repairShikiTheme(_html);
 ---
 
 
diff --git a/packages/astro/test/astro-component-code.test.js b/packages/astro/test/astro-component-code.test.js
index e721f94987..6a525f58d3 100644
--- a/packages/astro/test/astro-component-code.test.js
+++ b/packages/astro/test/astro-component-code.test.js
@@ -100,4 +100,14 @@ describe('', () => {
 		expect($('#lang > pre')).to.have.lengthOf(1);
 		expect($('#lang > pre > code span').length).to.equal(3);
 	});
+
+	it(' has no pre tag', async () => {
+		let html = await fixture.readFile('/inline/index.html');
+		const $ = cheerio.load(html);
+		const codeEl = $('.astro-code');
+
+		expect(codeEl.prop('tagName')).to.eq('CODE');
+		expect(codeEl.attr('style')).to.include('background-color:');
+		expect($('pre')).to.have.lengthOf(0);
+	});
 });
diff --git a/packages/astro/test/fixtures/astro-component-code/src/pages/inline.astro b/packages/astro/test/fixtures/astro-component-code/src/pages/inline.astro
new file mode 100644
index 0000000000..05422c8bf6
--- /dev/null
+++ b/packages/astro/test/fixtures/astro-component-code/src/pages/inline.astro
@@ -0,0 +1,10 @@
+---
+import {Code} from 'astro/components';
+---
+
+Code component
+
+  Simple:
+  
+
+