Skip to content

Instantly share code, notes, and snippets.

@thomaslevesque
Last active April 14, 2021 00:33
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save thomaslevesque/b8be05b7c08f50ce3e8e to your computer and use it in GitHub Desktop.
Keyboard shortcuts for GitHub markdown editor
// ==UserScript==
// @name GitHub Markdown shortcuts
// @namespace ThomasLevesque
// @include https://github.com/*
// @include https://gist.github.com/*
// @version 1
// @grant none
// ==/UserScript==
var shortcuts = [
{ keyCode: 75, command: "code" }, // Ctrl+K
{ keyCode: 76, command: "link" }, // Ctrl+L
];
function findToolbarForTextArea(textArea) {
var div = textArea.parents(".previewable-comment-form").first();
return div.find(".toolbar-commenting").first();
}
function findToolbarButtonForTextArea(textArea, buttonName) {
var toolbar = findToolbarForTextArea(textArea);
return toolbar.find("button[data-ga-click='Markdown Toolbar, click, " + buttonName + "']").first();
}
function clickToolbarButton(textArea, buttonName) {
var button = findToolbarButtonForTextArea(textArea, buttonName);
button.click();
}
function processShortcut(textArea, e) {
if (e.ctrlKey) {
for each (var s in shortcuts) {
if (e.keyCode == s.keyCode) {
clickToolbarButton(textArea, s.command);
e.stopImmediatePropagation();
e.preventDefault();
break;
}
}
}
}
function setupShortcuts() {
$(".comment-form-textarea").unbind();
$(".comment-form-textarea").keydown(function(e) {
processShortcut($(this), e);
});
}
function handleSetupShortcut(e)
{
if (e.ctrlKey && e.altKey && e.keyCode == 83) {
setupShortcuts();
e.stopImmediatePropagation();
e.preventDefault();
}
}
$(document).ready(function(){
$(document).keydown(handleSetupShortcut);
setTimeout(setupShortcuts, 1000);
});
@darkwater4213
Copy link

By "shortcuts" do you mean things like Ctrl+i for _ _ and Ctrl+b for ** **, or something else?

@thomaslevesque
Copy link
Author

@darkwater4213 yes, but these shortcuts are for "code" (ctrl+k) and "link" (ctrl+l)
To be honest, I'm not sure this still works...

@darkwater4213
Copy link

Okay, then. Thanks for responding anyways!

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