Unity Docs Syntax Hightligher Fork

Adds syntax highlighting to the Unity Documentation. This is a fork from Hekky.

Από την 08/10/2023. Δείτε την τελευταία έκδοση.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey, το Greasemonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Userscripts για να εγκαταστήσετε αυτόν τον κώδικα.

You will need to install an extension such as Tampermonkey to install this script.

Θα χρειαστεί να εγκαταστήσετε μια επέκταση διαχείρισης κώδικα χρήστη για να εγκαταστήσετε αυτόν τον κώδικα.

(Έχω ήδη έναν διαχειριστή κώδικα χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(Έχω ήδη έναν διαχειριστή στυλ χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

    // ==UserScript==
    // @name        Unity Docs Syntax Hightligher Fork
    // @namespace   Violentmonkey Scripts
    // @version     1.0
    // @author      Maoyeedy
    // @license     MIT
    // @description Adds syntax highlighting to the Unity Documentation. This is a fork from Hekky.
    //
    // @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/[email protected]/prism.js
    // @require     https://unpkg.com/[email protected]/components/prism-c.min.js
    // @require     https://unpkg.com/[email protected]/components/prism-clike.min.js
    // @require     https://unpkg.com/[email protected]/components/prism-csharp.min.js
    // @require     https://unpkg.com/[email protected]/components/prism-hlsl.min.js
    // @resource    PRISM_THEME https://unpkg.com/[email protected]/themes/prism-twilight.min.css

    //
    // ==/UserScript==

    (function() {
        'use strict';
        GM_addStyle(GM_getResourceText("PRISM_THEME"));
        //https://unpkg.com/[email protected]/themes/prism-tomorrow.min.css
        //https://unpkg.com/[email protected]/themes/prism-twilight.min.css
        //https://unpkg.com/[email protected]/themes/prism.min.css

        // Inject custom CSS to override font-family with !important
        const customCSS = `
            code {
                font-family: 'Jetbrains Mono', monospace !important;
            }
        `;

        const styleElement = document.createElement('style');
        styleElement.type = 'text/css';
        styleElement.appendChild(document.createTextNode(customCSS));
        document.head.appendChild(styleElement);
    }());


    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');
            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>`;
            }
          });
        });
      });
    });