Remove extra newlines around Markdown components (#3620)

This commit is contained in:
hippotastic 2022-06-17 18:52:37 +02:00 committed by GitHub
parent 1a2f321e34
commit 05aa72442c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 23 additions and 13 deletions

View file

@ -0,0 +1,6 @@
---
'astro': patch
'@astrojs/markdown-remark': patch
---
Remove extra newlines around Markdown components

View file

@ -291,12 +291,16 @@ describe('Astro Markdown', () => {
expect(slots.find('> .fragmentSlot > div').text()).to.contain('1:');
expect(slots.find('> .fragmentSlot > div + p').text()).to.contain('2:');
expect(slots.find('> .pSlot > p[title="hello"]').text()).to.contain('3:');
expect(slots.find('> .defaultSlot').text().replace(/\s+/g, ' ')).to.equal(
`
4: Div in default slot
5: Paragraph in fragment in default slot
6: Regular text in default slot
`.replace(/\s+/g, ' ')
expect(slots.find('> .defaultSlot').html()).to.match(
new RegExp(
`<div>4: Div in default slot</div>` +
// Optional extra paragraph due to the line breaks between components
`(<p></p>)?` +
`<p>5: Paragraph in fragment in default slot</p>` +
// Optional whitespace due to the line breaks between components
`[\s\n]*` +
`6: Regular text in default slot`
)
);
const nestedSlots = $('article').eq(1);

View file

@ -25,7 +25,7 @@ export default function createCollectHeaders() {
return;
}
if (child.type === 'raw') {
if (child.value.startsWith('\n<') || child.value.endsWith('>\n')) {
if (child.value.match(/^\n?<.*>\n?$/)) {
return;
}
}

View file

@ -53,11 +53,11 @@ export default function rehypeJsx(): ReturnType<RehypePlugin> {
// wrapped by raw opening and closing tags
const openingTag = {
type: 'raw',
value: `\n<${node.name}${attrs}>`,
value: `<${node.name}${attrs}>`,
};
const closingTag = {
type: 'raw',
value: `</${node.name}>\n`,
value: `</${node.name}>`,
};
parent.children.splice(index, 1, openingTag, ...node.children, closingTag);
});

View file

@ -49,7 +49,7 @@ describe('components', () => {
it('should normalize children', async () => {
const { code } = await renderMarkdown(`<Component bool={true}>Hello world!</Component>`, {});
chai.expect(code).to.equal(`\n<Component bool={true}>Hello world!</Component>\n`);
chai.expect(code).to.equal(`<Component bool={true}>Hello world!</Component>`);
});
it('should be able to nest components', async () => {
@ -60,7 +60,7 @@ describe('components', () => {
chai
.expect(code)
.to.equal(`\n<Component bool={true}>\n<Component>Hello world!</Component>\n</Component>\n`);
.to.equal(`<Component bool={true}><Component>Hello world!</Component></Component>`);
});
it('should allow markdown without many spaces', async () => {
@ -71,6 +71,6 @@ describe('components', () => {
{}
);
chai.expect(code).to.equal(`\n<Component><h1 id="hello-world">Hello world!</h1></Component>\n`);
chai.expect(code).to.equal(`<Component><h1 id="hello-world">Hello world!</h1></Component>`);
});
});

View file

@ -11,7 +11,7 @@ describe('expressions', () => {
it('should be able to serialize expression inside component', async () => {
const { code } = await renderMarkdown(`<Component>{a}</Component>`, {});
chai.expect(code).to.equal(`\n<Component>{a}</Component>\n`);
chai.expect(code).to.equal(`<Component>{a}</Component>`);
});
it('should be able to serialize expression inside markdown', async () => {