From 86b709de644b2d3b4d9f24eb9b3d8850ec314209 Mon Sep 17 00:00:00 2001 From: William Nurmi Date: Fri, 12 Jun 2026 13:20:19 +0300 Subject: [PATCH] fix: gap-separated id lines and end-of-line caret expressions MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Two review findings (pass 3) on the full-block text path: - a block id alone on its own line, one blank line below the content, belongs to the block above it (Obsidian's gap form). getBlockText() now jumps back to that block before expanding, so the alias carries the real block text instead of falling back to the block id. - the per-line trailing-id strip accepted a caret glued to content, eating e.g. the exponent of a line-final `2^10`. Obsidian only parses `^id` as a block id when it is whitespace-separated (or alone on the line) — the strip regex now requires the same. --- src/linkBuilder.test.ts | 5 +++++ src/linkBuilder.ts | 3 ++- src/main.ts | 14 +++++++++++++- 3 files changed, 20 insertions(+), 2 deletions(-) diff --git a/src/linkBuilder.test.ts b/src/linkBuilder.test.ts index dc5c905..b127cb3 100644 --- a/src/linkBuilder.test.ts +++ b/src/linkBuilder.test.ts @@ -791,6 +791,11 @@ describe('extractBlockDisplayText', () => { expect(result).toBe('value is 2^10 here'); }); + it('keeps an end-of-line caret expression intact (block ids need a separator)', () => { + const result = extractBlockDisplayText('first line ^abc123\n the result is 2^10', 'fallback', 99, 99); + expect(result).toBe('first line the result is 2^10'); + }); + it('strips pipes that would break the wiki link alias', () => { const result = extractBlockDisplayText('| col1 | col2 |', 'fallback', 99, 99); expect(result).not.toContain('|'); diff --git a/src/linkBuilder.ts b/src/linkBuilder.ts index 5cc2774..9476fd8 100644 --- a/src/linkBuilder.ts +++ b/src/linkBuilder.ts @@ -192,7 +192,8 @@ export function extractBlockDisplayText( let text = blockText .split('\n') .map((line) => line - .replace(/\s*\^[a-zA-Z0-9_-]+\s*$/, '') + // 块 ID 前必须有空白(或独占一行)才是真正的 ID——行尾的 2^10 这类不算 + .replace(/(?:^|\s+)\^[a-zA-Z0-9_-]+\s*$/, '') .trim() .replace(/^- \[.\]\s+/, '') .replace(/^- /, '')) diff --git a/src/main.ts b/src/main.ts index 981ab26..0a552c4 100644 --- a/src/main.ts +++ b/src/main.ts @@ -345,6 +345,16 @@ export default class EasyCopy extends Plugin { * 块 ID 作为显示文本(与旧版对这类块的兜底行为一致)。 */ private getBlockText(editor: Editor, cursorLine: number): string { + // 独立成行的块 ID(与内容隔一个空行)属于上面那个 block—— + // 先跳回上面的 block 再展开,别名才能取到真正的块内容 + if ( + cursorLine >= 2 && + /^\^[a-zA-Z0-9_-]+$/.test(editor.getLine(cursorLine).trim()) && + editor.getLine(cursorLine - 1).trim() === '' && + editor.getLine(cursorLine - 2).trim() !== '' + ) { + cursorLine -= 2; + } let start = cursorLine; while (start > 0) { const line = editor.getLine(start).trim(); @@ -372,7 +382,9 @@ export default class EasyCopy extends Plugin { if (trimmed.startsWith('|') || trimmed.startsWith('```') || trimmed.startsWith('$$')) { return ''; } - lines.push(line.replace(/\s*\^[a-zA-Z0-9_-]+\s*$/, '')); + // 块 ID 前必须有空白(或独占一行)才是真正的 ID—— + // 行尾的 2^10 这类插入语不能被当作块 ID 去掉 + lines.push(line.replace(/(?:^|\s+)\^[a-zA-Z0-9_-]+\s*$/, '')); } return lines.join('\n'); }