diff --git a/.changeset/four-plums-train.md b/.changeset/four-plums-train.md new file mode 100644 index 0000000000..ed84751640 --- /dev/null +++ b/.changeset/four-plums-train.md @@ -0,0 +1,6 @@ +--- +'astro': patch +'@astrojs/markdown-support': patch +--- + +Fix parsing of an empty `
` tag in markdown files, which expected the pre tag to have a child
diff --git a/packages/astro/test/astro-markdown.test.js b/packages/astro/test/astro-markdown.test.js
index b0a1dba4d9..a10be66cf3 100644
--- a/packages/astro/test/astro-markdown.test.js
+++ b/packages/astro/test/astro-markdown.test.js
@@ -35,6 +35,17 @@ Markdown('Runs code blocks through syntax highlighter', async ({ runtime }) => {
   assert.ok($el.length > 0, 'There are child spans in code blocks');
 });
 
+Markdown('Empty code blocks do not fail', async ({ runtime }) => {
+  const result = await runtime.load('/empty-code');
+  assert.ok(!result.error, `build error: ${result.error}`);
+
+  const $ = doc(result.contents);
+  
+  const $el = $('pre');
+  assert.ok($el[0].children.length === 1, "There is not a `` in the codeblock");
+  assert.ok($el[1].children.length === 0, "The empty `
` failed to render");
+});
+
 Markdown('Scoped styles should not break syntax highlight', async ({ runtime }) => {
   const result = await runtime.load('/scopedStyles-code');
   assert.ok(!result.error, `build error: ${result.error}`);
diff --git a/packages/astro/test/fixtures/astro-markdown/src/pages/empty-code.md b/packages/astro/test/fixtures/astro-markdown/src/pages/empty-code.md
new file mode 100644
index 0000000000..93cb4eedb8
--- /dev/null
+++ b/packages/astro/test/fixtures/astro-markdown/src/pages/empty-code.md
@@ -0,0 +1,20 @@
+---
+title: My Blog Post
+layout: ../layouts/content.astro
+---
+
+## Title
+
+Hello world
+
+With this in the body ---
+
+## Another
+
+more content
+
+```
+
+```
+
+

\ No newline at end of file
diff --git a/packages/markdown-support/src/codeblock.ts b/packages/markdown-support/src/codeblock.ts
index 32adc0151c..3f0c2894dd 100644
--- a/packages/markdown-support/src/codeblock.ts
+++ b/packages/markdown-support/src/codeblock.ts
@@ -35,6 +35,7 @@ export function rehypeCodeBlock() {
       }
 
       if (node.tagName !== 'pre') return;
+      if (!node.children[0]) return;
       const code = node.children[0];
       if (code.type !== 'element' || code.tagName !== 'code') return;
       node.properties = { ...code.properties };