From cac4a321e814fb805eb0e3ced469e25261a50885 Mon Sep 17 00:00:00 2001 From: Bjorn Lu Date: Thu, 4 May 2023 00:15:00 +0800 Subject: [PATCH] Support `` to output inline code (#6959) * Support `` to output inline code * Fix typo * Fix typo again * Remove expected error --------- Co-authored-by: Matthew Phillips --- .changeset/nine-geckos-act.md | 5 ++ packages/astro/components/Code.astro | 67 +++++++++++++------ .../astro/test/astro-component-code.test.js | 10 +++ .../src/pages/inline.astro | 10 +++ 4 files changed, 71 insertions(+), 21 deletions(-) create mode 100644 .changeset/nine-geckos-act.md create mode 100644 packages/astro/test/fixtures/astro-component-code/src/pages/inline.astro diff --git a/.changeset/nine-geckos-act.md b/.changeset/nine-geckos-act.md new file mode 100644 index 000000000..164dbbfd5 --- /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 0c5f946af..40f99bcd1 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 e721f9498..6a525f58d 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 000000000..05422c8bf
--- /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:
+  
+
+