From 8bde0d4ac1e9f58b55addbb11327368f93fa0bed Mon Sep 17 00:00:00 2001 From: Damien Elmes Date: Sun, 26 Apr 2020 10:31:46 +1000 Subject: [PATCH] remove :{} and leading/trailing whitespace from field names on save --- rslib/src/notetype/fields.rs | 13 +++++++++++++ rslib/src/notetype/mod.rs | 5 +++++ 2 files changed, 18 insertions(+) diff --git a/rslib/src/notetype/fields.rs b/rslib/src/notetype/fields.rs index e2b8cd05e..6ba1ce03e 100644 --- a/rslib/src/notetype/fields.rs +++ b/rslib/src/notetype/fields.rs @@ -34,4 +34,17 @@ impl NoteField { }, } } + + pub(crate) fn fix_name(&mut self) { + // remove special characters + let bad_chars = |c| c == ':' || c == '{' || c == '}'; + if self.name.contains(bad_chars) { + self.name = self.name.replace(bad_chars, ""); + } + // and leading/trailing whitespace + let trimmed = self.name.trim(); + if trimmed.len() != self.name.len() { + self.name = trimmed.into(); + } + } } diff --git a/rslib/src/notetype/mod.rs b/rslib/src/notetype/mod.rs index 2996fb671..80b479b5e 100644 --- a/rslib/src/notetype/mod.rs +++ b/rslib/src/notetype/mod.rs @@ -194,6 +194,7 @@ impl NoteType { return Err(AnkiError::invalid_input("1 template required")); } self.normalize_names(); + self.fix_field_names(); self.ensure_names_unique(); self.reposition_sort_idx(); @@ -289,6 +290,10 @@ impl NoteType { pub fn target_deck_id(&self) -> DeckID { DeckID(self.config.target_deck_id) } + + fn fix_field_names(&mut self) { + self.fields.iter_mut().for_each(NoteField::fix_name); + } } impl From for NoteTypeProto {