Skip to content

A way to parse the inline latex like $latex$ #500

@acidsweet

Description

@acidsweet

Markwon can parse and render inline latex like $$latex$$,but it can't work with $latex$
by add another InlineProcessor,I found it can work well with both $$latext$$ and $latex$;code as follow:

  1. write a new JLatexMathInlineProcessor
class JLatexMathInlineProcessor2 : InlineProcessor() {
    override fun specialCharacter(): Char {
        return '$'
    }

    override fun parse(): Node? {
        val latex = match(RE) ?: return null

        val node = JLatexMathNode()
        node.latex(latex.substring(1, latex.length - 1))
        return node
    }

    companion object {
        private val RE: Pattern = Pattern.compile( "(?<!\\\\)\\$((?:\\\\.|[^$\\\\])*)\\$(?!\\$)")
    }
}
  1. add it to the MarkwonInlineParserPlugin
val markwonInlineParserPlugin = MarkwonInlineParserPlugin.create().also { 
                it.factoryBuilder().addInlineProcessor(JLatexMathInlineProcessor2())
            }
  1. make sure markwon use that plugin
val MARKWON = Markwon.builder(parent.context)
                .usePlugin(markwonInlineParserPlugin)
                .usePlugin(JLatexMathPlugin.create(markwonView.textSize
                ) { builder -> builder.inlinesEnabled(true) })
  1. it works

Image

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions