"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.mdToOneLinePlainText = exports.toSafeHtml = void 0;
const core_utils_1 = require("../../shared/core-utils");
const defaultSanitizeOptions = (0, core_utils_1.getDefaultSanitizeOptions)();
const textOnlySanitizeOptions = (0, core_utils_1.getTextOnlySanitizeOptions)();
const sanitizeHtml = require('sanitize-html');
const markdownItEmoji = require('markdown-it-emoji/light');
const MarkdownItClass = require('markdown-it');
const markdownItWithHTML = new MarkdownItClass('default', { linkify: true, breaks: true, html: true });
const markdownItWithoutHTML = new MarkdownItClass('default', { linkify: true, breaks: true, html: false });
const toSafeHtml = (text) => {
    if (!text)
        return '';
    const textWithLineFeed = text.replace(/<br.?\/?>/g, '\r\n');
    const html = markdownItWithHTML.enable(core_utils_1.TEXT_WITH_HTML_RULES)
        .use(markdownItEmoji)
        .render(textWithLineFeed);
    return sanitizeHtml(html, defaultSanitizeOptions);
};
exports.toSafeHtml = toSafeHtml;
const mdToOneLinePlainText = (text) => {
    if (!text)
        return '';
    markdownItWithoutHTML.use(markdownItEmoji)
        .use(plainTextPlugin)
        .render(text);
    return sanitizeHtml(markdownItWithoutHTML.plainText, textOnlySanitizeOptions);
};
exports.mdToOneLinePlainText = mdToOneLinePlainText;
function plainTextPlugin(markdownIt) {
    let lastSeparator = '';
    function plainTextRule(state) {
        const text = scan(state.tokens);
        markdownIt.plainText = text.replace(/\s+/g, ' ');
    }
    function scan(tokens) {
        let text = '';
        for (const token of tokens) {
            if (token.children !== null) {
                text += scan(token.children);
                continue;
            }
            if (token.type === 'list_item_close') {
                lastSeparator = ', ';
            }
            else if (/[a-zA-Z]+_close/.test(token.type)) {
                lastSeparator = ' ';
            }
            else if (token.content) {
                text += lastSeparator;
                text += token.content;
            }
        }
        return text;
    }
    markdownIt.core.ruler.push('plainText', plainTextRule);
}
//# sourceMappingURL=markdown.js.map