import * as mdast from "mdast" ; import rehypeParse from "rehype-parse" ; import rehype2Remark from "rehype-remark" ; import remarkStringify from "remark-stringify" ; import { unified , Processor } from "unified" ; import { selectAll } from "unist-util-select" ; import { AnkiAttachmentReference } from "./ankiAttachmentReference" ; export function stripImages ( this ) { const data = this . data ( ) ; if ( ! data . toMarkdownExtensions ) { data . toMarkdownExtensions = [ ] ; } ( data . toMarkdownExtensions as any [ ] ) . push ( { handlers : { image : ( ) => "" } } ) ; } const processor = unified ( ) . use ( rehypeParse ) . use ( rehype2Remark ) . use ( remarkStringify ) . use ( stripImages ) ; export default function parseAnkiField ( ankiField ) { const ast = processor . parse ( ankiField ) ; const markdownAST = processor . runSync ( ast ) ; let markdown = processor . stringify ( markdownAST ) ; const images = selectAll ( "image" , markdownAST ) as mdast . Image [ ] ; const soundRegexp = /\\\[sound:([^\]]+)]/g ; const soundNames = [ ... markdown . matchAll ( soundRegexp ) ] . map ( ( matchArray ) => matchArray [ 1 ] ) ; markdown = markdown . replace ( soundRegexp , "" ) ; markdown = markdown . replace ( /{{c\d+::([^}]+)}}/g , "{$1}" ) ; markdown = markdown . replace ( /\n\n\\/gm , "" ) ; return { contentsMarkdown : markdown . trimRight ( ) , attachmentReferences : [ ... images . map ( ( image ) => ( { type : "image" , name : image . url , } as const ) ) , ... soundNames . map ( ( soundName ) => ( { type : "sound" , name : soundName , } as const ) ) , ] , } ; }