summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Caleb Jasik <calebjasik@jasik.xyz> 2021-09-07 15:22:23 -0500
committerGravatar GitHub <noreply@github.com> 2021-09-07 15:22:23 -0500
commit00fd7ca4dc4d2591eb31887e4b59f4ed312530ef (patch)
tree0da5d049097bd8c485e63f89258f54020fc16722
parent105be57f8077adde9dee39adc05537b058d52097 (diff)
downloadastro-00fd7ca4dc4d2591eb31887e4b59f4ed312530ef.tar.gz
astro-00fd7ca4dc4d2591eb31887e4b59f4ed312530ef.tar.zst
astro-00fd7ca4dc4d2591eb31887e4b59f4ed312530ef.zip
Fix parsing of an empty literal `<pre></pre>` in markdown source (#1332)
-rw-r--r--.changeset/four-plums-train.md6
-rw-r--r--packages/astro/test/astro-markdown.test.js11
-rw-r--r--packages/astro/test/fixtures/astro-markdown/src/pages/empty-code.md20
-rw-r--r--packages/markdown-support/src/codeblock.ts1
4 files changed, 38 insertions, 0 deletions
diff --git a/.changeset/four-plums-train.md b/.changeset/four-plums-train.md
new file mode 100644
index 000000000..ed8475164
--- /dev/null
+++ b/.changeset/four-plums-train.md
@@ -0,0 +1,6 @@
+---
+'astro': patch
+'@astrojs/markdown-support': patch
+---
+
+Fix parsing of an empty `<pre></pre>` 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 b0a1dba4d..a10be66cf 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 `<code>` in the codeblock");
+ assert.ok($el[1].children.length === 0, "The empty `<pre>` 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 000000000..93cb4eedb
--- /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
+
+```
+
+```
+
+<pre></pre> \ No newline at end of file
diff --git a/packages/markdown-support/src/codeblock.ts b/packages/markdown-support/src/codeblock.ts
index 32adc0151..3f0c2894d 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 };