Skip to content

Commit

Permalink
fix: Bug obsidian tags not being added for regex notes (#230)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
st3v3nmw and ShootingKing-AM committed Jan 7, 2024
1 parent c25e583 commit a5e1473
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 4 deletions.
8 changes: 8 additions & 0 deletions src/note.ts
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,14 @@ export class RegexNote {
if (this.note_type.includes("Cloze") && !(note_has_clozes(template))) {
this.identifier = CLOZE_ERROR //An error code that says "don't add this note!"
}
if (data.add_obs_tags) {
for (let key in template["fields"]) {
for (let match of template["fields"][key].matchAll(OBS_TAG_REGEXP)) {
this.tags.push(match[1])
}
template["fields"][key] = template["fields"][key].replace(OBS_TAG_REGEXP, "")
}
}
template["tags"].push(...this.tags)
template["deckName"] = deck
return {note: template, identifier: this.identifier}
Expand Down
11 changes: 9 additions & 2 deletions tests/anki/test_question_answer.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def test_deck_default_exists(col: Collection):
assert col.decks.id_for_name('Default') is not None

def test_cards_count(col: Collection):
assert len(col.find_cards( col.build_search_string(SearchNode(deck='Default')) )) == 4
assert len(col.find_cards( col.build_search_string(SearchNode(deck='Default')) )) == 5

def test_cards_ids_from_obsidian(col: Collection):

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

note5 = col.get_note(anki_IDs[4])
assert note5.fields[0] == "How is this possible? "
assert note5.fields[1] == "The 'magic' of regular expressions! "
assert note5.has_tag('tag2')
assert note5.has_tag('tag1')

assert note1.note_type()["name"] == "Basic"
assert note2.note_type()["name"] == "Basic"
assert note3.note_type()["name"] == "Basic"
assert note4.note_type()["name"] == "Basic"
assert note4.note_type()["name"] == "Basic"
assert note5.note_type()["name"] == "Basic"
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
"CurlyCloze": true,
"CurlyCloze - Highlights to Clozes": false,
"ID Comments": true,
"Add Obsidian Tags": false
"Add Obsidian Tags": true
}
},
"Added Media": [],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,8 @@ A: No, and preceding whitespace will be ignored.

<!-- CARD -->
Q: How is this possible?
A: The 'magic' of regular expressions!
A: The 'magic' of regular expressions!

<!-- CARD -->
Q: How is this possible? #tag2
A: The 'magic' of regular expressions! #tag1

0 comments on commit a5e1473

Please sign in to comment.