Skip to content

Commit 4e95d6f

Browse files
committed
✨ Added extra button with complete list of all code syntax. Fixes jerone#18
1 parent 5b3204b commit 4e95d6f

1 file changed

Lines changed: 112 additions & 3 deletions

File tree

Github_Comment_Enhancer/Github_Comment_Enhancer.user.js

Lines changed: 112 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,13 @@
159159
},
160160
shortcut: "ctrl+k"
161161
},
162+
"function-code-syntax": {
163+
exec: function(button, selText, commentForm, next) {
164+
var rt = "$1\n```" + button.dataset.value + "\n$2\n```$3";
165+
next(selText.replace(/^(\s*)([\s\S]*?)(\s*)$/g, rt));
166+
}
167+
},
168+
162169
"function-blockquote": {
163170
search: /(.+)([\n]?)/g,
164171
replace: "> $1$2",
@@ -286,9 +293,31 @@
286293
' </div>' +
287294

288295
' <div class="button-group btn-group">' +
289-
' <a href="#" id="function-code" class="btn btn-sm minibutton function-button tooltipped tooltipped-ne" aria-label="Code (ctrl+k)">' +
290-
' <span class="octicon octicon-code"></span>' +
291-
' </a>' +
296+
' <div class="select-menu js-menu-container js-select-menu tooltipped tooltipped-ne" aria-label="Code (ctrl+k)">' +
297+
' <a href="#" id="function-code" class="btn btn-sm minibutton function-button">' +
298+
' <span class="octicon octicon-code"></span>' +
299+
' </a>' +
300+
' <div class="select-menu-modal-holder js-menu-content js-navigation-container js-active-navigation-container" style="top:26px; z-index:22;">' +
301+
' <div class="select-menu-modal" style="overflow:visible;">' +
302+
' <div class="select-menu-header">' +
303+
' <span class="select-menu-title">Code syntax</span>' +
304+
' <span class="octicon octicon-remove-close js-menu-close"></span>' +
305+
' </div>' +
306+
' <div class="select-menu-filters">' +
307+
' <div class="select-menu-text-filter">' +
308+
' <input type="text" placeholder="Filter code syntax..." class="js-filterable-field js-navigation-enable" id="context-code-syntax-filter-field">' +
309+
' </div>' +
310+
' </div>' +
311+
' <div class="code-syntaxes select-menu-list" style="overflow:visible;">' +
312+
' <div class="select-menu-no-results">Nothing to show</div>' +
313+
' </div>' +
314+
' </div>' +
315+
' </div>' +
316+
' <span class="btn btn-sm minibutton select-menu-button js-menu-target" style="width:20px; margin-left:-1px;"></span>' +
317+
' </div>' +
318+
' </div>' +
319+
320+
' <div class="button-group btn-group">' +
292321
' <a href="#" id="function-blockquote" class="btn btn-sm minibutton function-button tooltipped tooltipped-ne" aria-label="Blockquote (ctrl+q)">' +
293322
' <span class="octicon octicon-quote"></span>' +
294323
' </a>' +
@@ -482,6 +511,84 @@
482511
return false;
483512
};
484513

