import { renderMarkdown } from '../dist/index.js';
import chai from 'chai';
describe('expressions', () => {
const renderAstroMd = (text, opts) => renderMarkdown(text, { isAstroFlavoredMd: true, ...opts });
it('should be able to serialize bare expression', async () => {
const { code } = await renderAstroMd(`{a}`, {});
chai.expect(code).to.equal(`{a}`);
});
it('should be able to serialize expression inside component', async () => {
const { code } = await renderAstroMd(`{a}`, {});
chai.expect(code).to.equal(`{a}`);
});
it('should be able to serialize expression inside markdown', async () => {
const { code } = await renderAstroMd(`# {frontmatter.title}`, {});
chai
.expect(code)
.to.equal(`
{frontmatter.title}
`);
});
it('should be able to serialize complex expression inside markdown', async () => {
const { code } = await renderAstroMd(`# Hello {frontmatter.name}`, {});
chai
.expect(code)
.to.equal(`Hello {frontmatter.name}
`);
});
it('should be able to serialize complex expression with markup inside markdown', async () => {
const { code } = await renderAstroMd(`# Hello {frontmatter.name}`, {});
chai
.expect(code)
.to.equal(
`Hello {frontmatter.name}
`
);
});
it('should be able to avoid evaluating JSX-like expressions in an inline code & generate a slug for id', async () => {
const { code } = await renderAstroMd(`# \`{frontmatter.title}\``, {});
chai
.expect(code)
.to.equal('{frontmatter.title}
');
});
it('should be able to avoid evaluating JSX-like expressions in inline codes', async () => {
const { code } = await renderAstroMd(`# \`{ foo }\` is a shorthand for \`{ foo: foo }\``, {});
chai
.expect(code)
.to.equal(
'{ foo }
is a shorthand for { foo: foo }
'
);
});
it('should be able to avoid evaluating JSX-like expressions & escape HTML tag characters in inline codes', async () => {
const { code } = await renderAstroMd(
`###### \`{}\` is equivalent to \`Record\` (at TypeScript v{frontmatter.version})`,
{}
);
chai
.expect(code)
.to.equal(
`{}
is equivalent to Record<never, never>
(at TypeScript v{frontmatter.version})
`
);
});
it('should be able to encode ampersand characters in code blocks', async () => {
const { code } = await renderAstroMd(
'The ampersand in ` ` must be encoded in code blocks.',
{}
);
chai
.expect(code)
.to.equal(
'The ampersand in
must be encoded in code blocks.
'
);
});
it('should be able to encode ampersand characters in fenced code blocks', async () => {
const { code } = await renderAstroMd(`
\`\`\`md
The ampersand in \` \` must be encoded in code blocks.
\`\`\`
`);
chai.expect(code).to.match(/^.*The ampersand in ` `/);
});
it('should be able to serialize function expression', async () => {
const { code } = await renderAstroMd(
`{frontmatter.list.map(item => {item}
)}`,
{}
);
chai.expect(code).to.equal(`{frontmatter.list.map(item => {item}
)}`);
});
it('should unwrap HTML comments in inline code blocks', async () => {
const { code } = await renderAstroMd(`\`{/**/}\``);
chai.expect(code).to.equal('<!-- HTML comment -->
');
});
it('should unwrap HTML comments in code fences', async () => {
const { code } = await renderAstroMd(
`
\`\`\`
\`\`\`
`
);
chai.expect(code).to.match(/(?