版本要求:思源笔记 v3.3.0
相关 issue:Export preview mode supports focus use · Issue #15340 · siyuan-note/siyuan
全部代码
let protyle = getProtyle();
let res = await fetchSyncPost("/api/export/exportMdContent", {
id: protyle.block.id || protyle.options.blockId || protyle.block.parentID,
yfm: false,
fillCSSVar: true, // true: 导出具体的css值,false:导出变量
refMode: 2, // 2:锚文本块链, 3:仅锚文本, 4:块引转脚注+锚点哈希
embedMode: 0, //0:使用原始文本,1:使用 Blockquote
adjustHeadingLevel: true
});
console.log(res.data)
function getProtyle() {
// Author: wilsons
try {
if(document.getElementById("sidebar")) return window.siyuan.mobile.editor.protyle;
const currDoc = window.siyuan?.layout?.centerLayout?.children.map(item=>item.children.find(item=>item.headElement?.classList.contains('item--focus') && (item.panelElement.closest('.layout__wnd--active')||item.panelElement.closest('[data-type="wnd"]')))).find(item=>item);
return currDoc?.model.editor.protyle;
} catch(e) {
console.error(e);
return null;
}
}
async function fetchSyncPost(url, data, returnType = 'json') {
const init = {
method: "POST",
};
if (data) {
if (data instanceof FormData) {
init.body = data;
} else {
init.body = JSON.stringify(data);
}
}
try {
const res = await fetch(url, init);
const res2 = returnType === 'json' ? await res.json() : await res.text();
return res2;
} catch (e) {
console.log(e);
return returnType === 'json' ? { code: e.code || 1, msg: e.message || "", data: null } : "";
}
}
笔记
获取 protyle 代码(如果使用插件的块菜单绑定等事件,可以直接获取 protyle 对象)
function getProtyle() {
// Author: wilsons
try {
if(document.getElementById("sidebar")) return window.siyuan.mobile.editor.protyle;
const currDoc = window.siyuan?.layout?.centerLayout?.children.map(item=>item.children.find(item=>item.headElement?.classList.contains('item--focus') && (item.panelElement.closest('.layout__wnd--active')||item.panelElement.closest('[data-type="wnd"]')))).find(item=>item);
return currDoc?.model.editor.protyle;
} catch(e) {
console.error(e);
return null;
}
}
let protyle = getProtyle();
获取 protyle 聚焦块
let id=protyle.block.id || protyle.options.blockId || protyle.block.parentID
获取聚焦块的 markdown:/api/export/exportMdContent
输入参数
-
id:块 id -
yfm: 是否导出 yaml 头 -
fillCSSVar- true: 导出具体的 css 值
- false:导出 css 变量
-
refMode:- 2:锚文本块链
- 3:仅锚文本
- 4:块引转脚注 + 锚点哈希
-
embedMode- 0:使用原始文本
- 1:使用 Blockquote
-
adjustHeadingLevel: 自动调整标题层级思源笔记 v3.3.0 新增
let res = await fetchSyncPost("/api/export/exportMdContent", {
id: protyle.block.id || protyle.options.blockId || protyle.block.parentID,
yfm: false,
fillCSSVar: true, // true: 导出具体的css值,false:导出变量
refMode: 2, // 2:锚文本块链, 3:仅锚文本, 4:块引转脚注+锚点哈希
embedMode: 0, //0:使用原始文本,1:使用 Blockquote
adjustHeadingLevel: true
});
console.log(res.data)
欢迎来到这里!
我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。
注册 关于