import { ribbit } from './setup';
const r = ribbit();
const macros = [
{
name: 'user',
toHTML: () => 'TestUser',
selector: 'A[href="/user"]',
toMarkdown: () => '@user',
},
{
name: 'npc',
toHTML: ({ keywords }: any) => {
const name = keywords.join(' ');
return '' + name + '';
},
selector: 'A[href^="/NPC/"]',
toMarkdown: (el: any) => '@npc(' + el.textContent + ')',
},
{
name: 'style',
toHTML: ({ keywords, content }: any) => '
' + (content || '') + '
',
selector: 'DIV[class]',
toMarkdown: (el: any, convert: any) => '\n\n@style(' + el.className + '\n' + convert.children(el) + '\n)\n\n',
},
{
name: 'toc',
toHTML: ({ params }: any) => '',
},
];
const h = new r.HopDown({ macros });
const H = (md: string) => h.toHTML(md);
const M = (html: string) => h.toMarkdown(html);
describe('Macros', () => {
describe('self-closing', () => {
it('bare name', () => expect(H('hello @user world')).toBe('hello TestUser world
'));
it('empty parens', () => expect(H('hello @user() world')).toBe('hello TestUser world
'));
it('keywords', () => expect(H('@npc(Goblin King)')).toBe('Goblin King
'));
it('params', () => expect(H('@toc(depth="2")')).toContain('data-depth="2"'));
});
describe('unknown macros', () => {
it('renders error', () => expect(H('@bogus')).toContain('ribbit-error'));
it('shows name', () => expect(H('@bogus')).toContain('@bogus'));
it('block error', () => expect(H('@bogus(args\ncontent\n)')).toContain('ribbit-error'));
});
it('email not matched', () => expect(H('user@example.com')).toBe('user@example.com
'));
describe('block macros', () => {
it('content processed', () => expect(H('@style(box\n**bold**\n)')).toContain('bold'));
it('wraps in div', () => expect(H('@style(box\ncontent\n)')).toContain(''));
it('multiple keywords', () => expect(H('@style(box center\ncontent\n)')).toContain('class="box center"'));
});
describe('verbatim', () => {
it('skips markdown', () => expect(H('@style(box verbatim\n**bold**\n)')).toContain('**bold**'));
it('no strong tag', () => expect(H('@style(box verbatim\n**bold**\n)')).not.toContain('
'));
it('escapes html', () => expect(H('@style(box verbatim\ntag\n)')).toContain('<b>'));
it('preserves newlines', () => expect(H('@style(box verbatim\nline1\nline2\n)')).toContain('line1
'));
it('strips keyword', () => expect(H('@style(box verbatim\ncontent\n)')).not.toContain('verbatim'));
});
describe('nesting', () => {
it('inline inside bold', () => expect(H('**@npc(Goblin King)**')).toContain(''));
it('block contains list', () => expect(H('@style(box\n- item 1\n- item 2\n)')).toContain('