import { AttachmentID , parseSingleCurlyBraceClozePromptMarkup , TaskContent , TaskContentField , TaskContentType , TaskSpec , TaskSpecType , } from "@withorbit/core" ; import { Note , splitAnkiDBNoteFields } from "./ankiPkg" ; import { AnkiAttachmentReference } from "./ankiPkg/ankiAttachmentReference" ; import parseAnkiField from "./ankiPkg/parseAnkiField" ; import { convertAnkiID } from "./convertAnkiID" ; import { ModelMapping , ModelMappingType } from "./modelMapping" ; export function mapNoteToTaskSpec ( note , modelMapping ) { const fields = splitAnkiDBNoteFields ( note . flds ) ; const issues = [ ] ; function checkForDroppedFields ( usedFieldIndexes ) { fields . forEach ( ( field , index ) => { if ( field !== "" && ! usedFieldIndexes . includes ( index ) ) { issues . push ( `Note contains data in extra field which will be dropped: "${ field }"` ) ; } } ) ; } function transformAttachmentReferences ( attachmentReferences ) { return attachmentReferences . map ( ( ref ) => convertAnkiID ( ref . name ) ) ; } function transformQAAnkiField ( field ) { const { contentsMarkdown , attachmentReferences } = parseAnkiField ( field ) ; return { text : contentsMarkdown , attachments : transformAttachmentReferences ( attachmentReferences ) , } ; } let content ; switch ( modelMapping . type ) { case ModelMappingType . Basic : checkForDroppedFields ( [ modelMapping . questionFieldIndex , modelMapping . answerFieldIndex , ] ) ; content = { type : TaskContentType . QA , body : transformQAAnkiField ( fields [ modelMapping . questionFieldIndex ] ) , answer : transformQAAnkiField ( fields [ modelMapping . answerFieldIndex ] ) , } ; break ; case ModelMappingType . Cloze : checkForDroppedFields ( [ modelMapping . contentsFieldIndex ] ) ; const { contentsMarkdown , attachmentReferences } = parseAnkiField ( fields [ modelMapping . contentsFieldIndex ] ) ; const { markupWithoutBraces , clozeComponents } = parseSingleCurlyBraceClozePromptMarkup ( contentsMarkdown ) ; content = { type : TaskContentType . Cloze , body : { text : markupWithoutBraces , attachments : transformAttachmentReferences ( attachmentReferences ) , } , components : clozeComponents , } ; } return { spec : { type : TaskSpecType . Memory , content , } , issues , } ; }