
import type { marked } from 'marked';
import { MarkdownOptions } from './render';

export interface EmbedToken extends marked.Tokens.Generic {
	media_type: string;
	href: string;
}

export function embed_ext(renderer: marked.Renderer, opts: MarkdownOptions) : marked.TokenizerExtension & marked.RendererExtension {
	return {
		name: 'embed',
		level: 'inline',
		start: (src) => src.match(/\{&/)?.index,
		tokenizer(src, tokens) {
			const rule = /^\{&\s+([^\s]+):\s*([^\s]+)\s+&\}/;
			const match = rule.exec(src);

			if (match) {
				return {
					type: 'embed',
					raw: match[0],
					media_type: match[1],
					href: match[2],
					tokens: [ ]
				};
			}
		},
		renderer(token: EmbedToken) {
			return `<embed type="${token.media_type}" src="${token.href}"></embed>`;
		}
	};
}
