import remarkGfm from "remark-gfm";
import remarkParse from "remark-parse";
import { unified } from "unified";
import { visit } from "unist-util-visit";
var mdastFootnoteProcessor = unified().use(remarkParse).use(remarkGfm, {
    singleTilde: false
});
var mdastPlainText = function mdastPlainText1(node) {
    if (!node) return '';
    if (node.type === 'text') return node.value || '';
    if (Array.isArray(node.children)) {
        return node.children.map(mdastPlainText).join('');
    }
    return '';
};
/**
 * 从 Markdown 源码提取 GFM 脚注定义，结构与 Slate 只读 Editor 中
 * `fncProps.onFootnoteDefinitionChange` 入参对齐。
 */ export var extractFootnoteDefinitionsFromMarkdown = function extractFootnoteDefinitionsFromMarkdown(content) {
    if (!(content === null || content === void 0 ? void 0 : content.trim())) return [];
    try {
        var tree = mdastFootnoteProcessor.parse(content);
        var list = [];
        visit(tree, 'footnoteDefinition', function(node) {
            var _node_label;
            list.push({
                id: node.identifier,
                placeholder: (_node_label = node.label) !== null && _node_label !== void 0 ? _node_label : node.identifier,
                origin_text: mdastPlainText({
                    children: node.children
                }),
                url: undefined,
                origin_url: undefined
            });
        });
        return list;
    } catch (unused) {
        return [];
    }
};
