-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix components in markdown regressions (#3486)
Co-authored-by: Nate Moore <natemoo-re@users.noreply.github.com>
- Loading branch information
1 parent
e02c72f
commit 119ecf8
Showing
9 changed files
with
171 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
'astro': patch | ||
'@astrojs/markdown-remark': patch | ||
--- | ||
|
||
Fix components in markdown regressions |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
packages/astro/test/fixtures/astro-markdown/src/components/SlotComponent.astro
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<article> | ||
<section class="fragmentSlot"> | ||
<slot name="fragmentSlot">❌ Missing content for slot "fragmentSlot"</slot> | ||
</section> | ||
|
||
<section class="pSlot"> | ||
<slot name="pSlot">❌ Missing content for slot "pSlot"</slot> | ||
</section> | ||
|
||
<section class="defaultSlot"> | ||
<slot>❌ Missing content for default slot</slot> | ||
</section> | ||
</article> |
16 changes: 16 additions & 0 deletions
16
packages/astro/test/fixtures/astro-markdown/src/pages/code-in-md.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# Inline code blocks | ||
|
||
`<script>` tags in **Astro** components are now built, | ||
bundled and optimized by default. | ||
|
||
> Markdown formatting still works between tags in inline code blocks. | ||
We can also use closing `</script>` tags without any problems. | ||
|
||
# Fenced code blocks | ||
|
||
```html | ||
<body> | ||
<div>This should also work without any problems.</div> | ||
</body> | ||
``` |
38 changes: 38 additions & 0 deletions
38
packages/astro/test/fixtures/astro-markdown/src/pages/slots.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
--- | ||
layout: ../layouts/content.astro | ||
setup: import SlotComponent from '../components/SlotComponent.astro'; | ||
--- | ||
|
||
# Component with slot contents in children | ||
|
||
<SlotComponent> | ||
<div>4: Div in default slot</div> | ||
<Fragment slot="fragmentSlot"> | ||
<div>1: Div in fragmentSlot</div> | ||
<p>2: Paragraph in fragmentSlot</p> | ||
</Fragment> | ||
<Fragment><p>5: Paragraph in fragment in default slot</p></Fragment> | ||
6: Regular text in default slot | ||
<p slot="pSlot" title="hello">3: p with title as pSlot</p> | ||
</SlotComponent> | ||
|
||
# Component with nested component in children | ||
|
||
<SlotComponent> | ||
<p slot="pSlot">2: pSlot</p> | ||
<SlotComponent> | ||
<p slot="pSlot">4: nested pSlot</p> | ||
5: nested text in default slot | ||
<Fragment slot="fragmentSlot">3: nested fragmentSlot</Fragment> | ||
</SlotComponent> | ||
<Fragment slot="fragmentSlot">1: fragmentSlot</Fragment> | ||
</SlotComponent> | ||
|
||
# Missing content due to empty children | ||
|
||
<SlotComponent> | ||
</SlotComponent> | ||
|
||
# Missing content due to self-closing tag | ||
|
||
<SlotComponent/> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,51 +1,49 @@ | ||
import { map } from 'unist-util-map'; | ||
import { visit } from 'unist-util-visit'; | ||
|
||
const MDX_ELEMENTS = new Set(['mdxJsxFlowElement', 'mdxJsxTextElement']); | ||
const MDX_ELEMENTS = ['mdxJsxFlowElement', 'mdxJsxTextElement']; | ||
export default function rehypeJsx(): any { | ||
return function (node: any): any { | ||
return map(node, (child: any) => { | ||
if (child.type === 'element') { | ||
return { ...child, tagName: `${child.tagName}` }; | ||
} | ||
if (MDX_ELEMENTS.has(child.type)) { | ||
const attrs = child.attributes.reduce((acc: any[], entry: any) => { | ||
let attr = entry.value; | ||
if (attr && typeof attr === 'object') { | ||
attr = `{${attr.value}}`; | ||
} else if (attr && entry.type === 'mdxJsxExpressionAttribute') { | ||
attr = `{${attr}}`; | ||
} else if (attr === null) { | ||
attr = ''; | ||
} else if (typeof attr === 'string') { | ||
attr = `"${attr}"`; | ||
} | ||
if (!entry.name) { | ||
return acc + ` ${attr}`; | ||
} | ||
return acc + ` ${entry.name}${attr ? '=' : ''}${attr}`; | ||
}, ''); | ||
|
||
if (child.children.length === 0) { | ||
return { | ||
type: 'raw', | ||
value: `<${child.name}${attrs} />`, | ||
}; | ||
visit(node, 'element', (child: any) => { | ||
child.tagName = `${child.tagName}`; | ||
}); | ||
visit(node, MDX_ELEMENTS, (child: any, index: number | null, parent: any) => { | ||
if (index === null || !Boolean(parent)) | ||
return; | ||
|
||
const attrs = child.attributes.reduce((acc: any[], entry: any) => { | ||
let attr = entry.value; | ||
if (attr && typeof attr === 'object') { | ||
attr = `{${attr.value}}`; | ||
} else if (attr && entry.type === 'mdxJsxExpressionAttribute') { | ||
attr = `{${attr}}`; | ||
} else if (attr === null) { | ||
attr = ''; | ||
} else if (typeof attr === 'string') { | ||
attr = `"${attr}"`; | ||
} | ||
child.children.splice(0, 0, { | ||
type: 'raw', | ||
value: `\n<${child.name}${attrs}>`, | ||
}); | ||
child.children.push({ | ||
type: 'raw', | ||
value: `</${child.name}>\n`, | ||
}); | ||
return { | ||
...child, | ||
type: 'element', | ||
tagName: `Fragment`, | ||
}; | ||
if (!entry.name) { | ||
return acc + ` ${attr}`; | ||
} | ||
return acc + ` ${entry.name}${attr ? '=' : ''}${attr}`; | ||
}, ''); | ||
|
||
if (child.children.length === 0) { | ||
child.type = 'raw'; | ||
child.value = `<${child.name}${attrs} />`; | ||
return; | ||
} | ||
return child; | ||
|
||
// Replace the current child node with its children | ||
// wrapped by raw opening and closing tags | ||
const openingTag = { | ||
type: 'raw', | ||
value: `\n<${child.name}${attrs}>`, | ||
}; | ||
const closingTag = { | ||
type: 'raw', | ||
value: `</${child.name}>\n`, | ||
}; | ||
parent.children.splice(index, 1, openingTag, ...child.children, closingTag); | ||
}); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters