From d8c0ab5d1a44536c23f25d55cfff6be598ef9b2b Mon Sep 17 00:00:00 2001 From: Erik Demaine Date: Thu, 14 Dec 2023 11:58:55 -0500 Subject: [PATCH] \verb support outside math mode --- CHANGELOG.md | 2 +- lib/formats.coffee | 12 ++++++++---- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ae80a10..1428d53 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,7 +10,7 @@ instead of version numbers. ## 2023-12-14 * Copy/pasting a URL from a message no longer adds extraneous zero-width spaces -* `\verb|$|` now works inside math mode +* `\verb|$|` now works both inside and outside math mode ## 2023-12-05 diff --git a/lib/formats.coffee b/lib/formats.coffee index 11cc57c..3c2f3ba 100644 --- a/lib/formats.coffee +++ b/lib/formats.coffee @@ -211,16 +211,16 @@ boldWeight = 700 ## (e.g. % should not be a comment when in a URL or verbatim). latex2htmlVerb = (tex) -> tex.replace /\\begin\s*{verbatim}([^]*?)\\end\s*{verbatim}/g, - (match, verb) -> "
#{latexEscape verb}
" - .replace /\\url\s*{([^{}]*)}/g, (match, url) -> + (match, verb) => "
#{latexEscape verb}
" + .replace /\\url\s*{([^{}]*)}/g, (match, url) => url = latexURL url """#{latexEscape url}""" .replace /\\href\s*{([^{}]*)}\s*{((?:[^{}]|{[^{}]*})*)}/g, - (match, url, text) -> + (match, url, text) => url = latexURL url """#{text}""" .replace /\\begin\s*{CJK(\*?)}\s*{UTF8}\s*{[^{}]*}(.*?)\\end{CJK\*?}/g, - (match, star, text) -> + (match, star, text) => text = text.replace /\s+/g, '' if star text @@ -293,6 +293,10 @@ splitOutside = (text, re) -> ## in Markdown too. latex2htmlCommandsAlpha = (tex, math) -> tex = tex + ## \verb is here instead of latex2htmlVerb so that it's processed after + ## math blocks are extracted (as KaTeX has its own \verb support) + .replace /\\verb(.)(.*?)\1/g, + (match, char, verb) => "#{latexEscape verb}" ## Process tabular environments first in order to split cells at & ## (so e.g. \bf is local to the cell) .replace /\\begin\s*{tabular}\s*{([^{}]*)}([^]*?)\\end\s*{tabular}/g, (m, cols, body) ->