protected String transformMardownLinks ( String content , ReferenceContext references ) { if ( ! isMarkdownToHtmlReferenceTransformation ( ) ) { return content ; } // Prepare replacement data structures final Map < BasedSequence , String > replacements = new TreeMap <> ( ( cmp1 , cmp2 ) -> { final int cmp = Integer . compare ( cmp2 . getStartOffset ( ) , cmp1 . getStartOffset ( ) ) ; if ( cmp != 0 ) { return cmp ; } return Integer . compare ( cmp2 . getEndOffset ( ) , cmp1 . getEndOffset ( ) ) ; } ) ; // Visit the links and record the transformations final MutableDataSet options = new MutableDataSet ( ) ; final Parser parser = Parser . builder ( options ) . build ( ) ; final Node document = parser . parse ( content ) ; final NodeVisitor visitor = new NodeVisitor ( new VisitHandler <> ( Link . class , it -> { URL url = FileSystem . convertStringToURL ( it . getUrl ( ) . toString ( ) , true ) ; url = transformURL ( url , references ) ; if ( url != null ) { replacements . put ( it . getUrl ( ) , convertURLToString ( url ) ) ; } } ) ) ; visitor . visitChildren ( document ) ; // Apply the replacements if ( ! replacements . isEmpty ( ) ) { final StringBuilder buffer = new StringBuilder ( content ) ; for ( final Entry < BasedSequence , String > entry : replacements . entrySet ( ) ) { final BasedSequence seq = entry . getKey ( ) ; buffer . replace ( seq . getStartOffset ( ) , seq . getEndOffset ( ) , entry . getValue ( ) ) ; } return buffer . toString ( ) ; } return content ; }
Apply link transformation on the Markdown links .