Fix double <pre> tag (#185)

This commit is contained in:
Drew Powers 2021-05-07 15:48:45 -06:00 committed by GitHub
parent 8f2a58534c
commit 9feffda850
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 14 additions and 7 deletions

View file

@ -0,0 +1,5 @@
---
'astro': patch
---
Bugfix: fixes double <pre> tags generated from markdown code blocks

View file

@ -8,7 +8,7 @@
"exports": {
".": "./astro.mjs",
"./snowpack-plugin": "./snowpack-plugin.cjs",
"./components/*.astro": "./components/*.astro",
"./components/*": "./components/*",
"./runtime/svelte": "./dist/frontend/runtime/svelte.js"
},
"imports": {

View file

@ -19,8 +19,11 @@ export default function (module: Script): Transformer {
html: {
Element: {
enter(node) {
if (node.name !== 'code') return;
const className = getAttrValue(node.attributes, 'class') || '';
if (node.name !== 'pre') return;
const codeEl = node.children && node.children[0];
if (!codeEl || codeEl.name !== 'code') return;
const className = getAttrValue(codeEl.attributes, 'class') || '';
const classes = className.split(' ');
let lang;
@ -33,10 +36,9 @@ export default function (module: Script): Transformer {
if (!lang) return;
let code;
if (node.children?.length) {
code = node.children[0].data;
}
let codeData = codeEl.children && codeEl.children[0];
if (!codeData) return;
let code = codeData.data as string;
const repl = {
start: 0,