#!/bin/sh
# marked is ESM-only since v5; exercise the parser and the bundled extensions
# through a small ESM smoke test (works both at build time and in autopkgtest).
# The @markedjs/html-differ helper (debian/tests/test_modules) additionally
# exercises node-diff and node-parse5-sax-parser.
set -e
node --input-type=module - <<'EOF'
import assert from 'node:assert/strict';
import { marked, Marked, Lexer, Parser } from 'marked';
import { gfmHeadingId } from 'marked-gfm-heading-id';
import { mangle } from 'marked-mangle';
import GithubSlugger from 'github-slugger';
import { HtmlDiffer } from '@markedjs/html-differ';

assert.equal(marked.parse('# Hello\n\ntext **b**').trim(),
  '<h1>Hello</h1>\n<p>text <strong>b</strong></p>');
assert.ok(new Marked() instanceof Marked);
assert.equal(typeof Lexer, 'function');
assert.equal(typeof Parser, 'function');

const mg = new Marked();
mg.use(gfmHeadingId());
assert.match(mg.parse('# Title'), /id="title"/);

const mm = new Marked();
mm.use(mangle());
assert.equal(typeof mangle, 'function');

assert.equal(new GithubSlugger().slug('Hello World'), 'hello-world');

// HTML comparison (uses node-diff + node-parse5-sax-parser)
const differ = new HtmlDiffer();
assert.ok(await differ.isEqual('<h1>Hello</h1>', marked.parse('# Hello')));
assert.ok(!(await differ.isEqual('<h1>Hello</h1>', marked.parse('# World'))));

console.log('marked autopkgtest OK');
EOF
