Masking fenced code, math and inline code in three passes let the math pattern match $ signs inside inline code, and the inline code pass then wrapped that placeholder in a new one, so a placeholder leaked into the stored answer. Match all three in one left-to-right pass and skip the repair when the text already has a NUL.