/* eslint-disable */ /**
 * HTML to Markdown conversion utilities
 * 无依赖的 HTML 到 Markdown 转换工具
 */ /**
 * 转换 DOM 节点为 Markdown
 */ function convertNodeToMarkdown(node, options) {
    if (node.nodeType === Node.TEXT_NODE) {
        return node.textContent || '';
    }
    if (node.nodeType === Node.ELEMENT_NODE) {
        var element = node;
        var tagName = element.tagName.toLowerCase();
        return convertElementToMarkdown(element, tagName, options);
    }
    // 处理其他节点类型
    if (node.nodeType === Node.COMMENT_NODE) {
        return options.preserveComments ? "<!--".concat(node.textContent, "-->") : '';
    }
    return '';
}
/**
 * 转换链接为 Markdown
 */ function convertLinkToMarkdown(element, text, options) {
    var href = element.getAttribute('href') || '';
    var linkText = text.trim();
    if (options.linkHandler) {
        return options.linkHandler(href, linkText);
    }
    return "[".concat(linkText, "](").concat(href, ")");
}
/**
 * 转换图片为 Markdown
 */ function convertImageToMarkdown(element, options) {
    var src = element.getAttribute('src') || '';
    var alt = element.getAttribute('alt') || '';
    var title = element.getAttribute('title') || '';
    if (options.imageHandler) {
        return options.imageHandler(src, alt);
    }
    var titleAttr = title ? ' "'.concat(title, '"') : '';
    return "![".concat(alt, "](").concat(src).concat(titleAttr, ")");
}
/**
 * 转换列表为 Markdown
 */ function convertListToMarkdown(element, isOrdered, options) {
    var items = Array.from(element.querySelectorAll('li'));
    var result = '';
    items.forEach(function(item, index) {
        var itemText = convertNodeToMarkdown(item, options).trim();
        if (isOrdered) {
            result += "".concat(index + 1, ". ").concat(itemText, "\n");
        } else {
            result += "- ".concat(itemText, "\n");
        }
    });
    return result + '\n';
}
/**
 * 转换表格为 Markdown
 */ function convertTableToMarkdown(element, options) {
    var rows = Array.from(element.querySelectorAll('tr'));
    if (rows.length === 0) return '';
    var result = '';
    // 处理表头
    var headerRow = rows[0];
    var headerCells = Array.from(headerRow.querySelectorAll('th, td'));
    result += '| ' + headerCells.map(function(cell) {
        return convertNodeToMarkdown(cell, options).trim();
    }).join(' | ') + ' |\n';
    // 添加分隔行
    result += '| ' + headerCells.map(function() {
        return '---';
    }).join(' | ') + ' |\n';
    // 处理数据行
    for(var i = 1; i < rows.length; i++){
        var row = rows[i];
        var cells = Array.from(row.querySelectorAll('td'));
        result += '| ' + cells.map(function(cell) {
            return convertNodeToMarkdown(cell, options).trim();
        }).join(' | ') + ' |\n';
    }
    return result.trim() + '\n';
}
/**
 * 转换表格行为 Markdown
 */ function convertTableRowToMarkdown(element, options) {
    var cells = Array.from(element.querySelectorAll('th, td'));
    return '| ' + cells.map(function(cell) {
        return convertNodeToMarkdown(cell, options).trim();
    }).join(' | ') + ' |\n';
}
/**
 * 转换 HTML 元素为 Markdown
 */ function convertElementToMarkdown(element, tagName, options) {
    var children = Array.from(element.childNodes).map(function(child) {
        return convertNodeToMarkdown(child, options);
    }).join('');
    switch(tagName){
        case 'h1':
            return "# ".concat(children, "\n\n");
        case 'h2':
            return "## ".concat(children, "\n\n");
        case 'h3':
            return "### ".concat(children, "\n\n");
        case 'h4':
            return "#### ".concat(children, "\n\n");
        case 'h5':
            return "##### ".concat(children, "\n\n");
        case 'h6':
            return "###### ".concat(children, "\n\n");
        case 'p':
            return "".concat(children, "\n\n");
        case 'br':
            return '\n';
        case 'strong':
        case 'b':
            return "**".concat(children, "**");
        case 'em':
        case 'i':
            return "*".concat(children, "*");
        case 'del':
        case 's':
            return "~~".concat(children, "~~");
        case 'code':
            return "`".concat(children, "`");
        case 'pre':
            {
                var codeElement = element.querySelector('code');
                if (codeElement) {
                    var language = codeElement.className.replace('language-', '') || '';
                    return "```".concat(language, "\n").concat(codeElement.textContent || '', "\n```\n\n");
                }
                return "```\n".concat(element.textContent || '', "\n```\n\n");
            }
        case 'blockquote':
            return "> ".concat(children.replace(/\n/g, '\n> '), "\n\n");
        case 'ul':
            return convertListToMarkdown(element, false, options);
        case 'ol':
            return convertListToMarkdown(element, true, options);
        case 'li':
            return "".concat(children, "\n");
        case 'a':
            return convertLinkToMarkdown(element, children, options);
        case 'img':
            return convertImageToMarkdown(element, options);
        case 'table':
            return convertTableToMarkdown(element, options);
        case 'tr':
            return convertTableRowToMarkdown(element, options);
        case 'th':
        case 'td':
            return children.trim();
        case 'hr':
            return '---\n\n';
        case 'div':
            return "".concat(children, "\n\n");
        case 'span':
            return children;
        default:
            return children;
    }
}
/**
 * 将 HTML 字符串转换为 Markdown
 * @param html HTML 字符串
 * @param options 转换选项
 * @returns Markdown 字符串
 */ export function htmlToMarkdown(html) {
    var options = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : {};
    if (!html || typeof html !== 'string') {
        return '';
    }
    // 创建临时 DOM 元素来解析 HTML
    var parser = new DOMParser();
    var doc = parser.parseFromString(html, 'text/html');
    return convertNodeToMarkdown(doc.body, options);
}
/**
 * 清理 HTML 字符串，移除不必要的空白和换行
 */ export function cleanHtml(html) {
    return html.replace(/\s+/g, ' ').replace(/>\s+</g, '><').replace(/\s+</g, '<').replace(/>\s+/g, '>').trim();
}
/**
 * 检测字符串是否为 HTML
 */ export function isHtml(text) {
    if (!text || text.trim() === '') {
        return false;
    }
    // 检查基本的 HTML 标签
    var htmlTags = /<\/?[a-z][\s\S]*>/i;
    return htmlTags.test(text);
}
/**
 * 从 HTML 中提取纯文本
 */ export function extractTextFromHtml(html) {
    if (!html) return '';
    var parser = new DOMParser();
    var doc = parser.parseFromString(html, 'text/html');
    return doc.body.textContent || '';
}
/**
 * 批量转换 HTML 片段为 Markdown
 */ export function batchHtmlToMarkdown(htmlFragments) {
    var options = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : {};
    return htmlFragments.map(function(fragment) {
        return htmlToMarkdown(fragment, options);
    });
}