514+
var codeSyntaxTop = ["JavaScript", "Java", "Ruby", "PHP", "Python", "CSS", "C++", "C#", "C", "HTML"]; // https://github.com/blog/2047-language-trends-on-github
515+
var codeSyntaxList = ["ABAP","abl","aconf","ActionScript","actionscript 3","actionscript3","Ada","ada2005","ada95",
516+
"advpl","Agda","ags","AGS Script","ahk","Alloy","AMPL","Ant Build System","ANTLR","apache","ApacheConf","Apex",
517+
"API Blueprint","APL","AppleScript","Arc","Arduino","as3","AsciiDoc","ASP","AspectJ","aspx","aspx-vb","Assembly",
518+
"ATS","ats2","au3","Augeas","AutoHotkey","AutoIt","AutoIt3","AutoItScript","Awk","b3d","bash","bash session","bat",
519+
"batch","Batchfile","Befunge","Bison","BitBake","blitz3d","BlitzBasic","BlitzMax","blitzplus","Bluespec","bmax",
520+
"Boo","bplus","Brainfuck","Brightscript","Bro","bsdmake","byond","C","C#","C++","c++-objdumb","C-ObjDump","c2hs",
521+
"C2hs Haskell","Cap'n Proto","Carto","CartoCSS","Ceylon","cfc","cfm","cfml","Chapel","Charity","chpl","ChucK",
522+
"Cirru","Clarion","Clean","clipper","CLIPS","Clojure","CMake","COBOL","coffee","coffee-script","CoffeeScript",
523+
"ColdFusion","ColdFusion CFC","coldfusion html","Common Lisp","Component Pascal","console","Cool","Coq","cpp",
524+
"Cpp-ObjDump","Creole","Crystal","csharp","CSS","Cucumber","Cuda","Cycript","Cython","D","D-ObjDump","Darcs Patch",
525+
"Dart","dcl","delphi","desktop","Diff","DIGITAL Command Language","DM","DNS Zone","Dockerfile","Dogescript",
526+
"dosbatch","dosini","dpatch","DTrace","dtrace-script","Dylan","E","Eagle","eC","Ecere Projects","ECL","ECLiPSe",
527+
"edn","Eiffel","elisp","Elixir","Elm","emacs","Emacs Lisp","EmberScript","erb","Erlang","F#","Factor","Fancy",
528+
"Fantom","Filterscript","fish","flex","FLUX","Formatted","Forth","FORTRAN","foxpro","Frege","fsharp","fundamental",
529+
"G-code","Game Maker Language","GAMS","GAP","GAS","GDScript","Genshi","Gentoo Ebuild","Gentoo Eclass",
530+
"Gettext Catalog","gf","gherkin","GLSL","Glyph","Gnuplot","Go","Golo","Gosu","Grace","Gradle",
531+
"Grammatical Framework","Graph Modeling Language","Graphviz (DOT)","Groff","Groovy","Groovy Server Pages","gsp",
532+
"Hack","Haml","Handlebars","Harbour","Haskell","Haxe","hbs","HCL","HTML","HTML+Django","html+django/jinja",
533+
"HTML+ERB","html+jinja","HTML+PHP","html+ruby","htmlbars","htmldjango","HTTP","Hy","hylang","HyPhy","i7","IDL",
534+
"Idris","igor","IGOR Pro","igorpro","inc","Inform 7","inform7","INI","Inno Setup","Io","Ioke","irc","IRC log",
535+
"irc logs","Isabelle","Isabelle ROOT","J","Jade","Jasmin","Java","java server page","Java Server Pages",
536+
"JavaScript","JFlex","jruby","js","JSON","JSON5","JSONiq","JSONLD","jsp","JSX","Julia","KiCad","Kit","Kotlin",
537+
"KRL","LabVIEW","Lasso","lassoscript","latex","Latte","Lean","Less","Lex","LFE","lhaskell","lhs","LilyPond",
538+
"Limbo","Linker Script","Linux Kernel Module","Liquid","lisp","litcoffee","Literate Agda","Literate CoffeeScript",
539+
"Literate Haskell","live-script","LiveScript","LLVM","Logos","Logtalk","LOLCODE","LookML","LoomScript","ls","LSL",
540+
"Lua","M","macruby","make","Makefile","Mako","Markdown","Mask","Mathematica","Matlab","Maven POM","Max","max/msp",
541+
"maxmsp","MediaWiki","Mercury","mf","MiniD","Mirah","mma","Modelica","Modula-2","Module Management System",
542+
"Monkey","Moocode","MoonScript","MTML","MUF","mumps","mupad","Myghty","nasm","NCL","Nemerle","nesC","NetLinx",
543+
"NetLinx+ERB","NetLogo","NewLisp","Nginx","nginx configuration file","Nimrod","Ninja","Nit","Nix","nixos","NL",
544+
"node","nroff","NSIS","Nu","NumPy","nush","nvim","obj-c","obj-c++","obj-j","objc","objc++","ObjDump","Objective-C",
545+
"Objective-C++","Objective-J","objectivec","objectivec++","objectivej","objectpascal","objj","OCaml","Omgrofl",
546+
"ooc","Opa","Opal","OpenCL","openedge","OpenEdge ABL","OpenSCAD","Org","osascript","Ox","Oxygene","Oz","Pan",
547+
"Papyrus","Parrot","Parrot Assembly","Parrot Internal Representation","Pascal","pasm","PAWN","Perl","Perl6","PHP",
548+
"PicoLisp","PigLatin","Pike","pir","PLpgSQL","PLSQL","Pod","PogoScript","posh","postscr","PostScript","pot",
549+
"PowerShell","Processing","progress","Prolog","Propeller Spin","protobuf","Protocol Buffer","Protocol Buffers",
550+
"Public Key","Puppet","Pure Data","PureBasic","PureScript","pyrex","Python","Python traceback","QMake","QML","R",
551+
"Racket","Ragel in Ruby Host","ragel-rb","ragel-ruby","rake","RAML","raw","Raw token data","rb","rbx","RDoc",
552+
"REALbasic","Rebol","Red","red/system","Redcode","RenderScript","reStructuredText","RHTML","RMarkdown",
553+
"RobotFramework","Rouge","Rscript","rss","rst","Ruby","Rust","rusthon","Sage","salt","SaltStack","saltstate",
554+
"SAS","Sass","Scala","Scaml","Scheme","Scilab","SCSS","Self","sh","Shell","ShellSession","Shen","Slash","Slim",
555+
"Smali","Smalltalk","Smarty","sml","SMT","sourcemod","SourcePawn","SPARQL","splus","SQF","SQL","SQLPL","squeak",
556+
"Squirrel","Standard ML","Stata","STON","Stylus","SuperCollider","SVG","Swift","SystemVerilog","Tcl","Tcsh","Tea",
557+
"TeX","Text","Textile","Thrift","TOML","ts","Turing","Turtle","Twig","TXL","TypeScript","udiff",
558+
"Unified Parallel C","Unity3D Asset","UnrealScript","Vala","vb.net","vbnet","VCL","Verilog","VHDL","vim","VimL",
559+
"Visual Basic","Volt","Vue","Web Ontology Language","WebIDL","winbatch","wisp","wsdl","X10","xBase","XC","xhtml",
560+
"XML","xml+genshi","xml+kid","Xojo","XPages","XProc","XQuery","XS","xsd","xsl","XSLT","xten","Xtend","Yacc","YAML",
561+
"yml","Zephir","Zimpl","zsh"]; // https://github.com/jerone/UserScripts/issues/18
562+
var codeSyntaxes = [].concat(codeSyntaxTop, codeSyntaxList).filter(function (a, b, c) { return c.indexOf(a) === b; });
563+
564+
function addCodeSyntax(commentForm) {
565+
var syntaxSuggestions = document.createElement("div");
566+
syntaxSuggestions.dataset.filterableType = "substring";
567+
syntaxSuggestions.dataset.filterableFor = "context-code-syntax-filter-field";
568+
syntaxSuggestions.dataset.filterableLimit = codeSyntaxTop.length; // Show top code syntaxes on open;
569+
570+
codeSyntaxes.forEach(function(syntax){
571+
var syntaxSuggestion = document.createElement("a");
572+
syntaxSuggestion.setAttribute("href", "#");
573+
syntaxSuggestion.classList.add("function-button", "select-menu-item", "js-navigation-item");
574+
syntaxSuggestion.dataset.value = syntax;
575+
syntaxSuggestions.appendChild(syntaxSuggestion);
576+
syntaxSuggestion.addEventListener("click", function(e) {
577+
e.preventDefault();
578+
executeAction(MarkDown["function-code-syntax"], commentForm, this);
579+
return false;
580+
});
581+
582+
var syntaxSuggestionText = document.createElement("span");
583+
syntaxSuggestionText.classList.add("select-menu-item-text", "js-select-button-text");
584+
syntaxSuggestionText.appendChild(document.createTextNode(syntax));
585+
syntaxSuggestion.appendChild(syntaxSuggestionText);
586+
});
587+
588+
var suggester = commentForm.parentNode.parentNode.querySelector(".code-syntaxes");
589+
suggester.appendChild(syntaxSuggestions);
590+
}
591+
485592
var suggestionsCache = {};
486593

487594
function addSuggestions(commentForm) {
@@ -603,6 +710,8 @@
603710

604711
addSuggestions(commentForm);
605712

713+
addCodeSyntax(commentForm);
714+
606715
var tabnavExtras = commentForm.parentNode.parentNode.querySelector(".comment-form-head .tabnav-right, .comment-form-head .right");
607716
if (tabnavExtras) {
608717
var elem = commentForm;

0 commit comments

Comments
 (0)