import remarkWikiLink from "remark-wiki-link" ; import unified from "unified" ; import unist from "unist" ; import { map as unistUtilMap } from "unist-util-map" ; export const noteLinkNodeType = "noteLinkNode" ; export interface NoteLinkNode extends unist . Node { type : typeof noteLinkNodeType ; targetNoteName : string ; targetNoteDisplayName : string ; } function transformWikiLinksIntoNoteLinks ( node ) { return unistUtilMap ( node , ( visitee ) => { if ( visitee . type === "wikiLink" ) { const wikiLinkNode = visitee as unknown as remarkWikiLink . RemarkWikiLinkNode ; return { type : "noteLinkNode" , targetNoteName : wikiLinkNode . value , targetNoteDisplayName : wikiLinkNode . data . alias , } as NoteLinkNode ; } else { return visitee ; } } ) ; } export default function noteLinkPlugin ( this ) { remarkWikiLink . apply ( this ) ; const data = this . data ( ) ; if ( ! data . toMarkdownExtensions ) { data . toMarkdownExtensions = [ ] ; } ( data . toMarkdownExtensions as any [ ] ) . push ( { handlers : { noteLinkNode : ( node ) => node . targetNoteName === node . targetNoteDisplayName ? `[[${ node . targetNoteName }]]` : `[[${ node . targetNoteName }:${ node . targetNoteDisplayName }]]` , } , } ) ; return transformWikiLinksIntoNoteLinks ; }