Last active
September 3, 2020 03:29
-
-
Save dannymcgee/8cea783e9f974e319827d6efebc7194a to your computer and use it in GitHub Desktop.
Extra GitHub syntax highlighting
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name Extra GitHub syntax highlighting | |
// @namespace http://tampermonkey.net/ | |
// @version 1.0 | |
// @description Add extra classes to TypeScript/JavaScript tokens for more granular syntax highlighting. Combine with something like StyleBot for fun and profit. | |
// @author https://github.com/dannymcgee | |
// @include /^https?:\/\/(.*)?\.github\.com\/?.*$/ | |
// @require https://cdn.jsdelivr.net/g/lodash@4(lodash.min.js+lodash.fp.min.js) | |
// @grant none | |
// ==/UserScript== | |
(function() { | |
'use strict'; | |
const { flow, partition } = _; | |
// Closures | |
const test = (regexp) => ((elem) => regexp.test(elem.textContent.trim())); | |
const addClass = (className) => (([matchingElements, remaining]) => { | |
matchingElements.forEach((elem) => elem.classList.add(className)); | |
return remaining; | |
}); | |
// Predicates | |
const isOperator = test(/^[-+*/=.,;<>]|(>=|<=|=>|==|===|&&|\|\|)$/); | |
const isNum = test(/^\d+$/); | |
const isConst = test(/^(true|false|null|undefined)$/); | |
const isThis = test(/^this$/); | |
const isPrim = test(/^(string|number)$/); | |
// Wait for DOMContentReady | |
if (document.readyState != 'loading') { | |
highlight(); | |
} else { | |
document.addEventListener('DOMContentLoaded', highlight); | |
} | |
// Do the thing! | |
function highlight() { | |
flow( | |
partition(isOperator), addClass('pl-operator'), | |
partition(isNum), addClass('pl-number'), | |
partition(isConst), addClass('pl-const'), | |
)( | |
document.querySelectorAll('.pl-c1') | |
); | |
flow( | |
partition(isThis), addClass('pl-this'), | |
partition(isPrim), addClass('pl-prim'), | |
)( | |
document.querySelectorAll('.pl-smi') | |
); | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment