"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 markdownItForSafeHtml = new MarkdownItClass('default', { linkify: true, breaks: true, html: true })
    .enable(core_utils_1.TEXT_WITH_HTML_RULES)
    .use(markdownItEmoji);
const markdownItForPlainText = new MarkdownItClass('default', { linkify: false, breaks: true, html: false })
    .use(markdownItEmoji)
    .use(plainTextPlugin);
const toSafeHtml = (text) => {
    if (!text)
        return '';
    const textWithLineFeed = text.replace(/<br.?\/?>/g, '\r\n');
    const html = markdownItForSafeHtml.render(textWithLineFeed);
    return sanitizeHtml(html, defaultSanitizeOptions);
};
exports.toSafeHtml = toSafeHtml;
const mdToOneLinePlainText = (text) => {
    if (!text)
        return '';
    markdownItForPlainText.render(text);
    return sanitizeHtml(markdownItForPlainText.plainText, textOnlySanitizeOptions);
};
exports.mdToOneLinePlainText = mdToOneLinePlainText;
function plainTextPlugin(markdownIt) {
    function plainTextRule(state) {
        const text = scan(state.tokens);
        markdownIt.plainText = text;
    }
    function scan(tokens) {
        let lastSeparator = '';
        let text = '';
        function buildSeparator(token) {
            if (token.type === 'list_item_close') {
                lastSeparator = ', ';
            }
            if (token.tag === 'br' || token.type === 'paragraph_close') {
                lastSeparator = ' ';
            }
        }
        for (const token of tokens) {
            buildSeparator(token);
            if (token.type !== 'inline')
                continue;
            for (const child of token.children) {
                buildSeparator(child);
                if (!child.content)
                    continue;
                text += lastSeparator + child.content;
                lastSeparator = '';
            }
        }
        return text;
    }
    markdownIt.core.ruler.push('plainText', plainTextRule);
}
//# sourceMappingURL=markdown.js.map