Fix left curly bracket formatting (#1094)

* Fix curly braces

* Add tests

* chore: formatting

* sstyle: update fix to be more explicit and ireduce chance of false positive

* style: use suggestions

Co-authored-by: mmarkelov <maks-markel@mail.ru>
This commit is contained in:
Jonathan Neal 2021-08-12 12:51:37 -04:00 committed by GitHub
parent 2c0d0a7aa2
commit 0a04c69dbb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 29 additions and 4 deletions

View file

@ -768,7 +768,7 @@ async function compileHtml(enterNode: TemplateNode, state: CodegenState, compile
}
if (parent.name === 'code') {
// Special case, escaped { characters from markdown content
text = node.raw.replace(/&#x26;#123;/g, '{');
text = node.raw.replace(/ASTRO_ESCAPED_LEFT_CURLY_BRACKET\0/g, '{');
}
buffers[curr] += ',' + JSON.stringify(text);
return;

View file

@ -11,14 +11,14 @@ function escape(code: string) {
.replace(/[`$]/g, (match) => {
return '\\' + match;
})
.replace(/&#123;/g, '{');
.replace(/ASTRO_ESCAPED_LEFT_CURLY_BRACKET\0/g, '{');
}
/** Unescape { characters transformed by Markdown generation */
function unescapeCode(code: TemplateNode) {
code.children = code.children?.map((child) => {
if (child.type === 'Text') {
return { ...child, raw: child.raw.replace(/&#x26;#123;/g, '{') };
return { ...child, raw: child.raw.replace(/ASTRO_ESCAPED_LEFT_CURLY_BRACKET\0/g, '{') };
}
return child;
});

View file

@ -90,6 +90,17 @@ Markdown('Renders dynamic content though the content attribute', async ({ runtim
assert.ok($('#inner').is('[class]'), 'Scoped class passed down');
});
Markdown('Renders curly braces correctly', async ({ runtime }) => {
const result = await runtime.load('/braces');
assert.ok(!result.error, `build error: ${result.error}`);
const $ = doc(result.contents);
assert.equal($('code').length, 3, 'Rendered curly braces markdown content');
assert.equal($('code:first-child').text(), '({})', 'Rendered curly braces markdown content');
assert.equal($('code:nth-child(2)').text(), '{...props}', 'Rendered curly braces markdown content');
assert.equal($('code:last-child').text(), '{/* JavaScript */}', 'Rendered curly braces markdown content');
});
Markdown('Does not close parent early when using content attribute (#494)', async ({ runtime }) => {
const result = await runtime.load('/close');
assert.ok(!result.error, `build error: ${result.error}`);

View file

@ -0,0 +1,14 @@
---
import { Markdown } from 'astro/components';
const title = 'My Blog Post';
const description = 'This is a post about some stuff.';
---
<Markdown>
## Interesting Topic
`({})`
`{...props}`
`{/* JavaScript */}`
</Markdown>

View file

@ -21,7 +21,7 @@ export function rehypeCodeBlock() {
const escapeCode = (code: any) => {
code.children = code.children.map((child: any) => {
if (child.type === 'text') {
return { ...child, value: child.value.replace(/\{/g, '&#123;') };
return { ...child, value: child.value.replace(/\{/g, 'ASTRO_ESCAPED_LEFT_CURLY_BRACKET\0') };
}
return child;
});