GitHub: fix Asciidoc rendering

Fix Asciidoc rendering on GitHub: add standard Asciidoc icons to NOTE/TIP/etc., highlight block titles, fix TOC.

  1. // ==UserScript==
  2. // @name GitHub: fix Asciidoc rendering
  3. // @namespace https://github.com/powerman/userjs-github-asciidoc
  4. // @description Fix Asciidoc rendering on GitHub: add standard Asciidoc icons to NOTE/TIP/etc., highlight block titles, fix TOC.
  5. // @include /^https://github.com/[^/]+/[^/]+([#?].*)?$/
  6. // @include /^https://github.com/[^/]+/[^/]+/blob/.*\.(asciidoc|adoc|asc)([#?].*)?$/
  7. // @include /^https://github.com/[^/]+/[^/]+/wiki.*$/
  8. // @include /^https://gist.github.com/[^/]+/.*$/
  9. // @version 1.7
  10. // @grant none
  11. // @require https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js
  12. // ==/UserScript==
  13.  
  14. (function(window,$){
  15. 'use strict';
  16.  
  17. var icons = 'https://raw.githubusercontent.com/powerman/asciidoc-cheatsheet/master/images/icons/';
  18. var handler = function(){
  19.  
  20. if(window.location.host==='gist.github.com'){
  21. var filename = $('strong.file-name').html();
  22. if(filename===undefined || !filename.match('\\.(asciidoc|adoc|asc)$')){
  23. return;
  24. }
  25. }
  26.  
  27. // Replace text with icons for NOTE/TIP/etc.
  28. $('tbody > tr > td:first-child > div')
  29. .filter(function(){ return this.innerHTML==='Note'; })
  30. .html('<img src="'+icons+'note.png">');
  31. $('tbody > tr > td:first-child > div')
  32. .filter(function(){ return this.innerHTML==='Tip'; })
  33. .html('<img src="'+icons+'tip.png">');
  34. $('tbody > tr > td:first-child > div')
  35. .filter(function(){ return this.innerHTML==='Important'; })
  36. .html('<img src="'+icons+'important.png">');
  37. $('tbody > tr > td:first-child > div')
  38. .filter(function(){ return this.innerHTML==='Warning'; })
  39. .html('<img src="'+icons+'warning.png">');
  40. $('tbody > tr > td:first-child > div')
  41. .filter(function(){ return this.innerHTML==='Caution'; })
  42. .html('<img src="'+icons+'caution.png">');
  43. // Remove border around NOTE/TIP/etc and fix it width.
  44. $('tbody:has(> tr > td:first-child > div > img)').find('tr, td').css({'border':'none'});
  45. $('tbody > tr > td:first-child > div > img').css({'max-width':'none'});
  46.  
  47. // Make block titles bold
  48. $('.markdown-body div > div:first-child + *').prev().filter(':not(:has(*))').css({'font-weight':'bold'});
  49. $('.markdown-body td > div:first-child').filter(':not(:has(*))').css({'font-weight':'bold'});
  50. };
  51.  
  52. window.addEventListener('load', handler, false);
  53. $(document).on('pjax:end', handler);
  54.  
  55. })(window,$);