mirror of
https://github.com/ankitects/anki.git
synced 2025-09-20 15:02:21 -04:00
Add note ids to log and strip HTML
This commit is contained in:
parent
50c8267c15
commit
6eb8aa0987
4 changed files with 45 additions and 16 deletions
|
@ -37,11 +37,15 @@ message ImportAnkiPackageRequest {
|
|||
}
|
||||
|
||||
message ImportAnkiPackageResponse {
|
||||
message Note {
|
||||
notes.NoteId id = 1;
|
||||
repeated string fields = 2;
|
||||
}
|
||||
message Log {
|
||||
repeated generic.StringList new = 1;
|
||||
repeated generic.StringList updated = 2;
|
||||
repeated generic.StringList duplicate = 3;
|
||||
repeated generic.StringList conflicting = 4;
|
||||
repeated Note new = 1;
|
||||
repeated Note updated = 2;
|
||||
repeated Note duplicate = 3;
|
||||
repeated Note conflicting = 4;
|
||||
}
|
||||
collection.OpChanges changes = 1;
|
||||
Log log = 2;
|
||||
|
|
|
@ -69,6 +69,12 @@ impl From<pb::NoteId> for NoteId {
|
|||
}
|
||||
}
|
||||
|
||||
impl From<NoteId> for pb::NoteId {
|
||||
fn from(nid: NoteId) -> Self {
|
||||
pb::NoteId { nid: nid.0 }
|
||||
}
|
||||
}
|
||||
|
||||
impl From<pb::NotetypeId> for NotetypeId {
|
||||
fn from(ntid: pb::NotetypeId) -> Self {
|
||||
NotetypeId(ntid.ntid)
|
||||
|
|
|
@ -12,9 +12,9 @@ use sha1::Sha1;
|
|||
|
||||
use super::{media::MediaUseMap, Context};
|
||||
use crate::{
|
||||
import_export::package::{media::safe_normalized_file_name, NoteLog},
|
||||
import_export::package::{media::safe_normalized_file_name, LogNote, NoteLog},
|
||||
prelude::*,
|
||||
text::replace_media_refs,
|
||||
text::{replace_media_refs, strip_html_preserving_media_filenames, CowMapping},
|
||||
};
|
||||
|
||||
struct NoteContext<'a> {
|
||||
|
@ -39,21 +39,40 @@ pub(super) struct NoteImports {
|
|||
impl NoteImports {
|
||||
fn log_new(&mut self, note: Note, source_id: NoteId) {
|
||||
self.id_map.insert(source_id, note.id);
|
||||
self.log.new.push(note.take_fields().into());
|
||||
self.log.new.push(note.into_log_note());
|
||||
}
|
||||
|
||||
fn log_updated(&mut self, note: Note, source_id: NoteId) {
|
||||
self.id_map.insert(source_id, note.id);
|
||||
self.log.updated.push(note.take_fields().into());
|
||||
self.log.updated.push(note.into_log_note());
|
||||
}
|
||||
|
||||
fn log_duplicate(&mut self, note: Note, target_id: NoteId) {
|
||||
fn log_duplicate(&mut self, mut note: Note, target_id: NoteId) {
|
||||
self.id_map.insert(note.id, target_id);
|
||||
self.log.duplicate.push(note.take_fields().into());
|
||||
// id is for looking up note in *target* collection
|
||||
note.id = target_id;
|
||||
self.log.duplicate.push(note.into_log_note());
|
||||
}
|
||||
|
||||
fn log_conflicting(&mut self, note: Note) {
|
||||
self.log.conflicting.push(note.take_fields().into());
|
||||
self.log.conflicting.push(note.into_log_note());
|
||||
}
|
||||
}
|
||||
|
||||
impl Note {
|
||||
fn into_log_note(self) -> LogNote {
|
||||
LogNote {
|
||||
id: Some(self.id.into()),
|
||||
fields: self
|
||||
.take_fields()
|
||||
.into_iter()
|
||||
.map(|field| {
|
||||
strip_html_preserving_media_filenames(&field)
|
||||
.get_owned()
|
||||
.unwrap_or(field)
|
||||
})
|
||||
.collect(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -341,7 +360,7 @@ mod test {
|
|||
|
||||
assert_log(
|
||||
&ctx.imports.log.new,
|
||||
&[&["<img src='bar.jpg'>", ""], &["", ""], &["", ""]],
|
||||
&[&[" bar.jpg ", ""], &["", ""], &["", ""]],
|
||||
);
|
||||
assert_log(&ctx.imports.log.duplicate, &[&["outdated", ""]]);
|
||||
assert_log(
|
||||
|
@ -372,9 +391,9 @@ mod test {
|
|||
assert_eq!(col.get_note_field(updated_note_with_remapped_nt.id, 0), "");
|
||||
}
|
||||
|
||||
fn assert_log(log: &[crate::backend_proto::StringList], expected: &[&[&str]]) {
|
||||
for (idx, fields) in log.iter().enumerate() {
|
||||
assert_eq!(fields.vals, expected[idx]);
|
||||
fn assert_log(log: &[LogNote], expected: &[&[&str]]) {
|
||||
for (idx, note) in log.iter().enumerate() {
|
||||
assert_eq!(note.fields, expected[idx]);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -11,5 +11,5 @@ pub(crate) use colpkg::export::export_colpkg_from_data;
|
|||
pub use colpkg::import::import_colpkg;
|
||||
pub(self) use meta::{Meta, Version};
|
||||
|
||||
pub use crate::backend_proto::import_anki_package_response::Log as NoteLog;
|
||||
pub use crate::backend_proto::import_anki_package_response::{Log as NoteLog, Note as LogNote};
|
||||
pub(self) use crate::backend_proto::{media_entries::MediaEntry, MediaEntries};
|
||||
|
|
Loading…
Reference in a new issue