2023-02-09 18:25:18 +00:00
import { parseHTML } from 'linkedom' ;
import { parse as parseDevalue } from 'devalue' ;
import { expect } from 'chai' ;
2023-02-13 23:18:54 +00:00
import { loadFixture , fixLineEndings } from '../../../astro/test/test-utils.js' ;
2023-03-02 18:53:55 +00:00
import markdoc from '../dist/index.js' ;
2023-02-13 23:18:54 +00:00
function formatPost ( post ) {
return {
... post ,
body : fixLineEndings ( post . body ) ,
} ;
}
2023-02-09 18:25:18 +00:00
2023-03-02 18:53:55 +00:00
const root = new URL ( './fixtures/content-collections/' , import . meta . url ) ;
2023-02-09 18:25:18 +00:00
describe ( 'Markdoc - Content Collections' , ( ) => {
2023-03-02 18:53:55 +00:00
let baseFixture ;
2023-02-09 18:25:18 +00:00
before ( async ( ) => {
2023-03-02 18:53:55 +00:00
baseFixture = await loadFixture ( {
root ,
integrations : [ markdoc ( ) ] ,
2023-02-09 18:25:18 +00:00
} ) ;
} ) ;
describe ( 'dev' , ( ) => {
let devServer ;
before ( async ( ) => {
2023-03-02 18:53:55 +00:00
devServer = await baseFixture . startDevServer ( ) ;
2023-02-09 18:25:18 +00:00
} ) ;
after ( async ( ) => {
await devServer . stop ( ) ;
} ) ;
it ( 'loads entry' , async ( ) => {
2023-03-02 18:53:55 +00:00
const res = await baseFixture . fetch ( '/entry.json' ) ;
2023-02-09 18:25:18 +00:00
const post = parseDevalue ( await res . text ( ) ) ;
2023-02-13 23:18:54 +00:00
expect ( formatPost ( post ) ) . to . deep . equal ( simplePostEntry ) ;
2023-02-09 18:25:18 +00:00
} ) ;
it ( 'loads collection' , async ( ) => {
2023-03-02 18:53:55 +00:00
const res = await baseFixture . fetch ( '/collection.json' ) ;
2023-02-09 18:25:18 +00:00
const posts = parseDevalue ( await res . text ( ) ) ;
expect ( posts ) . to . not . be . null ;
2023-02-13 23:18:54 +00:00
expect ( posts . sort ( ) . map ( ( post ) => formatPost ( post ) ) ) . to . deep . equal ( [
simplePostEntry ,
withComponentsEntry ,
withConfigEntry ,
] ) ;
2023-02-10 14:01:10 +00:00
} ) ;
2023-02-10 14:09:58 +00:00
it ( 'renders content - simple' , async ( ) => {
2023-03-02 18:53:55 +00:00
const res = await baseFixture . fetch ( '/content-simple' ) ;
2023-02-10 14:09:58 +00:00
const html = await res . text ( ) ;
const { document } = parseHTML ( html ) ;
const h2 = document . querySelector ( 'h2' ) ;
expect ( h2 . textContent ) . to . equal ( 'Simple post' ) ;
const p = document . querySelector ( 'p' ) ;
expect ( p . textContent ) . to . equal ( 'This is a simple Markdoc post.' ) ;
} ) ;
it ( 'renders content - with config' , async ( ) => {
2023-03-02 18:53:55 +00:00
const fixture = await getFixtureWithConfig ( ) ;
const server = await fixture . startDevServer ( ) ;
2023-02-10 14:09:58 +00:00
const res = await fixture . fetch ( '/content-with-config' ) ;
const html = await res . text ( ) ;
const { document } = parseHTML ( html ) ;
const h2 = document . querySelector ( 'h2' ) ;
expect ( h2 . textContent ) . to . equal ( 'Post with config' ) ;
2023-03-02 18:53:55 +00:00
const textContent = html ;
expect ( textContent ) . to . not . include ( 'Hello' ) ;
expect ( textContent ) . to . include ( 'Hola' ) ;
expect ( textContent ) . to . include ( ` Konnichiwa ` ) ;
await server . stop ( ) ;
2023-02-10 14:09:58 +00:00
} ) ;
it ( 'renders content - with components' , async ( ) => {
2023-03-02 18:53:55 +00:00
const fixture = await getFixtureWithComponents ( ) ;
const server = await fixture . startDevServer ( ) ;
2023-02-10 14:09:58 +00:00
const res = await fixture . fetch ( '/content-with-components' ) ;
const html = await res . text ( ) ;
const { document } = parseHTML ( html ) ;
const h2 = document . querySelector ( 'h2' ) ;
expect ( h2 . textContent ) . to . equal ( 'Post with components' ) ;
// Renders custom shortcode component
const marquee = document . querySelector ( 'marquee' ) ;
expect ( marquee ) . to . not . be . null ;
expect ( marquee . hasAttribute ( 'data-custom-marquee' ) ) . to . equal ( true ) ;
// Renders Astro Code component
const pre = document . querySelector ( 'pre' ) ;
expect ( pre ) . to . not . be . null ;
expect ( pre . className ) . to . equal ( 'astro-code' ) ;
2023-03-02 18:53:55 +00:00
await server . stop ( ) ;
2023-02-10 14:09:58 +00:00
} ) ;
2023-02-10 14:01:10 +00:00
} ) ;
describe ( 'build' , ( ) => {
before ( async ( ) => {
2023-03-02 18:53:55 +00:00
await baseFixture . build ( ) ;
2023-02-10 14:01:10 +00:00
} ) ;
it ( 'loads entry' , async ( ) => {
2023-03-02 18:53:55 +00:00
const res = await baseFixture . readFile ( '/entry.json' ) ;
2023-02-10 14:01:10 +00:00
const post = parseDevalue ( res ) ;
2023-02-13 23:18:54 +00:00
expect ( formatPost ( post ) ) . to . deep . equal ( simplePostEntry ) ;
2023-02-10 14:01:10 +00:00
} ) ;
it ( 'loads collection' , async ( ) => {
2023-03-02 18:53:55 +00:00
const res = await baseFixture . readFile ( '/collection.json' ) ;
2023-02-10 14:01:10 +00:00
const posts = parseDevalue ( res ) ;
expect ( posts ) . to . not . be . null ;
2023-02-13 23:18:54 +00:00
expect ( posts . sort ( ) . map ( ( post ) => formatPost ( post ) ) ) . to . deep . equal ( [
simplePostEntry ,
withComponentsEntry ,
withConfigEntry ,
] ) ;
2023-02-10 14:09:58 +00:00
} ) ;
it ( 'renders content - simple' , async ( ) => {
2023-03-02 18:53:55 +00:00
const html = await baseFixture . readFile ( '/content-simple/index.html' ) ;
2023-02-10 14:09:58 +00:00
const { document } = parseHTML ( html ) ;
const h2 = document . querySelector ( 'h2' ) ;
expect ( h2 . textContent ) . to . equal ( 'Simple post' ) ;
const p = document . querySelector ( 'p' ) ;
expect ( p . textContent ) . to . equal ( 'This is a simple Markdoc post.' ) ;
} ) ;
it ( 'renders content - with config' , async ( ) => {
2023-03-02 18:53:55 +00:00
const fixture = await getFixtureWithConfig ( ) ;
await fixture . build ( ) ;
2023-02-10 14:09:58 +00:00
const html = await fixture . readFile ( '/content-with-config/index.html' ) ;
const { document } = parseHTML ( html ) ;
const h2 = document . querySelector ( 'h2' ) ;
expect ( h2 . textContent ) . to . equal ( 'Post with config' ) ;
2023-03-02 18:53:55 +00:00
const textContent = html ;
expect ( textContent ) . to . not . include ( 'Hello' ) ;
expect ( textContent ) . to . include ( 'Hola' ) ;
expect ( textContent ) . to . include ( ` Konnichiwa ` ) ;
2023-02-10 14:09:58 +00:00
} ) ;
it ( 'renders content - with components' , async ( ) => {
2023-03-02 18:53:55 +00:00
const fixture = await getFixtureWithComponents ( ) ;
await fixture . build ( ) ;
2023-02-10 14:09:58 +00:00
const html = await fixture . readFile ( '/content-with-components/index.html' ) ;
const { document } = parseHTML ( html ) ;
const h2 = document . querySelector ( 'h2' ) ;
expect ( h2 . textContent ) . to . equal ( 'Post with components' ) ;
// Renders custom shortcode component
const marquee = document . querySelector ( 'marquee' ) ;
expect ( marquee ) . to . not . be . null ;
expect ( marquee . hasAttribute ( 'data-custom-marquee' ) ) . to . equal ( true ) ;
// Renders Astro Code component
const pre = document . querySelector ( 'pre' ) ;
expect ( pre ) . to . not . be . null ;
expect ( pre . className ) . to . equal ( 'astro-code' ) ;
2023-02-09 18:25:18 +00:00
} ) ;
} ) ;
} ) ;
2023-03-02 18:53:55 +00:00
function getFixtureWithConfig ( ) {
return loadFixture ( {
root ,
integrations : [
markdoc ( {
variables : {
countries : [ 'ES' , 'JP' ] ,
} ,
functions : {
includes : {
transform ( parameters ) {
const [ array , value ] = Object . values ( parameters ) ;
return Array . isArray ( array ) ? array . includes ( value ) : false ;
} ,
} ,
} ,
} ) ,
] ,
} ) ;
}
function getFixtureWithComponents ( ) {
return loadFixture ( {
root ,
integrations : [
markdoc ( {
nodes : {
fence : {
render : 'Code' ,
attributes : {
language : { type : String } ,
content : { type : String } ,
} ,
} ,
} ,
tags : {
mq : {
render : 'CustomMarquee' ,
attributes : {
direction : {
type : String ,
default : 'left' ,
matches : [ 'left' , 'right' , 'up' , 'down' ] ,
errorLevel : 'critical' ,
} ,
} ,
} ,
} ,
} ) ,
] ,
} ) ;
}
2023-02-09 18:25:18 +00:00
const simplePostEntry = {
id : 'simple.mdoc' ,
slug : 'simple' ,
collection : 'blog' ,
data : {
schemaWorks : true ,
title : 'Simple post' ,
} ,
body : '\n## Simple post\n\nThis is a simple Markdoc post.\n' ,
} ;
2023-02-10 14:01:10 +00:00
const withComponentsEntry = {
id : 'with-components.mdoc' ,
slug : 'with-components' ,
collection : 'blog' ,
data : {
schemaWorks : true ,
title : 'Post with components' ,
} ,
body : '\n## Post with components\n\nThis uses a custom marquee component with a shortcode:\n\n{% mq direction="right" %}\nI\'m a marquee too!\n{% /mq %}\n\nAnd a code component for code blocks:\n\n```js\nconst isRenderedWithShiki = true;\n```\n' ,
} ;
const withConfigEntry = {
id : 'with-config.mdoc' ,
slug : 'with-config' ,
collection : 'blog' ,
data : {
schemaWorks : true ,
title : 'Post with config' ,
} ,
2023-03-02 18:53:55 +00:00
body : '\n## Post with config\n\n{% if includes($countries, "EN") %} Hello {% /if %}\n{% if includes($countries, "ES") %} Hola {% /if %}\n{% if includes($countries, "JP") %} Konnichiwa {% /if %}\n' ,
2023-02-10 14:01:10 +00:00
} ;