ajalt / mordant

Multiplatform text styling for Kotlin command-line applications
https://ajalt.github.io/mordant/
Apache License 2.0
935 stars 33 forks source link

Markdown fails to render dollar sign #182

Closed Caralluin closed 3 weeks ago

Caralluin commented 3 weeks ago

Attempting to render text with a dollar sign as Markdown fails with an exception.

Example:

Terminal().println(Markdown("Just $10"))

this fails with

java.lang.IllegalStateException: Unexpected token when parsing inlines: org.intellij.markdown.ast.LeafASTNode@4982cc36; [Markdown:DOLLAR:'$'}]
    at com.github.ajalt.mordant.markdown.MarkdownRenderer.parseInlines(MarkdownRenderer.kt:262)
    at com.github.ajalt.mordant.markdown.MarkdownRenderer.innerInlines$lambda$11(MarkdownRenderer.kt:274)
    at kotlin.text.StringsKt__AppendableKt.appendElement(Appendable.kt:84)
    at kotlin.collections.CollectionsKt___CollectionsKt.joinTo(_Collections.kt:3493)
    at kotlin.collections.CollectionsKt___CollectionsKt.joinToString(_Collections.kt:3510)
    at kotlin.collections.CollectionsKt___CollectionsKt.joinToString$default(_Collections.kt:3509)
    at com.github.ajalt.mordant.markdown.MarkdownRenderer.innerInlines(MarkdownRenderer.kt:274)
    at com.github.ajalt.mordant.markdown.MarkdownRenderer.innerInlines$default(MarkdownRenderer.kt:272)
    at com.github.ajalt.mordant.markdown.MarkdownRenderer.parseStructure(MarkdownRenderer.kt:119)
    at com.github.ajalt.mordant.markdown.MarkdownRenderer.parseBlocks$lambda$0(MarkdownRenderer.kt:69)
    at com.github.ajalt.mordant.table.TableDslKt.verticalLayout(TableDsl.kt:375)
    at com.github.ajalt.mordant.markdown.MarkdownRenderer.parseBlocks(MarkdownRenderer.kt:57)
    at com.github.ajalt.mordant.markdown.MarkdownRenderer.parseFile(MarkdownRenderer.kt:53)
    at com.github.ajalt.mordant.markdown.MarkdownRenderer.render(MarkdownRenderer.kt:47)
    at com.github.ajalt.mordant.markdown.Markdown.document(Markdown.kt:25)
    at com.github.ajalt.mordant.markdown.Markdown.render(Markdown.kt:31)
    at com.github.ajalt.mordant.rendering.Widget$DefaultImpls.render$default(Widget.kt:7)
    at com.github.ajalt.mordant.terminal.Terminal.render(Terminal.kt:279)
    at com.github.ajalt.mordant.terminal.Terminal.println(Terminal.kt:238)
    at com.github.ajalt.mordant.terminal.Terminal.println$default(Terminal.kt:237)

What works are dollar signs within backticks:

Terminal().println(Markdown("`this $ works`"))
FelixZY commented 3 weeks ago

Some math plugins for markdown uses $ as a delimiter (https://meta.stackexchange.com/a/263344/1459143). While I doubt mordant has such support, perhaps a (temporary) workaround in your case could be to escape the dollar sign (i.e. \$ or \\$)?

Caralluin commented 3 weeks ago

perhaps a (temporary) workaround in your case could be to escape the dollar sign (i.e. \$ or \\$)?

Unfortunately escaping with a single backslash has no effect here. A double backslash prevents the exception, but is rendered itself before the dollar sign (\$), which is not really a solution.

ajalt commented 3 weeks ago

It looks like the markdown parser started parsing math syntax in https://github.com/JetBrains/markdown/issues/133. There's no way to opt-out, so I'll have to update Mordant's renderer to handle the new token types they emit. There's of course no way to render the embedded math markup itself, so I guess I'll just put it in code blocks.