Open GoogleCodeExporter opened 8 years ago
-- function to toggle headers from h1 to h6 + create + remove-------
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;
}
};
Original comment by uli.h...@gmail.com
on 10 Oct 2009 at 4:34
Original issue reported on code.google.com by
theh...@gmail.com
on 16 Jul 2009 at 2:40