Search code examples
markdownwmdwmd-editor

wmd markdown code problem


i m using wmd markdown editor in my project and i have a problem with code tags: if i enter a code snippet , markdown does not convert to html correctly it converts in "<p>" tags but if i enter some text else first and then code snippet it converts correctly in "<code>" tags is this a bug of wmd markdown editor? and how can i solve this problem?


Solution

  • I was actually working on this for my fixed version of WMD edit. Using regex you can quickly lop off the leading and trailing <p> tags which are most notably the causers of a lot of problems:

    html = html.replace(/^<p>/g, '').replace(/<\/p>$/g, '');
    

    To enforce this in wmd..

    (I'm asuming you're using the SO fork of wmd editor) Find this part of the code and change it as follows:

    var convertToHtml = function(){
    
        if (wmd.showdown) {
            var markdownConverter = new wmd.showdown.converter();
        }
        var text = inputBox.value;
    
        var callback = function(){
            inputBox.value = text;
        };
    
        if (!/markdown/.test(wmd.wmd_env.output.toLowerCase())) {
            if (markdownConverter) {
                inputBox.value = markdownConverter.makeHtml(text);
    
                // Add this line here:
                inputBox.value= inputBox.value.replace(/^<p>/g, '').replace(/<\/p>$/g, '');
    
                top.setTimeout(callback, 0);
                }
            }
        return true;
    };
    

    Untested, but you should get the idea.