Markdown -> Showdown bug in detab regex?

后端 未结 2 1851
深忆病人
深忆病人 2021-01-14 02:09

I\'m looking at Gruber\'s original Markdown implementation here and the Showdown implementation here.

I\'m comparing the _Detab function in each. I\'m

2条回答
  •  自闭症患者
    2021-01-14 03:12

    There are several bugs in Showdown's detabber. That's why for Stack Overflow's version, I have rewritten it:

    function _Detab(text) {
        if (!/\t/.test(text))
            return text;
    
        var spaces = ["    ", "   ", "  ", " "],
        skew = 0,
        v;
    
        return text.replace(/[\n\t]/g, function (match, offset) {
            if (match === "\n") {
                skew = offset + 1;
                return match;
            }
            v = (offset - skew) % 4;
            skew = offset + 1;
            return spaces[v];
        });
    }
    

    It detabs correctly, and if I recall my measurements correctly, this is about as fast (maybe a little slower) as the original in older IE versions, and much faster in newer browsers.

    See http://code.google.com/p/pagedown/wiki/PageDown for our full version of Showdown.

提交回复
热议问题