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:
parent
2c0d0a7aa2
commit
0a04c69dbb
5 changed files with 29 additions and 4 deletions
|
@ -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(/&#123;/g, '{');
|
||||
text = node.raw.replace(/ASTRO_ESCAPED_LEFT_CURLY_BRACKET\0/g, '{');
|
||||
}
|
||||
buffers[curr] += ',' + JSON.stringify(text);
|
||||
return;
|
||||
|
|
|
@ -11,14 +11,14 @@ function escape(code: string) {
|
|||
.replace(/[`$]/g, (match) => {
|
||||
return '\\' + match;
|
||||
})
|
||||
.replace(/{/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(/&#123;/g, '{') };
|
||||
return { ...child, raw: child.raw.replace(/ASTRO_ESCAPED_LEFT_CURLY_BRACKET\0/g, '{') };
|
||||
}
|
||||
return child;
|
||||
});
|
||||
|
|
|
@ -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}`);
|
||||
|
|
14
packages/astro/test/fixtures/astro-markdown/src/pages/braces.astro
vendored
Normal file
14
packages/astro/test/fixtures/astro-markdown/src/pages/braces.astro
vendored
Normal 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>
|
|
@ -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, '{') };
|
||||
return { ...child, value: child.value.replace(/\{/g, 'ASTRO_ESCAPED_LEFT_CURLY_BRACKET\0') };
|
||||
}
|
||||
return child;
|
||||
});
|
||||
|
|
Loading…
Reference in a new issue