Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

toggle Headings #1

Closed
leotiger opened this issue Oct 10, 2009 · 0 comments
Closed

toggle Headings #1

leotiger opened this issue Oct 10, 2009 · 0 comments

Comments

@leotiger
Copy link

command.doHeading = function(chunk, postProcessing, useDefaultText){

    //chunk.trimWhitespace();
    // Remove leading/trailing whitespace and reduce internal spaces to single spaces.
    var chunkOrigSel = chunk.selection;
    chunk.selection = chunk.selection.replace(/\s+/g, " ");
    chunk.selection = chunk.selection.replace(/(^\s+|\s+$)/g, "");
    var firstChr = chunk.selection.substring(0, 1);
    // If we clicked the button with no selected text, we just
    // make a level 2 hash header around some default text.
    if(!chunk.selection){
        chunk.findTags(/[^\n]/, /[^\n]/);       
        if(/[^\n]/.test(chunk.startTag))
            chunk.startTag += "\n\n## ";
        else chunk.startTag = "## ";
        chunk.selection = "Heading";
        if(/[^\n]/.test(chunk.startTag))
            chunk.endTag = " ##\n\n" + chunk.endTag;
        else chunk.endTag = " ##";
        return;
    }

    var headerLevel = 0;        // The existing header level of the selected text.

    // Remove any existing hash heading markdown and save the header level.
    chunk.findTags(/#+[ ]*/, /[ ]*#+/);
    if(/#+/.test(chunk.startTag)){
        headerLevel = re.lastMatch.length;
    }
    chunk.startTag = chunk.endTag = "";

    var addNewLine = "";
    if (headerLevel == 0)
    {
        chunk.findTags(/[^\n]/, null);      
        if(/[^\n]/.test(chunk.startTag)){
            addNewLine = chunk.startTag.substring(0,1);
        }
        chunk.startTag = chunk.endTag = "";
    }

    var removeNewLines = false;
    if (headerLevel < 6) headerLevel++;
    else if (headerLevel == 6) 
    {
        headerLevel = 0;
        removeNewLines = true;
    }

    var headerChar = "#";
    var len = headerLevel;
    if (headerLevel > 0)
    {
        if (headerLevel == 1 && addNewLine != "") 
        {
            chunk.startTag = addNewLine.replace(/\s+/g, "") + "\n\n";
        }
        chunk.endTag = " ";
        while(len--){
            chunk.endTag += headerChar;
            chunk.startTag += headerChar;
        }
        chunk.startTag += " ";

        if (headerLevel == 1) 
        {
            chunk.endTag += "\n\n";
            chunk.selection = firstChr + chunk.selection;
        }

    }
    else
    {
        chunk.findTags(/[\n]+/, /[\n]+/);       
        if(/[\n]+/.test(chunk.startTag)){
            chunk.startTag = "";
        }
        if(/[\n]+/.test(chunk.endTag)){
            chunk.endTag = "";
        }
        chunk.selection = chunkOrigSel;     
        chunk.findTags(/[^\n]/, /[^\n]/);       
        if(/[^\n]/.test(chunk.startTag)){
            chunk.startTag = chunk.startTag.substring(0,1) + " ";
            chunk.startTag = chunk.startTag.replace(/\s+/g, " ");
        }
        if(/[^\n]/.test(chunk.endTag)){
            chunk.endTag = " " + chunk.endTag.substring(0,1);
            chunk.endTag = chunk.endTag.replace(/\s+/g, " ");
        }
        chunk.selection = chunkOrigSel;         
    }

};  
@derobins derobins closed this as completed Oct 3, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants