Well, I spent more than than I care to admit to convert and fix broken content after upgrading to the new math plugin, so I’m posting here the important commands I used (excuse the ugly hackery, got too lazy). Some manual fixing of content was still necessary, but not that much in our case.
NOTE: This is for inspirational purposes. Recall the “I take no responsibility …” poem. Don’t run this blindly if you don’t understand it.
# replace \\ into \ (which also translates \\\\ into \\)
Post.where('raw ~ ?', "\\\\").each{|p| p.raw=p.raw.gsub(/\\\\/, "\\"); p.save; print "."; $stdout.flush;}; puts "";
# replace {align} with {aligned} and remove all starred versions
Post.where('raw ~ ?', "\\\\begin\\{align").each{|p| p.raw=p.raw.gsub(/(begin|end)\{align.*?\}/, "\\1{aligned}"); p.save; print "."; $stdout.flush;}; puts "";
# replace {array} with {matrix} and remove all starred versions
Post.where('raw ~ ?', "\\\\begin\\{array").each{|p| p.raw=p.raw.gsub(/(begin|end)\{array\*?\}/, "\\1{matrix}"); p.save; print "."; $stdout.flush;}; puts "";
# replace `\begin{equation}` with `$$`
Post.where('raw ~ ?', "\\\\begin\\{equation").each{|p| p.raw=p.raw.gsub(/\s*\\(begin|end)\{equation\*?\}\s*/, "$$"); p.save; print "."; $stdout.flush;}; puts "";
# add new lines before and after `$$`
Post.where('raw ~ ?', "\\$\\$").each{|p| p.raw=p.raw.gsub(/\$\$/,"\n$$\n"); p.save; print "."; $stdout.flush;}; puts "";
# add `$$` before and after `{aligned}` blocks
Post.where('raw ~ ?', "\\\\begin\\{align").each{|p| p.raw=p.raw.gsub(/\s*(\\begin\{align)/m,"\n$$\n\\1").gsub(/(\\end\{align.*?\})\s*/m,"\\1\n$$\n"); p.save; print "."; $stdout.flush;}; puts "";
# ... fix double `$$` in `{aligned}`
Post.where('raw ~ ?', "\\\\begin\\{aligned").each{|p| p.raw=p.raw.gsub(/(\\end\{align.*?\}\n\$\$)\n\$\$/,"\\1").gsub(/\$\$\n(\$\$\n\\begin\{align)/,"\\1"); p.save; print "."; $stdout.flush;}; puts "";
## apply the above two commands to other environments you may have besides {aligned}
# MANUAL FIX RECOMMENDED FOR THE FOLLOWING:
# Posts where `$$` appear in quoted `> ` environment. Only the opening and
# closing `$$` must be prefixed, i.e. `> $$`, the content in between must not
# or else it's rendered as math greater-than signs.
# Run this to identify the posts:
Post.where('raw ~ ?', ">\\s*\\n\\s*\\$\\$").each{|p| puts "https://discourse.domain.com/p/#{p.id}";};
# ... OR, run this to try an automated fix, albeit risky
# (worked fine for us, a few posts needed further manual work):
Post.where('raw ~ ?', ">\\s*\\n\\s*\\$\\$").each{|p| p.raw=p.raw.gsub(/(>\s*)\$\$(.*?)\$\$\s*?\n/m,"\\1> $$\\2> $$\n>"); p.save; print "."; $stdout.flush;}; puts "";
# Cosmetic, optional: replace 3+ consecutive newlines with 2 newlines
Post.where('raw ~ ?', "\\n{3,}").each{|p| p.raw=p.raw.gsub(/\n{3,}/,"\n\n"); p.save; print "."; $stdout.flush;}; puts "";
# Optional: replace 2+ consecutive newlines before and after `$$`
# with single newline
# WARNING: fine in the current version of the plugin, but it may display
# differently in a future version (?). E.g. In actual Latex, an empty line
# after $$ before text does makes a difference in vertical spacing.
Post.where('raw ~ ?', "\\n\\$\\$\\n{2,}|\\n{2,}\\$\\$").each{|p| p.raw=p.raw.gsub(/\n\$\$\n{2,}/,"\n$$\n").gsub(/\n{2,}\$\$/,"\n$$"); p.save; print "."; $stdout.flush;}; puts "";