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

export interface MarkToken extends marked.Tokens.Generic {
	text: string;
}

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

			if (match) {
				return {
					type: 'mark',
					raw: match[0],
					text: match[1],
					tokens: this.lexer.inlineTokens(match[1], [ ])
				};
			}
		},
		renderer(token: MarkToken) {
			return `<mark>${this.parser.parseInline(token.tokens, renderer)}</mark>`;
		}
	};
}
