fix: gap-separated id lines and end-of-line caret expressions

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.
This commit is contained in:
William Nurmi 2026-06-12 13:20:19 +03:00
parent e0ae5b203e
commit 86b709de64
3 changed files with 20 additions and 2 deletions

View file

@ -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('|');

View file

@ -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(/^- /, ''))

View file

@ -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');
}