Skip to content
39 changes: 33 additions & 6 deletions __tests__/lib/mdxish/mdxish.test.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,43 @@
import type { Root } from 'hast';

import { mdxish } from '../../../lib/mdxish';

describe('mdxish', () => {
describe('mdxish should render', () => {
describe('invalid mdx syntax', () => {
it('should render unclosed tags', () => {
const md = '<br>';
expect(() => mdxish(md)).not.toThrow();
});
});

it('should render content in new lines', () => {
const md = `<div>hello
</div>`;
expect(() => mdxish(md)).not.toThrow();
describe('relaxed md syntax, such as', () => {
it('wrong bold syntax', () => {
const md = `**Bold**

Normal

Hello** Wrong Bold**`;
const tree = mdxish(md);

const getStrongTexts = (node: Root | Root['children'][number]): string[] => {
const texts: string[] = [];
if ('type' in node && node.type === 'element' && node.tagName === 'strong') {
const textNodes =
'children' in node && Array.isArray(node.children)
? node.children.filter(c => 'type' in c && c.type === 'text')
: [];
texts.push(textNodes.map(t => ('value' in t ? t.value : '')).join(''));
}
if ('children' in node && Array.isArray(node.children)) {
node.children.forEach(child => {
texts.push(...getStrongTexts(child));
});
}
return texts;
};

const strongTexts = getStrongTexts(tree);
expect(strongTexts.length).toBeGreaterThanOrEqual(2);
});
});
});
});
Loading