From 0cadc957c652443949d3cd3e6fb78d2bf00c6b77 Mon Sep 17 00:00:00 2001 From: zzhirong Date: Fri, 26 May 2023 00:01:47 +0800 Subject: [PATCH] Feat: Generate URI in 'Source' to point directly to a block. Obsidian already supports URI to a block by block ID through the URI 'obsidian://open?file=file#^block_id'. Please note that the 'file#^block_id' part needs to be URI encoded. Therefore, the URI in the 'Source' field will be 'obsidian://open?file={file}%23%5E{block_id}'. --- src/services/cards.ts | 5 +++++ src/services/parser.ts | 11 +++++++++-- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/src/services/cards.ts b/src/services/cards.ts index 238ea86..618f4e0 100644 --- a/src/services/cards.ts +++ b/src/services/cards.ts @@ -212,6 +212,11 @@ export class CardsService { card.reversed ? (total += 2) : total++; }); + if(this.settings.sourceSupport){ + this.parser.updateCardSource(cardsToCreate) + this.anki.updateCards(cardsToCreate) + } + this.updateFrontmatter(frontmatter, deckName); this.writeAnkiBlocks(cardsToCreate); diff --git a/src/services/parser.ts b/src/services/parser.ts index f7d2262..be6576d 100644 --- a/src/services/parser.ts +++ b/src/services/parser.ts @@ -493,14 +493,21 @@ export class Parser { return links; } + public updateCardSource(cards: Card[]){ + for(let card of cards){ + if(card.id == null) continue + card.fields["Source"] = card.fields["Source"].replace("__BLOCK_ID__", String(card.id)); + } + } + private substituteObsidianLinks(str: string, vaultName: string) { const linkRegex = /\[\[(.+?)(?:\|(.+?))?\]\]/gim; vaultName = encodeURIComponent(vaultName); return str.replace(linkRegex, (match, filename, rename) => { const href = `obsidian://open?vault=${vaultName}&file=${encodeURIComponent( - filename - )}.md`; + filename + "#^__BLOCK_ID__" + )}`; const fileRename = rename ? rename : filename; return `${fileRename}`; });