调用 Lute 的正确方式

调用 Lute 的正确方式

see https://github.com/siyuan-note/siyuan/issues/14310

see https://ld246.com/article/1741527865453

调用方式:getLute().Md2BlockDOM(xxxxx);

封装函数如下:

function getLute() {
    const setLute = (options) => {
        const lute = window.Lute.New();
        lute.SetSpellcheck(window.siyuan.config.editor.spellcheck);
        lute.SetProtyleMarkNetImg(window.siyuan.config.editor.displayNetImgMark);
        lute.SetFileAnnotationRef(true);
        lute.SetHTMLTag2TextMark(true);
        lute.SetTextMark(true);
        lute.SetHeadingID(false);
        lute.SetYamlFrontMatter(false);
        lute.PutEmojis(options.emojis);
        lute.SetEmojiSite(options.emojiSite);
        lute.SetHeadingAnchor(options.headingAnchor);
        lute.SetInlineMathAllowDigitAfterOpenMarker(true);
        lute.SetToC(false);
        lute.SetIndentCodeBlock(false);
        lute.SetParagraphBeginningSpace(true);
        lute.SetSetext(false);
        lute.SetFootnotes(false);
        lute.SetLinkRef(false);
        lute.SetSanitize(options.sanitize);
        lute.SetChineseParagraphBeginningSpace(options.paragraphBeginningSpace);
        lute.SetRenderListStyle(options.listStyle);
        lute.SetImgPathAllowSpace(true);
        lute.SetKramdownIAL(true);
        lute.SetTag(true);
        lute.SetSuperBlock(true);
        lute.SetMark(true);
        lute.SetInlineAsterisk(window.siyuan.config.editor.markdown.inlineAsterisk);
        lute.SetInlineUnderscore(window.siyuan.config.editor.markdown.inlineUnderscore);
        lute.SetSup(window.siyuan.config.editor.markdown.inlineSup);
        lute.SetSub(window.siyuan.config.editor.markdown.inlineSub);
        lute.SetTag(window.siyuan.config.editor.markdown.inlineTag);
        lute.SetInlineMath(window.siyuan.config.editor.markdown.inlineMath);
        lute.SetGFMStrikethrough1(false);
        lute.SetGFMStrikethrough(window.siyuan.config.editor.markdown.inlineStrikethrough);
        lute.SetMark(window.siyuan.config.editor.markdown.inlineMark);
        lute.SetSpin(true);
        lute.SetProtyleWYSIWYG(true);
        if (options.lazyLoadImage) {
            lute.SetImageLazyLoading(options.lazyLoadImage);
        }
        lute.SetBlockRef(true);
        if (window.siyuan.emojis[0].items.length > 0) {
            const emojis = {};
            window.siyuan.emojis[0].items.forEach(item => {
                emojis[item.keywords] = options.emojiSite + "/" + item.unicode;
            });
            lute.PutEmojis(emojis);
        }
        return lute;
    };
  
    // 1. 优化查找函数(仅匹配 .editor.protyle 结尾路径)
    function findProtylePaths(obj) {
        const results = [];
        const seen = new Set();
  
        function walk(obj, path = '') {
            if (!obj || seen.has(obj)) return;
            seen.add(obj);
  
            for (const [key, value] of Object.entries(obj)) {
                const currentPath = path ? `${path}.${key}` : key;
              
                // 检查是否以 .editor.protyle 结尾
                if (currentPath.endsWith('.editor.protyle')) {
                    results.push({ path: currentPath, value });
                }
  
                if (typeof value === 'object') {
                    walk(value, currentPath);
                }
            }
        }
  
        walk(obj);
        return results;
    }
  
    // 2. 获取目标对象
    const protylePaths = findProtylePaths(window.siyuan);
    const firstProtyle = protylePaths[0]?.value;
  
    if (firstProtyle) {
        // 3. 动态设置 lute 并调用
        firstProtyle.lute = setLute({
            emojiSite: firstProtyle.options?.hint?.emojiPath,
            emojis: firstProtyle.options?.hint?.emoji,
            headingAnchor: false,
            listStyle: firstProtyle.options?.preview?.markdown?.listStyle,
            paragraphBeginningSpace: firstProtyle.options?.preview?.markdown?.paragraphBeginningSpace,
            sanitize: firstProtyle.options?.preview?.markdown?.sanitize,
        });
  
        // 4. 获取lute实例
        return firstProtyle.lute;
    } else {
        console.warn('未找到符合条件的 protyle 对象');
        return Lute.New();
    }
}
image.png

留下你的脚步
推荐阅读