import { marked } from '../../src/marked.js'; import { timeout } from './utils.js'; describe('Hooks', () => { it('should preprocess markdown', () => { marked.use({ hooks: { preprocess(markdown) { return `# preprocess\n\n${markdown}`; } } }); const html = marked('*text*'); expect(html.trim()).toBe('

preprocess

\n

text

'); }); it('should preprocess async', async() => { marked.use({ async: true, hooks: { async preprocess(markdown) { await timeout(); return `# preprocess async\n\n${markdown}`; } } }); const promise = marked('*text*'); expect(promise).toBeInstanceOf(Promise); const html = await promise; expect(html.trim()).toBe('

preprocess async

\n

text

'); }); it('should preprocess options', () => { marked.use({ hooks: { preprocess(markdown) { this.options.headerIds = false; return markdown; } } }); const html = marked('# test'); expect(html.trim()).toBe('

test

'); }); it('should preprocess options async', async() => { marked.use({ async: true, hooks: { async preprocess(markdown) { await timeout(); this.options.headerIds = false; return markdown; } } }); const html = await marked('# test'); expect(html.trim()).toBe('

test

'); }); it('should postprocess html', () => { marked.use({ hooks: { postprocess(html) { return html + '

postprocess

'; } } }); const html = marked('*text*'); expect(html.trim()).toBe('

text

\n

postprocess

'); }); it('should postprocess async', async() => { marked.use({ async: true, hooks: { async postprocess(html) { await timeout(); return html + '

postprocess async

\n'; } } }); const promise = marked('*text*'); expect(promise).toBeInstanceOf(Promise); const html = await promise; expect(html.trim()).toBe('

text

\n

postprocess async

'); }); it('should process all hooks in reverse', async() => { marked.use({ hooks: { preprocess(markdown) { return `# preprocess1\n\n${markdown}`; }, postprocess(html) { return html + '

postprocess1

\n'; } } }); marked.use({ async: true, hooks: { preprocess(markdown) { return `# preprocess2\n\n${markdown}`; }, async postprocess(html) { await timeout(); return html + '

postprocess2 async

\n'; } } }); const promise = marked('*text*'); expect(promise).toBeInstanceOf(Promise); const html = await promise; expect(html.trim()).toBe('

preprocess1

\n

preprocess2

\n

text

\n

postprocess2 async

\n

postprocess1

'); }); });