From e0ae5b203ed667e78959955fa36bd82e9fb30ed5 Mon Sep 17 00:00:00 2001 From: William Nurmi Date: Fri, 12 Jun 2026 13:10:51 +0300 Subject: [PATCH] fix: derive the full-block alias from the block that owns the id MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Review finding (pass 2): with the full-block setting on, the alias was expanded from the cursor line while detectBlockRange() (which only treats `- ` as a boundary) could detect a block id on a neighboring ordered/`* `/`+ ` list item — producing a link whose alias text comes from a different block than its target. Expand the alias from the line the id actually sits on (ContextData.line, set by detectBlockId) when copying, and from the insertion line (range end) when generating a new id. Detection and insertion behavior are unchanged. --- src/main.ts | 15 +++++++++++---- src/type.ts | 1 + 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/src/main.ts b/src/main.ts index 6f66d3a..981ab26 100644 --- a/src/main.ts +++ b/src/main.ts @@ -384,6 +384,7 @@ export default class EasyCopy extends Plugin { const cursor = editor.getCursor(); const { end } = this.detectBlockRange(editor, cursor.line); let lastLine = editor.getLine(end); + let lastLineNo = end; // 检查下两行是否为单独的块ID行 if (end <= editor.lineCount() - 2) { @@ -393,6 +394,7 @@ export default class EasyCopy extends Plugin { // 判断该行是否为合法的 block ID 行:前面可有空格,必须以 ^ 开头,后面只能是 block id,不允许有其他字符或空格 if (/^\s*\^[a-zA-Z0-9_-]+$/.test(possibleBlockIdLine)) { lastLine = possibleBlockIdLine; + lastLineNo = end + 2; } } } @@ -403,6 +405,7 @@ export default class EasyCopy extends Plugin { return { type: ContextType.BLOCKID, curLine: lastLine, + line: lastLineNo, match: match[1], range: [lastLine.lastIndexOf('^'), lastLine.length] }; @@ -651,10 +654,12 @@ export default class EasyCopy extends Plugin { switch (contextType.type) { case ContextType.BLOCKID: // 显示文本来源:设置开启时取整个 block 的文本(多行块的别名才完整), - // 否则保持旧行为,仅用检测到块 ID 的那一行 + // 否则保持旧行为,仅用检测到块 ID 的那一行。 + // 从块 ID 实际所在的行(而不是光标行)展开,确保别名和链接 + // 指向同一个 block——检测范围可能跨越列表项。 this.copyBlockLink(contextType.match!, filename, true, this.settings.blockDisplayFullBlock - ? this.getBlockText(editor, editor.getCursor().line) + ? this.getBlockText(editor, contextType.line ?? editor.getCursor().line) : contextType.curLine); return; case ContextType.BOLD: @@ -921,9 +926,11 @@ export default class EasyCopy extends Plugin { const cursor = editor.getCursor(); const { start, end } = this.detectBlockRange(editor, cursor.line); // 在插入块 ID 之前取显示文本来源:设置开启时取整个 block 的文本 - // (多行 → 一行),否则保持旧行为,仅用 block 的第一行 + // (多行 → 一行),否则保持旧行为,仅用 block 的第一行。 + // 从将要插入块 ID 的行(end)展开,确保别名和链接指向同一个 + // block——检测范围可能跨越列表项。 const blockText = this.settings.blockDisplayFullBlock - ? this.getBlockText(editor, cursor.line) + ? this.getBlockText(editor, end) : editor.getLine(start); const lastLine = editor.getLine(end); diff --git a/src/type.ts b/src/type.ts index 636eba8..1e6b9f0 100644 --- a/src/type.ts +++ b/src/type.ts @@ -37,6 +37,7 @@ export enum ContextType { export interface ContextData { type: ContextType; curLine: string; + line?: number; // curLine 所在的行号(目前仅 BLOCKID 上下文会设置) match: string | null; range: [number, number] | null; }