import markdownit from 'markdown-it'
import markdownItMark from 'markdown-it-mark'
const md = markdownit().use(markdownItMark);

export function renderInline(rawText: string) {
    // Swap double-dash for em-dash
    const text = rawText.replace(/---/g, '&mdash;').replace(/--/g, '&ndash;');
    return md.renderInline(text);
}
export function render(rawText: string) {
    // Swap double-dash for em-dash
    const text = rawText.replace(/---/g, '&mdash;').replace(/--/g, '&ndash;');
    return md.render(text);
}

export function highlight(text: string, term: string) {
    const terms = term.split(',').map(t => t.trim()).filter(Boolean);
    const regex = new RegExp(`(${terms.map(t => t.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')).join('|')})`, 'gi');
    const parts = text.split(regex);
    return renderInline(parts.map((part) => regex.test(part) ? `**${part}**` : part).join(''));
}
