From 0d464eed32ff44d0bc069a2fdff62cdde80f8578 Mon Sep 17 00:00:00 2001 From: Moy Date: Tue, 5 Aug 2025 00:29:35 +0800 Subject: [PATCH] feat: enhance block ID detection to include next lines --- src/main.ts | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/src/main.ts b/src/main.ts index b1d981b..0548a73 100644 --- a/src/main.ts +++ b/src/main.ts @@ -158,10 +158,26 @@ export default class EasyCopy extends Plugin { return { start, end }; } + /* + * 从给定的块里查找 Block ID(最多延伸至下一个空行+下第二行) + */ private detectBlockId(editor: Editor, view: MarkdownView): ContextData | null { const cursor = editor.getCursor(); const { end } = this.detectBlockRange(editor, cursor.line); - const lastLine = editor.getLine(end); + let lastLine = editor.getLine(end); + + // 检查下两行是否为单独的块ID行 + if (end <= editor.lineCount() - 2) { + const lineAfterBlock = editor.getLine(end + 1); + if (lineAfterBlock.trim() === '') { + const possibleBlockIdLine = editor.getLine(end + 2); + // 判断该行是否为合法的 block ID 行:前面可有空格,必须以 ^ 开头,后面只能是 block id,不允许有其他字符或空格 + if (/^\s*\^[a-zA-Z0-9_-]+$/.test(possibleBlockIdLine)) { + lastLine = possibleBlockIdLine; + } + } + } + const match = lastLine.trimEnd().match(/\^([a-zA-Z0-9_-]+)$/); if (match) {