Skip to content

Commit a5e1473

Browse files
fix: Bug obsidian tags not being added for regex notes (#230)
* fix addition of Obsidian tags in regex notes * fix lint * Add e2e tests so that bug will be caught in future * rebased from master --------- Co-authored-by: Harsha Raghu <narnindi.raghu@gmail.com>
1 parent c25e583 commit a5e1473

4 files changed

Lines changed: 23 additions & 4 deletions

File tree

src/note.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,14 @@ export class RegexNote {
302302
if (this.note_type.includes("Cloze") && !(note_has_clozes(template))) {
303303
this.identifier = CLOZE_ERROR //An error code that says "don't add this note!"
304304
}
305+
if (data.add_obs_tags) {
306+
for (let key in template["fields"]) {
307+
for (let match of template["fields"][key].matchAll(OBS_TAG_REGEXP)) {
308+
this.tags.push(match[1])
309+
}
310+
template["fields"][key] = template["fields"][key].replace(OBS_TAG_REGEXP, "")
311+
}
312+
}
305313
template["tags"].push(...this.tags)
306314
template["deckName"] = deck
307315
return {note: template, identifier: this.identifier}

tests/anki/test_question_answer.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def test_deck_default_exists(col: Collection):
2424
assert col.decks.id_for_name('Default') is not None
2525

2626
def test_cards_count(col: Collection):
27-
assert len(col.find_cards( col.build_search_string(SearchNode(deck='Default')) )) == 4
27+
assert len(col.find_cards( col.build_search_string(SearchNode(deck='Default')) )) == 5
2828

2929
def test_cards_ids_from_obsidian(col: Collection):
3030

@@ -63,7 +63,14 @@ def test_cards_front_back_tag_type(col: Collection):
6363
assert note4.fields[0] == "How is this possible?"
6464
assert note4.fields[1] == "The 'magic' of regular expressions!"
6565

66+
note5 = col.get_note(anki_IDs[4])
67+
assert note5.fields[0] == "How is this possible? "
68+
assert note5.fields[1] == "The 'magic' of regular expressions! "
69+
assert note5.has_tag('tag2')
70+
assert note5.has_tag('tag1')
71+
6672
assert note1.note_type()["name"] == "Basic"
6773
assert note2.note_type()["name"] == "Basic"
6874
assert note3.note_type()["name"] == "Basic"
69-
assert note4.note_type()["name"] == "Basic"
75+
assert note4.note_type()["name"] == "Basic"
76+
assert note5.note_type()["name"] == "Basic"

tests/defaults/test_vault_suites/question_answer/.obsidian/plugins/obsidian-to-anki-plugin/data.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
"CurlyCloze": true,
3838
"CurlyCloze - Highlights to Clozes": false,
3939
"ID Comments": true,
40-
"Add Obsidian Tags": false
40+
"Add Obsidian Tags": true
4141
}
4242
},
4343
"Added Media": [],

tests/defaults/test_vault_suites/question_answer/question_answer.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,8 @@ A: No, and preceding whitespace will be ignored.
1818

1919
<!-- CARD -->
2020
Q: How is this possible?
21-
A: The 'magic' of regular expressions!
21+
A: The 'magic' of regular expressions!
22+
23+
<!-- CARD -->
24+
Q: How is this possible? #tag2
25+
A: The 'magic' of regular expressions! #tag1

0 commit comments

Comments
 (0)