Skip to content

新的API或者回调函数的建议 #4491

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
idea-zone opened this issue Apr 4, 2022 · 4 comments
Closed

新的API或者回调函数的建议 #4491

idea-zone opened this issue Apr 4, 2022 · 4 comments

Comments

@idea-zone
Copy link

idea-zone commented Apr 4, 2022

在什么情况下你需要该特性? In what scenarios do you need this function?

  1. 自定义的导出:
    在导出 pdf 和 html 前,提供回调函数,允许通过 theme.js 改变生成的 html 代码块。

  2. 自定义解析行内块
    可以在行内块上保存(如code)自定义的属性,或者允许自定义 json 解析和渲染。

  3. 是否可以在 html 解析完成,或者笔记渲染完成时的回调函数,想调用 theme.js 中的函数执行自定义渲染。

例如:把行内代码块: #对外公开|0.0.1#(green) 渲染为
image

其实目前已经借用 theme.js 实现,但无法导出,因为导出为 pdf 和 html 没有调用 theme.js。

/**
 * 渲染解析书签
 */
async function render() {

    // 获取符合条件的 code 标签
    let elements = getElementWithoutCustomf_wz();

    // 正则表达式: '(#(.*?)[|](.*?)#){1,1}?([\(](#?[\\d\\w]+)(!)?[\)])?' 
    let simplePatt = new RegExp(config.theme.regs.wz);
    

    for(let e of elements){
       
        let oldHTML =e.innerHTML;
        // 处理 `#微章标题|微章内容#(颜色名称!)` 其中 ()
        if (simplePatt.test(oldHTML)){

            // let parseInfo = new ParseInfo(
            //     RegExp.$1,  // 微章标题
            //     RegExp.$2,  // 微章内容
            //     RegExp.$3,  // (颜色名称!) <可为空>, 这个不需要,主要用于调试和测试
            //     RegExp.$4,  // 颜色名称 <可为空>
            //     RegExp.$5   // ! <可为空>
            // )

            // #测试|efabcd#(#efabcd!)
            let parseInfo = new ParseInfo(
                // RegExp.$1, // #微章标题|微章内容#
                RegExp.$2,    // 微章标题
                RegExp.$3,    // 微章内容
                RegExp.$4,    // (颜色名称!) <可为空>, 这个不需要,主要用于调试和测试
                RegExp.$5,    // 颜色名称 <可为空>
                RegExp.$6     // ! <可为空>
            )

            if (!empty(parseInfo.wzTitle+parseInfo.wzMsg) && e.className!=='custom-codelabel-wz'){   
                
                let wz = new WzStyleInfo();

                e.className += 'custom-codelabel-wz'
                e.innerHTML = '<span>'+oldHTML +'</span>'
                e.setAttribute("custom-codelabel-wz-title",parseInfo.wzTitle)
                e.setAttribute("custom-codelabel-wz-msg",parseInfo.wzMsg)

                // 这个函数就是解析颜色,设置 wz.bgcolor 等的值
                wz.rerender(parseInfo.wzColor,parseInfo.wzEndsuffix)

                e.style ="--theme-wz-bgcolor: "+wz.bgcolor
                       +";--theme-wz-title-color:"+wz.titlecolor+";"
                       +";--theme-wz-msg-color:"+wz.msgcolor+";"
                       +";--theme-wz-msg-bgcolor:"+wz.msgbgcolor+";"
            }

        }
    }

}

// 对应的 css
/* ****************** custom-codelabel-wz *******************/
.protyle-wysiwyg *[data-node-id] :not([custom-f~=wz]) code.custom-codelabel-wz {
    /* background-color: #2aa899;   */
    background-color: var(--theme-wz-bgcolor);  
    padding: 5px;
    border-radius: 3px;
}

.protyle-wysiwyg *[data-node-id] .custom-codelabel-wz span{
    display: none;  
}

.protyle-wysiwyg *[data-node-id] .custom-codelabel-wz::before{
    color: var(--theme-wz-title-color);
    content: attr(custom-codelabel-wz-title);
    padding-right: 3px;
}

.protyle-wysiwyg *[data-node-id] .custom-codelabel-wz::after{
    /* color: #1f2e3b;  */
    color: var(--theme-wz-msg-color); 
    background-color: var(--theme-wz-msg-bgcolor);
    content: attr(custom-codelabel-wz-msg);
    padding: 3px;
    border-radius: 3px;
}

描述可能的最优解决方案 Describe the optimal solution

描述候选的解决方案 Describe the candidate solution

  1. 提供相关 API 或者回调事件
  1. 暴露底层的编辑器或者解析器出来

其他信息 Other information

希望思源笔记越来越好! 加油!

@88250
Copy link
Member

88250 commented Apr 4, 2022

你好,候选方案中提到的解析器是 Lute,Lute 不支持扩展 Parser,但是支持扩展 Renderer,应该可以通过覆写标签节点渲染函数实现这个需求。.sy JSON 的解析可以通过 Protyle 实现,解析为语法树以后就可以用修改版的 Lute 渲染了。

回调方案的话估计不好实现,因为导出这部分工作是完全由内核进程实现的,要回调的话估计通过 HTTP 要容易些,但是现有开发资源应该不会投入到这里,所以如果一定要实现这个需求的话,上面的方案可以尝试。

先关闭了,如果需要继续讨论请随时跟帖。

@88250 88250 closed this as completed Apr 4, 2022
@idea-zone
Copy link
Author

解析为语法树以后就可以用修改版的 Lute 渲

感谢您的回复。

能在 theme.js 中访问到思源笔记中的 Protyle 和 Lute 实列吗?有文档或者参考demo的链接吗?

@Vanessa219
Copy link
Member

目前没有封装相关的方法。Lute 的实列挂载在 Protyle 的实例上 protyle.lute。Protyle 实列就比较麻烦,可参考 siyuan.layout.centerLayout.children[0].children[0].model.editor.protyle

@idea-zone
Copy link
Author

siyuan.layout.centerLayout.children[0].children[0].model.editor.protyle

谢谢

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants