基本为 @BryceAndJuly 大佬的 【分享】使用当前文档的二级标题快速制作闪卡,我只是修改了获取块 id 的方式,使其能支持获取当前文档带标记的块。
设置方式参考【分享】使用当前文档的二级标题快速制作闪卡。
使用演示:
注意:
- 长文档受到思源动态加载块限制,未加载的块不会被制卡。
- 不支持行级元素嵌套,比如说标记加粗体
代码见下:
// 请求函数 function request(url, data = null, method = "POST") { return new Promise((resolve, reject) => { if (method.toUpperCase() == "POST") { fetch(url, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify(data), }) .then( (data) => resolve(data.json()), (error) => { reject(error); } ) .catch((err) => { console.error("请求失败:", err); }); } }); } // 弹出提示信息 async function showMessage(msg) { await request("/api/notification/pushMsg", { msg, timeout: 3000 }); } // 添加多个卡片 async function addCards() { // 获取需要被制成卡片的块的ID const currentPage = document.querySelector( ".fn__flex-1.protyle:not(.fn__none)" ); const markList = currentPage.querySelectorAll("span[data-type='mark']") if (markList && markList.length > 0) { let arr = []; Array.from(markList).forEach((item) => { let id = item.parentElement.parentElement.getAttribute("data-node-id"); if (id) { arr.push(id); } }) // 去重 arr = [... new Set(arr)] // 把标记所在块全部制成卡片 let body = { deckID: "20230131181452-sl72uxn", blockIDs: arr, }; let res = await request("/api/riff/addRiffCards", body); if (res.code === 0) { showMessage(`当前卡包的总卡片数:${res.data.size}`); } } } // 添加一个按钮 const barMode = document.getElementById("barMode"); barMode.insertAdjacentHTML( "beforebegin", '<div id="addCards" class="toolbar__item b3-tooltips b3-tooltips__se" aria-label="制作卡片" ></div>' ); const addCardsBtn = document.getElementById("addCards"); addCardsBtn.style.width = "auto"; const addCardsBtnIcon = `<svg t="1674130669751" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="4526" width="200" height="200"><path d="M928 160H96a32 32 0 0 0-32 32v672a32 32 0 0 0 32 32h832a32 32 0 0 0 32-32V192a32 32 0 0 0-32-32z m-32 672H128V224h768v608z" p-id="4527" fill="#9aa0a6"></path><path d="M230.592 448.096H544a32 32 0 1 0 0-64H230.592a32 32 0 0 0 0 64zM230.592 640.096H544a32 32 0 1 0 0-64H230.592a32 32 0 1 0 0 64zM768 704a32 32 0 0 0 32-32V350.016a32 32 0 1 0-64 0V672a32 32 0 0 0 32 32z" p-id="4528" fill="#9aa0a6"></path></svg>`; addCardsBtn.innerHTML = addCardsBtnIcon; addCardsBtn.addEventListener( "click", (e) => { addCards(); e.stopPropagation(); e.preventDefault(); }, true );
欢迎来到这里!
我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。
注册 关于