如何在复制一段文本到其他文档时, 自动在末尾加上来源文档的双链?

遇到的问题

如题, 用text transporter 可以复制为引用块或者链接, 但无法修改

预期的效果

希望能在复制粘贴一段文本后自动在其末尾加上来源文件的md双链, 最好可以自定义前面加空格还是其他连接符. 如果可以和text transporter联动直接复制文本和文本所在块引用就更完美了

尝试的方法

试了quickadd, 但没有找到可以组合复制文本和块引用的方法
试了text transporter中的copy block as embeds 和 replace link as text and alias 联用, 但一方面是只能复制整块的内容, 一方面操作仍然比较繁琐, 无法一键复制粘贴

1 个赞

可以参考


实测这个 quickadd 代码基本可以满足要求,

作者最后只复制了 “blockref 链接”,
如果要把 “当前段落的原文” 也复制到剪切板,
应改成类似下面这样:

// 原先
navigator.clipboard.writeText(`[[${app.workspace.getActiveFile().basename}#^${newID.slice(2)}]]`)

// 要改成也输出 str (目标段落原文), 比如
navigator.clipboard.writeText(`${str} - [[${app.workspace.getActiveFile().basename}#^${newID.slice(2)}]]`)
1 个赞

非常感谢! 完美解决问题!

以防有像我一样的小白搞不懂咋整, 复制以下代码为.js文件在quickadd macro里加载就阔以了

module.exports = async ({ quickAddApi })=>{
let customRandom = (alphabet, size)=> {
  let mask = (2 << Math.log(alphabet.length-1) / Math.LN2)-1, step = -~(1.6 * mask * size / alphabet.length)
  , id = '', bytes = crypto.getRandomValues(new Uint8Array(step)), j = step
  while (j--) { id += alphabet[bytes[j] & mask] || ''; if (id.length === size) return id }
}, Editor = app.workspace.activeEditor.editor, line = Editor.getCursor().line, str = Editor.getLine(line)
, rgx = / \^[a-z0-9]{6,}$/gm, ids = Editor.getValue().match(rgx) || []
, newID = ` ^${customRandom('abcdefghijklmnopqrstuvwz0123456789', 6)}`
, set = Array.from(new Set(ids.sort().filter((x, i)=> x == ids[i+1]).map(x=> x)))
if (set[0]) { new Notice(`重复项${set}`); navigator.clipboard.writeText(set) }
if (!str.match(rgx) && !ids.includes(newID)) { ids.push(newID)
  Editor.replaceRange(newID, {line: line, ch: str.length}) }
navigator.clipboard.writeText(`${str} - [[${app.workspace.getActiveFile().basename}#^${newID.slice(2)}]]`)
}
2 个赞

看看这个插件能否满足你的要求: Carry-Forward

1 个赞