Skip to content

Instantly share code, notes, and snippets.

@dotnetchris
Created September 30, 2014 16:05
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dotnetchris/0f68c879082343295503 to your computer and use it in GitHub Desktop.
Save dotnetchris/0f68c879082343295503 to your computer and use it in GitHub Desktop.
PageDown Headings that support H1-H4
//replace this method
commandProto.doHeading = function (chunk, postProcessing) {
// Remove leading/trailing whitespace and reduce internal spaces to single spaces.
chunk.selection = chunk.selection.replace(/\s+/g, " ");
chunk.selection = chunk.selection.replace(/(^\s+|\s+$)/g, "");
// 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.startTag = "#### "; /* rewrite to start at H4 instead */
chunk.selection = this.getString("headingexample");
chunk.endTag = "\n";
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 = "";
// Try to get the current header level by looking for - and = in the line
// below the selection.
chunk.findTags(null, /\s?(-+|=+)/);
if (/=+/.test(chunk.endTag)) {
headerLevel = 1;
}
if (/-+/.test(chunk.endTag)) {
headerLevel = 2;
}
/* not using ---- or ==== so no need to skip a line
// Skip to the next line so we can create the header markdown.
chunk.startTag = chunk.endTag = "";
chunk.skipLines(1, 1);
*/
// We make a level 2 header if there is no current header.
// If there is a header level, we substract one from the header level.
// If it's already a level 1 header, it's removed.
var headerLevelToCreate = headerLevel == 0 ? 4 : headerLevel - 1; /* rewrite to start at H4 instead */
/* old
if (headerLevelToCreate > 0) {
// The button only creates level 1 and 2 underline headers.
// Why not have it iterate over hash header levels? Wouldn't that be easier and cleaner?
var headerChar = headerLevelToCreate >= 2 ? "-" : "=";
var len = chunk.selection.length;
if (len > SETTINGS.lineLength) {
len = SETTINGS.lineLength;
}
chunk.endTag = "\n";
while (len--) {
chunk.endTag += headerChar;
}
}
*/
if (headerLevelToCreate > 0) {
var hashesToCreate = headerLevelToCreate;
while (hashesToCreate--) {
chunk.startTag += "#";
}
chunk.startTag += " "; //So we have #### Header instead of ####Header (optional)
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment