Skip to content

Instantly share code, notes, and snippets.

@hyblocker
Last active May 12, 2024 11:06
Show Gist options
  • Save hyblocker/d652e3d8775c42c5f63b08666a6c061c to your computer and use it in GitHub Desktop.
Save hyblocker/d652e3d8775c42c5f63b08666a6c061c to your computer and use it in GitHub Desktop.
Unity Docs Syntax Highlighting
// ==UserScript==
// @name Unity Docs Syntax Hightligher
// @namespace Violentmonkey Scripts
// @version 1.0
// @author Hekky
// @description 18/03/2023, 11:38:56
//
// @match https://docs.unity3d.com/Manual/*
// @match https://docs.unity3d.com/ScriptReference/*
// @match https://docs.unity3d.com/*/Manual/*
// @match https://docs.unity3d.com/*/ScriptReference/*
//
// @grant GM_getResourceText
// @grant GM_addStyle
//
// @require https://unpkg.com/prismjs@1.29.0/prism.js
//
// @require https://unpkg.com/prismjs@1.29.0/components/prism-c.min.js
// @require https://unpkg.com/prismjs@1.29.0/components/prism-clike.min.js
// @require https://unpkg.com/prismjs@1.29.0/components/prism-csharp.min.js
// @require https://unpkg.com/prismjs@1.29.0/components/prism-hlsl.min.js
//
// @require https://unpkg.com/prismjs@1.29.0/plugins/line-numbers/prism-line-numbers.min.js
// @require https://unpkg.com/prismjs@1.29.0/plugins/show-language/prism-show-language.min.js
//
// @resource PRISM_THEME https://unpkg.com/prismjs@1.29.0/themes/prism-tomorrow.min.css
//
// @resource LINE_NUMS_THEME https://unpkg.com/prismjs@1.29.0/plugins/line-numbers/prism-line-numbers.min.css
// ==/UserScript==
(function() {
'use strict';
GM_addStyle(GM_getResourceText("PRISM_THEME"));
GM_addStyle(GM_getResourceText("LINE_NUMS_THEME"));
}());
const CSHARP = 0;
const HLSL = 1;
var waitForGlobal = function(key, callback) {
if (window[key]) {
callback();
} else {
setTimeout(function() {
waitForGlobal(key, callback);
}, 100);
}
};
function waitForLangLoad(lang, callback) {
if (Prism.util.getLanguage(lang) != null) {
callback();
} else {
setTimeout(function() {
waitForLangLoad(lang, callback);
}, 100);
}
}
function detectCodeLanguage(elem) {
if (elem.classList.contains('codeExampleCS')) {
return CSHARP;
}
if (elem.innerHTML.match(/CGPROGRAM|ENDCG|CGINCLUDE|#pragma|SubShader \"/g) != null) {
return HLSL;
}
return CSHARP;
}
waitForGlobal("Prism", () => {
waitForLangLoad("csharp", () => {
waitForLangLoad("hlsl", () => {
document.querySelectorAll('.content-wrap pre').forEach((el) => {
el.innerHTML = el.innerHTML.replace(/\<br\>/g, '\n');
el.classList.add("line-numbers");
if (detectCodeLanguage(el) == CSHARP) {
el.classList.add("language-csharp");
} else {
el.classList.add("language-hlsl");
}
if (el.firstChild.nodeName != 'CODE') {
el.innerHTML = `<code>${el.innerHTML}</code>`;
}
});
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment