prism/plugins/show-language/prism-show-language.js

326 lines
7.7 KiB
JavaScript
Raw Normal View History

(function () {
if (typeof Prism === 'undefined' || typeof document === 'undefined') {
return;
}
Plugins: Toolbar & Copy to Clipboard (#891) * Add prism-toolbar plugin This plugin exposes a `registerButton` method, which other plugins can use to add buttons to the toolbar. Comes with styles. * Add demo file for toolbar plugin Registers a "Hello World!" tag with the toolbar. * Make `toolbar.registerButton` polymorphic This allows developers to provide either a callback or an object with a `text` string and an optional `onClick` function to create a new button. * Add Toolbar & Copy to Clipboard to components.js * Add Copy to Clipboard plugin * Switch `innerHTML` to `textContent` This ensures additional HTML can't be passed to the toolbar via the `text` property, ensuring a consistent display for the buttons. * Use `call` to bind `this` to the `onClick` method This provides access to the clicked element, which is what `this` is usually bound to on event listeners. * Add hover animation to toolbar * Add drop shadow to toolbar buttons * Add `clipboard` to `optionalDependencies` This will install Clipboard.js when installing from `npm`, but won't fail the build if the installation of Clipboard.js fails. * Load Clipboard.js from CDN if not present * Display plugin code using data-src * Recompile prism-toolbar * Update Show Languages to be a Toolbar button Show Languages now registers a callback with the toolbar plugin to return an element with the language in it. * Add basic HTML API & documentation The Toolbar will now be able to read a `data-label` attribute and add it to the code snippet. * Switch a -> button when only providing onClick Also adds a `url` property which creats an anchor tag and sets the href. Adds some styles to override the button defaults. * Add support for data-url to create anchor tag This allows the HTML API to create links in the Toolbar. * Update toolbar to allow order controlled via HTML Uses a data-attribute on the `body` tag to update the order, should the user choose to do so. * Allow template element to provide content to label This provides one of several options a user can implement in order to get a custom button. Also fixes some bugs in the documentation. * Fix bug when combined with the autoloader plugin The autoloader will rehighlight the element after the language arrives. This means the complete hook can run multiple times. Without a check, multiple toolbars can get added to an element.
2016-11-09 09:08:49 +08:00
if (!Prism.plugins.toolbar) {
console.warn('Show Languages plugin loaded before Toolbar plugin.');
return;
}
2021-04-18 04:26:40 +08:00
/* eslint-disable */
// The languages map is built automatically with gulp
var Languages = /*languages_placeholder[*/{
"none": "Plain text",
2021-03-06 06:23:17 +08:00
"plain": "Plain text",
"plaintext": "Plain text",
"text": "Plain text",
"txt": "Plain text",
"html": "HTML",
"xml": "XML",
"svg": "SVG",
"mathml": "MathML",
2020-04-28 04:43:30 +08:00
"ssml": "SSML",
"rss": "RSS",
"css": "CSS",
"clike": "C-like",
"js": "JavaScript",
"abap": "ABAP",
"abnf": "ABNF",
2020-04-28 04:22:12 +08:00
"al": "AL",
"antlr4": "ANTLR4",
"g4": "ANTLR4",
"apacheconf": "Apache Configuration",
"apl": "APL",
"aql": "AQL",
"ino": "Arduino",
"arff": "ARFF",
2022-03-21 22:25:14 +08:00
"armasm": "ARM Assembly",
"arm-asm": "ARM Assembly",
"art": "Arturo",
"asciidoc": "AsciiDoc",
"adoc": "AsciiDoc",
"aspnet": "ASP.NET (C#)",
"asm6502": "6502 Assembly",
"asmatmel": "Atmel AVR Assembly",
"autohotkey": "AutoHotkey",
"autoit": "AutoIt",
"avisynth": "AviSynth",
"avs": "AviSynth",
"avro-idl": "Avro IDL",
"avdl": "Avro IDL",
2022-03-15 05:43:46 +08:00
"awk": "AWK",
"gawk": "GAWK",
"sh": "Shell",
"basic": "BASIC",
"bbcode": "BBcode",
"bbj": "BBj",
"bnf": "BNF",
"rbnf": "RBNF",
2022-08-06 03:25:30 +08:00
"bqn": "BQN",
"bsl": "BSL (1C:Enterprise)",
"oscript": "OneScript",
"csharp": "C#",
"cs": "C#",
"dotnet": "C#",
"cpp": "C++",
2021-02-20 00:11:51 +08:00
"cfscript": "CFScript",
"cfc": "CFScript",
"cil": "CIL",
"cilkc": "Cilk/C",
"cilk-c": "Cilk/C",
"cilkcpp": "Cilk/C++",
"cilk-cpp": "Cilk/C++",
"cilk": "Cilk/C++",
"cmake": "CMake",
2021-03-21 23:50:31 +08:00
"cobol": "COBOL",
"coffee": "CoffeeScript",
"conc": "Concurnas",
"csp": "Content-Security-Policy",
"css-extras": "CSS Extras",
2021-03-21 23:41:33 +08:00
"csv": "CSV",
2022-03-21 22:20:08 +08:00
"cue": "CUE",
"dataweave": "DataWeave",
"dax": "DAX",
"django": "Django/Jinja2",
"jinja2": "Django/Jinja2",
"dns-zone-file": "DNS zone file",
"dns-zone": "DNS zone file",
"dockerfile": "Docker",
2021-01-17 21:29:35 +08:00
"dot": "DOT (Graphviz)",
"gv": "DOT (Graphviz)",
"ebnf": "EBNF",
2020-07-11 06:07:19 +08:00
"editorconfig": "EditorConfig",
"ejs": "EJS",
"etlua": "Embedded Lua templating",
"erb": "ERB",
"excel-formula": "Excel Formula",
"xlsx": "Excel Formula",
"xls": "Excel Formula",
"fsharp": "F#",
"firestore-security-rules": "Firestore security rules",
"ftl": "FreeMarker Template Language",
"gml": "GameMaker Language",
"gamemakerlanguage": "GameMaker Language",
2021-09-13 01:52:44 +08:00
"gap": "GAP (CAS)",
"gcode": "G-code",
"gdscript": "GDScript",
"gedcom": "GEDCOM",
"gettext": "gettext",
"po": "gettext",
"glsl": "GLSL",
2021-09-02 00:08:02 +08:00
"gn": "GN",
"gni": "GN",
"linker-script": "GNU Linker Script",
"ld": "GNU Linker Script",
"go-module": "Go module",
"go-mod": "Go module",
"graphql": "GraphQL",
2021-05-05 01:00:33 +08:00
"hbs": "Handlebars",
"hs": "Haskell",
"hcl": "HCL",
"hlsl": "HLSL",
"http": "HTTP",
"hpkp": "HTTP Public-Key-Pins",
"hsts": "HTTP Strict-Transport-Security",
"ichigojam": "IchigoJam",
"icu-message-format": "ICU Message Format",
2021-02-23 19:44:32 +08:00
"idr": "Idris",
2020-07-22 23:12:01 +08:00
"ignore": ".ignore",
"gitignore": ".gitignore",
"hgignore": ".hgignore",
"npmignore": ".npmignore",
"inform7": "Inform 7",
"javadoc": "JavaDoc",
"javadoclike": "JavaDoc-like",
"javastacktrace": "Java stack trace",
"jq": "JQ",
"jsdoc": "JSDoc",
"js-extras": "JS Extras",
"json": "JSON",
"webmanifest": "Web App Manifest",
"json5": "JSON5",
"jsonp": "JSONP",
"jsstacktrace": "JS stack trace",
"js-templates": "JS Templates",
"keepalived": "Keepalived Configure",
"kts": "Kotlin Script",
"kt": "Kotlin",
2021-02-13 22:14:46 +08:00
"kumir": "KuMir (КуМир)",
"kum": "KuMir (КуМир)",
"latex": "LaTeX",
"tex": "TeX",
"context": "ConTeXt",
"lilypond": "LilyPond",
"ly": "LilyPond",
"emacs": "Lisp",
"elisp": "Lisp",
"emacs-lisp": "Lisp",
"llvm": "LLVM IR",
"log": "Log file",
"lolcode": "LOLCODE",
2021-09-13 01:57:47 +08:00
"magma": "Magma (CAS)",
"md": "Markdown",
"markup-templating": "Markup templating",
"matlab": "MATLAB",
2021-09-16 03:39:09 +08:00
"maxscript": "MAXScript",
"mel": "MEL",
"metafont": "METAFONT",
"mongodb": "MongoDB",
"moon": "MoonScript",
"n1ql": "N1QL",
"n4js": "N4JS",
"n4jsd": "N4JS",
"nand2tetris-hdl": "Nand To Tetris HDL",
2020-08-08 02:14:34 +08:00
"naniscript": "Naninovel Script",
"nani": "Naninovel Script",
"nasm": "NASM",
"neon": "NEON",
"nginx": "nginx",
"nsis": "NSIS",
"objectivec": "Objective-C",
2020-04-26 19:30:31 +08:00
"objc": "Objective-C",
"ocaml": "OCaml",
"opencl": "OpenCL",
2021-03-21 23:44:25 +08:00
"openqasm": "OpenQasm",
"qasm": "OpenQasm",
"parigp": "PARI/GP",
"objectpascal": "Object Pascal",
"psl": "PATROL Scripting Language",
"pcaxis": "PC-Axis",
"px": "PC-Axis",
2020-04-28 04:29:20 +08:00
"peoplecode": "PeopleCode",
"pcode": "PeopleCode",
"php": "PHP",
"phpdoc": "PHPDoc",
"php-extras": "PHP Extras",
2022-03-21 22:28:39 +08:00
"plant-uml": "PlantUML",
"plantuml": "PlantUML",
"plsql": "PL/SQL",
"powerquery": "PowerQuery",
"pq": "PowerQuery",
"mscript": "PowerQuery",
"powershell": "PowerShell",
2020-11-13 03:43:52 +08:00
"promql": "PromQL",
"properties": ".properties",
"protobuf": "Protocol Buffers",
"purebasic": "PureBasic",
"pbfasm": "PureBasic",
"purs": "PureScript",
"py": "Python",
2021-03-23 20:04:45 +08:00
"qsharp": "Q#",
"qs": "Q#",
"q": "Q (kdb+ database)",
"qml": "QML",
2020-04-28 05:18:30 +08:00
"rkt": "Racket",
"cshtml": "Razor C#",
"razor": "Razor C#",
"jsx": "React JSX",
"tsx": "React TSX",
"renpy": "Ren'py",
2020-05-16 10:22:21 +08:00
"rpy": "Ren'py",
2022-04-16 04:48:02 +08:00
"res": "ReScript",
"rest": "reST (reStructuredText)",
"robotframework": "Robot Framework",
"robot": "Robot Framework",
"rb": "Ruby",
"sas": "SAS",
"sass": "Sass (Sass)",
"scss": "Sass (SCSS)",
2019-06-12 21:46:07 +08:00
"shell-session": "Shell session",
2020-09-11 21:24:20 +08:00
"sh-session": "Shell session",
"shellsession": "Shell session",
2020-09-14 05:26:33 +08:00
"sml": "SML",
"smlnj": "SML/NJ",
"solidity": "Solidity (Ethereum)",
2020-05-29 22:22:10 +08:00
"sol": "Solidity (Ethereum)",
"solution-file": "Solution file",
"sln": "Solution file",
"soy": "Soy (Closure Template)",
"sparql": "SPARQL",
"rq": "SPARQL",
"splunk-spl": "Splunk SPL",
"sqf": "SQF: Status Quo Function (Arma 3)",
"sql": "SQL",
"stata": "Stata Ado",
"iecst": "Structured Text (IEC 61131-3)",
"supercollider": "SuperCollider",
"sclang": "SuperCollider",
"systemd": "Systemd configuration file",
"t4-templating": "T4 templating",
"t4-cs": "T4 Text Templates (C#)",
"t4": "T4 Text Templates (C#)",
"t4-vb": "T4 Text Templates (VB)",
"tap": "TAP",
"tt2": "Template Toolkit 2",
"toml": "TOML",
"trickle": "trickle",
"troy": "troy",
"trig": "TriG",
"ts": "TypeScript",
2020-08-11 21:33:48 +08:00
"tsconfig": "TSConfig",
2020-04-28 04:33:02 +08:00
"uscript": "UnrealScript",
"uc": "UnrealScript",
2022-01-29 00:13:21 +08:00
"uorazor": "UO Razor Script",
2021-01-24 22:51:37 +08:00
"uri": "URI",
"url": "URL",
"vbnet": "VB.Net",
"vhdl": "VHDL",
"vim": "vim",
"visual-basic": "Visual Basic",
2020-07-23 00:37:16 +08:00
"vba": "VBA",
"vb": "Visual Basic",
"wasm": "WebAssembly",
2021-10-11 22:52:12 +08:00
"web-idl": "Web IDL",
"webidl": "Web IDL",
2022-05-18 22:51:07 +08:00
"wgsl": "WGSL",
"wiki": "Wiki markup",
"wolfram": "Wolfram language",
"nb": "Mathematica Notebook",
"wl": "Wolfram language",
"xeoracube": "XeoraCube",
"xml-doc": "XML doc (.net)",
"xojo": "Xojo (REALbasic)",
"xquery": "XQuery",
"yaml": "YAML",
2020-07-22 23:27:00 +08:00
"yml": "YAML",
"yang": "YANG"
}/*]*/;
2021-04-18 04:26:40 +08:00
/* eslint-enable */
Prism.plugins.toolbar.registerButton('show-language', function (env) {
var pre = env.element.parentNode;
if (!pre || !/pre/i.test(pre.nodeName)) {
return;
}
/**
* Tries to guess the name of a language given its id.
*
* @param {string} id The language id.
* @returns {string}
*/
function guessTitle(id) {
if (!id) {
return id;
}
return (id.substring(0, 1).toUpperCase() + id.substring(1)).replace(/s(?=cript)/, 'S');
}
var language = pre.getAttribute('data-language') || Languages[env.language] || guessTitle(env.language);
if (!language) {
return;
}
var element = document.createElement('span');
element.textContent = language;
return element;
});
2021-04-18 21:55:05 +08:00
}());