2022-05-24 22:02:11 +00:00
import { renderMarkdown } from '../dist/index.js' ;
2022-07-22 22:45:16 +00:00
import chai from 'chai' ;
2022-05-24 22:02:11 +00:00
describe ( 'expressions' , ( ) => {
2022-07-22 22:45:16 +00:00
const renderAstroMd = ( text , opts ) => renderMarkdown ( text , { isAstroFlavoredMd : true , ... opts } ) ;
2022-06-03 12:26:39 +00:00
it ( 'should be able to serialize bare expression' , async ( ) => {
2022-07-22 22:45:16 +00:00
const { code } = await renderAstroMd ( ` {a} ` , { } ) ;
2022-05-24 22:02:11 +00:00
chai . expect ( code ) . to . equal ( ` {a} ` ) ;
} ) ;
it ( 'should be able to serialize expression inside component' , async ( ) => {
2022-07-22 22:45:16 +00:00
const { code } = await renderAstroMd ( ` <Component>{a}</Component> ` , { } ) ;
2022-05-24 22:02:11 +00:00
2022-06-17 16:52:37 +00:00
chai . expect ( code ) . to . equal ( ` <Component>{a}</Component> ` ) ;
2022-05-24 22:02:11 +00:00
} ) ;
it ( 'should be able to serialize expression inside markdown' , async ( ) => {
2022-07-22 22:45:16 +00:00
const { code } = await renderAstroMd ( ` # {frontmatter.title} ` , { } ) ;
2022-05-24 22:02:11 +00:00
2022-05-24 22:03:29 +00:00
chai
. expect ( code )
. to . equal ( ` <h1 id={ $ $ slug( \` \$ {frontmatter.title} \` )}>{frontmatter.title}</h1> ` ) ;
2022-05-24 22:02:11 +00:00
} ) ;
it ( 'should be able to serialize complex expression inside markdown' , async ( ) => {
2022-07-22 22:45:16 +00:00
const { code } = await renderAstroMd ( ` # Hello {frontmatter.name} ` , { } ) ;
2022-05-24 22:02:11 +00:00
2022-05-24 22:03:29 +00:00
chai
. expect ( code )
. to . equal ( ` <h1 id={ $ $ slug( \` Hello \$ {frontmatter.name} \` )}>Hello {frontmatter.name}</h1> ` ) ;
2022-05-24 22:02:11 +00:00
} ) ;
it ( 'should be able to serialize complex expression with markup inside markdown' , async ( ) => {
2022-07-22 22:45:16 +00:00
const { code } = await renderAstroMd ( ` # Hello <span>{frontmatter.name}</span> ` , { } ) ;
2022-05-24 22:02:11 +00:00
2022-05-24 22:03:29 +00:00
chai
. expect ( code )
. to . equal (
` <h1 id={ $ $ slug( \` Hello \$ {frontmatter.name} \` )}>Hello <span>{frontmatter.name}</span></h1> `
) ;
2022-05-24 22:02:11 +00:00
} ) ;
2022-06-03 12:26:39 +00:00
it ( 'should be able to avoid evaluating JSX-like expressions in an inline code & generate a slug for id' , async ( ) => {
2022-07-22 22:45:16 +00:00
const { code } = await renderAstroMd ( ` # \` {frontmatter.title} \` ` , { } ) ;
2022-06-03 12:26:39 +00:00
chai
. expect ( code )
. to . equal ( '<h1 id="frontmattertitle"><code is:raw>{frontmatter.title}</code></h1>' ) ;
} ) ;
it ( 'should be able to avoid evaluating JSX-like expressions in inline codes' , async ( ) => {
2022-07-22 22:45:16 +00:00
const { code } = await renderAstroMd ( ` # \` { foo } \` is a shorthand for \` { foo: foo } \` ` , { } ) ;
2022-06-03 12:26:39 +00:00
chai
. expect ( code )
. to . equal (
2022-07-16 09:45:41 +00:00
'<h1 id="-foo--is-a-shorthand-for--foo-foo"><code is:raw>{ foo }</code> is a shorthand for <code is:raw>{ foo: foo }</code></h1>'
2022-06-03 12:26:39 +00:00
) ;
} ) ;
it ( 'should be able to avoid evaluating JSX-like expressions & escape HTML tag characters in inline codes' , async ( ) => {
2022-07-22 22:45:16 +00:00
const { code } = await renderAstroMd (
2022-06-03 12:26:39 +00:00
` ###### \` {} \` is equivalent to \` Record<never, never> \` <small>(at TypeScript v{frontmatter.version})</small> ` ,
{ }
) ;
chai
. expect ( code )
. to . equal (
` <h6 id={ $ $ slug( \` {} is equivalent to Record<never, never> (at TypeScript v \$ {frontmatter.version}) \` )}><code is:raw>{}</code> is equivalent to <code is:raw>Record<never, never></code> <small>(at TypeScript v{frontmatter.version})</small></h6> `
) ;
} ) ;
2022-06-20 19:09:35 +00:00
it ( 'should be able to encode ampersand characters in code blocks' , async ( ) => {
2022-07-22 22:45:16 +00:00
const { code } = await renderAstroMd (
2022-06-20 19:09:35 +00:00
'The ampersand in ` ` must be encoded in code blocks.' ,
{ }
) ;
chai
. expect ( code )
. to . equal (
'<p>The ampersand in <code is:raw>&nbsp;</code> must be encoded in code blocks.</p>'
2022-06-20 19:10:59 +00:00
) ;
2022-06-20 19:09:35 +00:00
} ) ;
it ( 'should be able to encode ampersand characters in fenced code blocks' , async ( ) => {
2022-07-22 22:45:16 +00:00
const { code } = await renderAstroMd ( `
2022-06-20 19:09:35 +00:00
\ ` \` \` md
The ampersand in \ ` \` must be encoded in code blocks.
\ ` \` \`
` );
2022-06-20 19:10:59 +00:00
chai . expect ( code ) . to . match ( /^<pre is:raw.*<code>.*The ampersand in `&nbsp;`/ ) ;
} ) ;
2022-06-20 19:09:35 +00:00
2022-05-24 22:02:11 +00:00
it ( 'should be able to serialize function expression' , async ( ) => {
2022-07-22 22:45:16 +00:00
const { code } = await renderAstroMd (
2022-05-24 22:03:29 +00:00
` {frontmatter.list.map(item => <p id={item}>{item}</p>)} ` ,
{ }
) ;
2022-05-24 22:02:11 +00:00
chai . expect ( code ) . to . equal ( ` {frontmatter.list.map(item => <p id={item}>{item}</p>)} ` ) ;
2022-05-24 22:03:29 +00:00
} ) ;
2022-06-20 17:12:42 +00:00
it ( 'should unwrap HTML comments in inline code blocks' , async ( ) => {
2022-07-22 22:45:16 +00:00
const { code } = await renderAstroMd ( ` \` {/*<!-- HTML comment -->*/} \` ` ) ;
2022-06-20 17:12:42 +00:00
chai . expect ( code ) . to . equal ( '<p><code is:raw><!-- HTML comment --></code></p>' ) ;
} ) ;
it ( 'should unwrap HTML comments in code fences' , async ( ) => {
2022-07-22 22:45:16 +00:00
const { code } = await renderAstroMd (
2022-06-20 17:12:42 +00:00
`
\ ` \` \`
<!-- HTML comment -- >
\ ` \` \`
`
) ;
2022-06-20 17:14:08 +00:00
chai . expect ( code ) . to . match ( /(?<!{\/\*)<!-- HTML comment -->(?!\*\/})/ ) ;
2022-06-20 17:12:42 +00:00
} ) ;
2022-05-24 22:02:11 +00:00
} ) ;