Skip to content

Instantly share code, notes, and snippets.

@jerone
Created December 7, 2014 14:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jerone/85ea9f9a4dfa3496a963 to your computer and use it in GitHub Desktop.
Save jerone/85ea9f9a4dfa3496a963 to your computer and use it in GitHub Desktop.
Override markdown button to support code blocks
//$.fn.markdown.defaults.buttons[0][2].data[2]
var cmdBold, cmdItalic, cmdCode;
$.fn.markdown.defaults.buttons[0].forEach(function(group) {
group.data.forEach(function(button) {
if (button.name === 'cmdBold') cmdBold = button;
else if (button.name === 'cmdItalic') cmdItalic = button;
else if (button.name === 'cmdCode') cmdCode = button;
});
});
// Override `cmdCode` button;
if (cmdCode) {
cmdCode.callback = function(e) {
var cursor,
selected = e.getSelection(),
content = e.getContent(),
chunk = selected.length === 0 ? e.__localize('code text here') : selected.text;
if (content.substr(selected.start - 4, 4) === '```\n'
&& content.substr(selected.end, 4) === '\n```') {
e.setSelection(selected.start - 4, selected.end + 4);
e.replaceSelection(chunk);
cursor = selected.start - 4;
} else if (content.substr(selected.start - 1, 1) === '`'
&& content.substr(selected.end, 1) === '`') {
e.setSelection(selected.start - 1, selected.end + 1);
e.replaceSelection(chunk);
cursor = selected.start - 1;
} else if (content.indexOf('\n') > -1) {
e.replaceSelection('```\n' + chunk + '\n```');
cursor = selected.start + 4;
} else {
e.replaceSelection('`' + chunk + '`');
cursor = selected.start + 1;
}
e.setSelection(cursor, cursor + chunk.length);
};
}
$('[data-provide="markdown"]').markdown({
additionalButtons: [
[
{
name: "groupFont2",
data: [
cmdBold
]
}
]
],
reorderButtonGroups: [
'groupFont',
'groupLink',
'groupMisc',
/*{
'groupMisc': [
'cmdList',
'cmdListO',
'cmdCodeMultiLine',
'cmdQuote'
]
},*/
'groupUtil'
]
});
@jerone
Copy link
Author

jerone commented Dec 7, 2014

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment