There is a problem with the edit preview. For example, edit this post and see this error:

  • The bug seems to be triggered by the presence of the $ character earlier on the line; replacing it with something else fixes it. Investigating further. Commented Aug 26, 2015 at 13:09

1 Answer 1


Looks like this bug is triggered by the presence of $ or ~ earlier on the line. In fact, I found the code in wmd.en.js that triggers it:

// attacklab: Replace ~ with ~T
// This lets us use tilde as an escape char to avoid md5 hashes
// The choice of character is arbitray; anything that isn't
// magic in Markdown will work.
text = text.replace(/~/g, "~T");

// attacklab: Replace $ with ~D
// RegExp interprets $ as a special character
// when it's in a replacement string
text = text.replace(/\$/g, "~D");

// ...SNIP...

// Convert all tabs to spaces.
text = _Detab(text);

// ...SNIP...

text = _RunBlockGamut(text);

text = _UnescapeSpecialChars(text);

// attacklab: Restore dollar signs
text = text.replace(/~D/g, "$$");

// attacklab: Restore tildes
text = text.replace(/~T/g, "~");

(Ed. note: some irrelevant code replaced by // ...SNIP... markers for brevity.)

The _Detab() function (which is also called indirectly by _RunBlockGamut() via _DoCodeBlocks(); I'm not sure why this is done twice) isn't aware of the fact that ~T and ~D should be counted as single characters, so it's miscalculating the length of any strings that contain those escape sequences, and thus getting the tab stops wrong for lines that contain them.

Presumably, the proper fix would be to make _Detab() aware of the tilde-escaping convention, so that it can count such escape sequences correctly.

In fact, here's a tentative (untested) patch to fix this:

--- wmd.en.js   2015-08-26 16:32:34.930482439 +0300
+++ wmd.en.new.js   2015-08-26 16:42:00.030476026 +0300
@@ -1586,11 +1586,16 @@
             skew = 0,

-            return text.replace(/[\n\t]/g, function (match, offset) {
+            return text.replace(/[\n\t~]/g, function (match, offset) {
                 if (match === "\n") {
                     skew = offset + 1;
                     return match;
+                // count ~T and ~D as only one character each: http://meta.stackexchange.com/questions/260882/error-in-edit-preview-functionality-when-using-tabs-in-a-code-block
+                if (match === "~") {
+                    skew++;
+                    return match;
+                }
                 v = (offset - skew) % 4;
                 skew = offset + 1;
                 return spaces[v];

(The patch is generated against the combined wmd.en.js file, but it should apply against the internal Markdown.Converter.js with a slight offset.)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .