Question

I've decided that I've fallen in love with the Markdown editor on Stack Overflow. It's a fork from showdown.js from John Fraser. I want to use this parser on a project of mine, but after analyzing the source, I found that it was a bit messy for my taste.

So I set about modifying the JavaScript code to meet my needs, namely:

  1. Getting rid of all the global variables,
  2. Combining the variable declarations to a single var per scope,
  3. Changing concatenation to array joins,
  4. various other tweaks intended to make the minified source smaller.

I've got everything working beautifully, except for one small problem: The autocomplete code in the command.doList function is wonky in Chrome. I've tested everything to work properly in Internet Explorer, FireFox, and Safari. I've isolated the matter down to the following lines:

// Get the item prefix - e.g. " 1. " for a numbered list, " - " for a bulleted
// list.
getItemPrefix = function () {
    var prefix;
    if (isNumberedList) {
        // the `s` variable is just a string space.
        prefix = [s, num, '. '].join('');
        num++;
    } else {
        prefix = [s, bullet, s].join('');
    }
    return prefix;
};

// Fixes the prefixes of the other list items.
getPrefixedItem = function (itemText) {
    // The numbering flag is unset when called by autoindent.
    if (isNumberedList === undefined) {
        isNumberedList = /^\s*\d/.test(itemText);
    }

    // Renumber/bullet the list element.
    // THE FOLLOWING LINES COMMENTED OUT TO FIX A BUG.
    //itemText = itemText.replace(/^[ ]{0,3}([\*\+\-]|\d+[.])\s/gm,
        // function () { return getItemPrefix(); });
    return itemText;
};

The two commented-out lines near the end stop Chrome from behaving wonky, but numbered lists no longer auto-increment. As soon as I un-comment the lines, all major browsers begin working normally, except for Chrome. (Chrome goes crazy and auto-completes the list with extra numbering and bullets, also happens when trying to use an unordered list.)

The problem obviously doesn't exist here on Stack Overflow, so I'm hoping it's just a stupid oversight on my part. (Either that or Stack Overflow has updated and fixed the bug.)

Was it helpful?

Solution

You can look at the fix for another WMD fork (line 32).

OTHER TIPS

You shouldn't need to backslash the *+- in square brackets, should you? That might be what Chrome is having problems with.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top