UNPKG

prism-react-renderer

Version:
1,503 lines (1,386 loc) 137 kB
/** * Prism: Lightweight, robust, elegant syntax highlighting * * @license MIT <https://opensource.org/licenses/MIT> * @author Lea Verou <https://lea.verou.me> * @namespace * @public */ /** * prism-react-renderer: * This file has been modified to remove: * - globals and window dependency * - worker support * - highlightAll and other element dependent methods * - _.hooks helpers * - UMD/node-specific hacks * It has also been run through prettier */ var Prism = (function () { // Private helper vars var lang = /(?:^|\s)lang(?:uage)?-([\w-]+)(?=\s|$)/i; var uniqueId = 0; // The grammar object for plaintext var plainTextGrammar = {}; var _ = { /** * A namespace for utility methods. * * All function in this namespace that are not explicitly marked as _public_ are for __internal use only__ and may * change or disappear at any time. * * @namespace * @memberof Prism */ util: { encode: function encode(tokens) { if (tokens instanceof Token) { return new Token(tokens.type, encode(tokens.content), tokens.alias); } else if (Array.isArray(tokens)) { return tokens.map(encode); } else { return tokens.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/\u00a0/g, ' '); } }, /** * Returns the name of the type of the given value. * * @param {any} o * @returns {string} * @example * type(null) === 'Null' * type(undefined) === 'Undefined' * type(123) === 'Number' * type('foo') === 'String' * type(true) === 'Boolean' * type([1, 2]) === 'Array' * type({}) === 'Object' * type(String) === 'Function' * type(/abc+/) === 'RegExp' */ type: function (o) { return Object.prototype.toString.call(o).slice(8, -1); }, /** * Returns a unique number for the given object. Later calls will still return the same number. * * @param {Object} obj * @returns {number} */ objId: function (obj) { if (!obj['__id']) { Object.defineProperty(obj, '__id', { value: ++uniqueId }); } return obj['__id']; }, /** * Creates a deep clone of the given object. * * The main intended use of this function is to clone language definitions. * * @param {T} o * @param {Record<number, any>} [visited] * @returns {T} * @template T */ clone: function deepClone(o, visited) { visited = visited || {}; var clone; var id; switch (_.util.type(o)) { case 'Object': id = _.util.objId(o); if (visited[id]) { return visited[id]; } clone = /** @type {Record<string, any>} */ ({}); visited[id] = clone; for (var key in o) { if (o.hasOwnProperty(key)) { clone[key] = deepClone(o[key], visited); } } return /** @type {any} */ (clone); case 'Array': id = _.util.objId(o); if (visited[id]) { return visited[id]; } clone = []; visited[id] = clone; (/** @type {Array} */(/** @type {any} */(o))).forEach(function (v, i) { clone[i] = deepClone(v, visited); }); return /** @type {any} */ (clone); default: return o; } }, /** * Returns the Prism language of the given element set by a `language-xxxx` or `lang-xxxx` class. * * If no language is set for the element or the element is `null` or `undefined`, `none` will be returned. * * @param {Element} element * @returns {string} */ getLanguage: function (element) { while (element) { var m = lang.exec(element.className); if (m) { return m[1].toLowerCase(); } element = element.parentElement; } return 'none'; }, /** * Sets the Prism `language-xxxx` class of the given element. * * @param {Element} element * @param {string} language * @returns {void} */ setLanguage: function (element, language) { // remove all `language-xxxx` classes // (this might leave behind a leading space) element.className = element.className.replace(RegExp(lang, 'gi'), ''); // add the new `language-xxxx` class // (using `classList` will automatically clean up spaces for us) element.classList.add('language-' + language); }, /** * Returns whether a given class is active for `element`. * * The class can be activated if `element` or one of its ancestors has the given class and it can be deactivated * if `element` or one of its ancestors has the negated version of the given class. The _negated version_ of the * given class is just the given class with a `no-` prefix. * * Whether the class is active is determined by the closest ancestor of `element` (where `element` itself is * closest ancestor) that has the given class or the negated version of it. If neither `element` nor any of its * ancestors have the given class or the negated version of it, then the default activation will be returned. * * In the paradoxical situation where the closest ancestor contains __both__ the given class and the negated * version of it, the class is considered active. * * @param {Element} element * @param {string} className * @param {boolean} [defaultActivation=false] * @returns {boolean} */ isActive: function (element, className, defaultActivation) { var no = 'no-' + className; while (element) { var classList = element.classList; if (classList.contains(className)) { return true; } if (classList.contains(no)) { return false; } element = element.parentElement; } return !!defaultActivation; } }, /** * This namespace contains all currently loaded languages and the some helper functions to create and modify languages. * * @namespace * @memberof Prism * @public */ languages: { /** * The grammar for plain, unformatted text. */ plain: plainTextGrammar, plaintext: plainTextGrammar, text: plainTextGrammar, txt: plainTextGrammar, /** * Creates a deep copy of the language with the given id and appends the given tokens. * * If a token in `redef` also appears in the copied language, then the existing token in the copied language * will be overwritten at its original position. * * ## Best practices * * Since the position of overwriting tokens (token in `redef` that overwrite tokens in the copied language) * doesn't matter, they can technically be in any order. However, this can be confusing to others that trying to * understand the language definition because, normally, the order of tokens matters in Prism grammars. * * Therefore, it is encouraged to order overwriting tokens according to the positions of the overwritten tokens. * Furthermore, all non-overwriting tokens should be placed after the overwriting ones. * * @param {string} id The id of the language to extend. This has to be a key in `Prism.languages`. * @param {Grammar} redef The new tokens to append. * @returns {Grammar} The new language created. * @public * @example * Prism.languages['css-with-colors'] = Prism.languages.extend('css', { * // Prism.languages.css already has a 'comment' token, so this token will overwrite CSS' 'comment' token * // at its original position * 'comment': { ... }, * // CSS doesn't have a 'color' token, so this token will be appended * 'color': /\b(?:red|green|blue)\b/ * }); */ extend: function (id, redef) { var lang = _.util.clone(_.languages[id]); for (var key in redef) { lang[key] = redef[key]; } return lang; }, /** * Inserts tokens _before_ another token in a language definition or any other grammar. * * ## Usage * * This helper method makes it easy to modify existing languages. For example, the CSS language definition * not only defines CSS highlighting for CSS documents, but also needs to define highlighting for CSS embedded * in HTML through `<style>` elements. To do this, it needs to modify `Prism.languages.markup` and add the * appropriate tokens. However, `Prism.languages.markup` is a regular JavaScript object literal, so if you do * this: * * ```js * Prism.languages.markup.style = { * // token * }; * ``` * * then the `style` token will be added (and processed) at the end. `insertBefore` allows you to insert tokens * before existing tokens. For the CSS example above, you would use it like this: * * ```js * Prism.languages.insertBefore('markup', 'cdata', { * 'style': { * // token * } * }); * ``` * * ## Special cases * * If the grammars of `inside` and `insert` have tokens with the same name, the tokens in `inside`'s grammar * will be ignored. * * This behavior can be used to insert tokens after `before`: * * ```js * Prism.languages.insertBefore('markup', 'comment', { * 'comment': Prism.languages.markup.comment, * // tokens after 'comment' * }); * ``` * * ## Limitations * * The main problem `insertBefore` has to solve is iteration order. Since ES2015, the iteration order for object * properties is guaranteed to be the insertion order (except for integer keys) but some browsers behave * differently when keys are deleted and re-inserted. So `insertBefore` can't be implemented by temporarily * deleting properties which is necessary to insert at arbitrary positions. * * To solve this problem, `insertBefore` doesn't actually insert the given tokens into the target object. * Instead, it will create a new object and replace all references to the target object with the new one. This * can be done without temporarily deleting properties, so the iteration order is well-defined. * * However, only references that can be reached from `Prism.languages` or `insert` will be replaced. I.e. if * you hold the target object in a variable, then the value of the variable will not change. * * ```js * var oldMarkup = Prism.languages.markup; * var newMarkup = Prism.languages.insertBefore('markup', 'comment', { ... }); * * assert(oldMarkup !== Prism.languages.markup); * assert(newMarkup === Prism.languages.markup); * ``` * * @param {string} inside The property of `root` (e.g. a language id in `Prism.languages`) that contains the * object to be modified. * @param {string} before The key to insert before. * @param {Grammar} insert An object containing the key-value pairs to be inserted. * @param {Object<string, any>} [root] The object containing `inside`, i.e. the object that contains the * object to be modified. * * Defaults to `Prism.languages`. * @returns {Grammar} The new grammar object. * @public */ insertBefore: function (inside, before, insert, root) { root = root || /** @type {any} */ (_.languages); var grammar = root[inside]; /** @type {Grammar} */ var ret = {}; for (var token in grammar) { if (grammar.hasOwnProperty(token)) { if (token == before) { for (var newToken in insert) { if (insert.hasOwnProperty(newToken)) { ret[newToken] = insert[newToken]; } } } // Do not insert token which also occur in insert. See #1525 if (!insert.hasOwnProperty(token)) { ret[token] = grammar[token]; } } } var old = root[inside]; root[inside] = ret; // Update references in other language definitions _.languages.DFS(_.languages, function (key, value) { if (value === old && key != inside) { this[key] = ret; } }); return ret; }, // Traverse a language definition with Depth First Search DFS: function DFS(o, callback, type, visited) { visited = visited || {}; var objId = _.util.objId; for (var i in o) { if (o.hasOwnProperty(i)) { callback.call(o, i, o[i], type || i); var property = o[i]; var propertyType = _.util.type(property); if (propertyType === 'Object' && !visited[objId(property)]) { visited[objId(property)] = true; DFS(property, callback, null, visited); } else if (propertyType === 'Array' && !visited[objId(property)]) { visited[objId(property)] = true; DFS(property, callback, i, visited); } } } } }, plugins: {}, /** * Low-level function, only use if you know what you’re doing. It accepts a string of text as input * and the language definitions to use, and returns a string with the HTML produced. * * The following hooks will be run: * 1. `before-tokenize` * 2. `after-tokenize` * 3. `wrap`: On each {@link Token}. * * @param {string} text A string with the code to be highlighted. * @param {Grammar} grammar An object containing the tokens to use. * * Usually a language definition like `Prism.languages.markup`. * @param {string} language The name of the language definition passed to `grammar`. * @returns {string} The highlighted HTML. * @memberof Prism * @public * @example * Prism.highlight('var foo = true;', Prism.languages.javascript, 'javascript'); */ highlight: function (text, grammar, language) { var env = { code: text, grammar: grammar, language: language }; _.hooks.run('before-tokenize', env); env.tokens = _.tokenize(env.code, env.grammar); _.hooks.run('after-tokenize', env); return Token.stringify(_.util.encode(env.tokens), env.language); }, /** * This is the heart of Prism, and the most low-level function you can use. It accepts a string of text as input * and the language definitions to use, and returns an array with the tokenized code. * * When the language definition includes nested tokens, the function is called recursively on each of these tokens. * * This method could be useful in other contexts as well, as a very crude parser. * * @param {string} text A string with the code to be highlighted. * @param {Grammar} grammar An object containing the tokens to use. * * Usually a language definition like `Prism.languages.markup`. * @returns {TokenStream} An array of strings and tokens, a token stream. * @memberof Prism * @public * @example * let code = `var foo = 0;`; * let tokens = Prism.tokenize(code, Prism.languages.javascript); * tokens.forEach(token => { * if (token instanceof Prism.Token && token.type === 'number') { * console.log(`Found numeric literal: ${token.content}`); * } * }); */ tokenize: function (text, grammar) { var rest = grammar.rest; if (rest) { for (var token in rest) { grammar[token] = rest[token]; } delete grammar.rest; } var tokenList = new LinkedList(); addAfter(tokenList, tokenList.head, text); matchGrammar(text, tokenList, grammar, tokenList.head, 0); return toArray(tokenList); }, /** * @namespace * @memberof Prism * @public */ hooks: { all: {}, /** * Adds the given callback to the list of callbacks for the given hook. * * The callback will be invoked when the hook it is registered for is run. * Hooks are usually directly run by a highlight function but you can also run hooks yourself. * * One callback function can be registered to multiple hooks and the same hook multiple times. * * @param {string} name The name of the hook. * @param {HookCallback} callback The callback function which is given environment variables. * @public */ add: function (name, callback) { var hooks = _.hooks.all; hooks[name] = hooks[name] || []; hooks[name].push(callback); }, /** * Runs a hook invoking all registered callbacks with the given environment variables. * * Callbacks will be invoked synchronously and in the order in which they were registered. * * @param {string} name The name of the hook. * @param {Object<string, any>} env The environment variables of the hook passed to all callbacks registered. * @public */ run: function (name, env) { var callbacks = _.hooks.all[name]; if (!callbacks || !callbacks.length) { return; } for (var i = 0, callback; (callback = callbacks[i++]);) { callback(env); } } }, Token: Token }; // Typescript note: // The following can be used to import the Token type in JSDoc: // // @typedef {InstanceType<import("./prism-core")["Token"]>} Token /** * Creates a new token. * * @param {string} type See {@link Token#type type} * @param {string | TokenStream} content See {@link Token#content content} * @param {string|string[]} [alias] The alias(es) of the token. * @param {string} [matchedStr=""] A copy of the full string this token was created from. * @class * @global * @public */ function Token(type, content, alias, matchedStr) { /** * The type of the token. * * This is usually the key of a pattern in a {@link Grammar}. * * @type {string} * @see GrammarToken * @public */ this.type = type; /** * The strings or tokens contained by this token. * * This will be a token stream if the pattern matched also defined an `inside` grammar. * * @type {string | TokenStream} * @public */ this.content = content; /** * The alias(es) of the token. * * @type {string|string[]} * @see GrammarToken * @public */ this.alias = alias; // Copy of the full string this token was created from this.length = (matchedStr || '').length | 0; } /** * A token stream is an array of strings and {@link Token Token} objects. * * Token streams have to fulfill a few properties that are assumed by most functions (mostly internal ones) that process * them. * * 1. No adjacent strings. * 2. No empty strings. * * The only exception here is the token stream that only contains the empty string and nothing else. * * @typedef {Array<string | Token>} TokenStream * @global * @public */ /** * Converts the given token or token stream to an HTML representation. * * The following hooks will be run: * 1. `wrap`: On each {@link Token}. * * @param {string | Token | TokenStream} o The token or token stream to be converted. * @param {string} language The name of current language. * @returns {string} The HTML representation of the token or token stream. * @memberof Token * @static */ Token.stringify = function stringify(o, language) { if (typeof o == 'string') { return o; } if (Array.isArray(o)) { var s = ''; o.forEach(function (e) { s += stringify(e, language); }); return s; } var env = { type: o.type, content: stringify(o.content, language), tag: 'span', classes: ['token', o.type], attributes: {}, language: language }; var aliases = o.alias; if (aliases) { if (Array.isArray(aliases)) { Array.prototype.push.apply(env.classes, aliases); } else { env.classes.push(aliases); } } _.hooks.run('wrap', env); var attributes = ''; for (var name in env.attributes) { attributes += ' ' + name + '="' + (env.attributes[name] || '').replace(/"/g, '&quot;') + '"'; } return '<' + env.tag + ' class="' + env.classes.join(' ') + '"' + attributes + '>' + env.content + '</' + env.tag + '>'; }; /** * @param {RegExp} pattern * @param {number} pos * @param {string} text * @param {boolean} lookbehind * @returns {RegExpExecArray | null} */ function matchPattern(pattern, pos, text, lookbehind) { pattern.lastIndex = pos; var match = pattern.exec(text); if (match && lookbehind && match[1]) { // change the match to remove the text matched by the Prism lookbehind group var lookbehindLength = match[1].length; match.index += lookbehindLength; match[0] = match[0].slice(lookbehindLength); } return match; } /** * @param {string} text * @param {LinkedList<string | Token>} tokenList * @param {any} grammar * @param {LinkedListNode<string | Token>} startNode * @param {number} startPos * @param {RematchOptions} [rematch] * @returns {void} * @private * * @typedef RematchOptions * @property {string} cause * @property {number} reach */ function matchGrammar(text, tokenList, grammar, startNode, startPos, rematch) { for (var token in grammar) { if (!grammar.hasOwnProperty(token) || !grammar[token]) { continue; } var patterns = grammar[token]; patterns = Array.isArray(patterns) ? patterns : [patterns]; for (var j = 0; j < patterns.length; ++j) { if (rematch && rematch.cause == token + ',' + j) { return; } var patternObj = patterns[j]; var inside = patternObj.inside; var lookbehind = !!patternObj.lookbehind; var greedy = !!patternObj.greedy; var alias = patternObj.alias; if (greedy && !patternObj.pattern.global) { // Without the global flag, lastIndex won't work var flags = patternObj.pattern.toString().match(/[imsuy]*$/)[0]; patternObj.pattern = RegExp(patternObj.pattern.source, flags + 'g'); } /** @type {RegExp} */ var pattern = patternObj.pattern || patternObj; for ( // iterate the token list and keep track of the current token/string position var currentNode = startNode.next, pos = startPos; currentNode !== tokenList.tail; pos += currentNode.value.length, currentNode = currentNode.next ) { if (rematch && pos >= rematch.reach) { break; } var str = currentNode.value; if (tokenList.length > text.length) { // Something went terribly wrong, ABORT, ABORT! return; } if (str instanceof Token) { continue; } var removeCount = 1; // this is the to parameter of removeBetween var match; if (greedy) { match = matchPattern(pattern, pos, text, lookbehind); if (!match || match.index >= text.length) { break; } var from = match.index; var to = match.index + match[0].length; var p = pos; // find the node that contains the match p += currentNode.value.length; while (from >= p) { currentNode = currentNode.next; p += currentNode.value.length; } // adjust pos (and p) p -= currentNode.value.length; pos = p; // the current node is a Token, then the match starts inside another Token, which is invalid if (currentNode.value instanceof Token) { continue; } // find the last node which is affected by this match for ( var k = currentNode; k !== tokenList.tail && (p < to || typeof k.value === 'string'); k = k.next ) { removeCount++; p += k.value.length; } removeCount--; // replace with the new match str = text.slice(pos, p); match.index -= pos; } else { match = matchPattern(pattern, 0, str, lookbehind); if (!match) { continue; } } // eslint-disable-next-line no-redeclare var from = match.index; var matchStr = match[0]; var before = str.slice(0, from); var after = str.slice(from + matchStr.length); var reach = pos + str.length; if (rematch && reach > rematch.reach) { rematch.reach = reach; } var removeFrom = currentNode.prev; if (before) { removeFrom = addAfter(tokenList, removeFrom, before); pos += before.length; } removeRange(tokenList, removeFrom, removeCount); var wrapped = new Token(token, inside ? _.tokenize(matchStr, inside) : matchStr, alias, matchStr); currentNode = addAfter(tokenList, removeFrom, wrapped); if (after) { addAfter(tokenList, currentNode, after); } if (removeCount > 1) { // at least one Token object was removed, so we have to do some rematching // this can only happen if the current pattern is greedy /** @type {RematchOptions} */ var nestedRematch = { cause: token + ',' + j, reach: reach }; matchGrammar(text, tokenList, grammar, currentNode.prev, pos, nestedRematch); // the reach might have been extended because of the rematching if (rematch && nestedRematch.reach > rematch.reach) { rematch.reach = nestedRematch.reach; } } } } } } /** * @typedef LinkedListNode * @property {T} value * @property {LinkedListNode<T> | null} prev The previous node. * @property {LinkedListNode<T> | null} next The next node. * @template T * @private */ /** * @template T * @private */ function LinkedList() { /** @type {LinkedListNode<T>} */ var head = { value: null, prev: null, next: null }; /** @type {LinkedListNode<T>} */ var tail = { value: null, prev: head, next: null }; head.next = tail; /** @type {LinkedListNode<T>} */ this.head = head; /** @type {LinkedListNode<T>} */ this.tail = tail; this.length = 0; } /** * Adds a new node with the given value to the list. * * @param {LinkedList<T>} list * @param {LinkedListNode<T>} node * @param {T} value * @returns {LinkedListNode<T>} The added node. * @template T */ function addAfter(list, node, value) { // assumes that node != list.tail && values.length >= 0 var next = node.next; var newNode = { value: value, prev: node, next: next }; node.next = newNode; next.prev = newNode; list.length++; return newNode; } /** * Removes `count` nodes after the given node. The given node will not be removed. * * @param {LinkedList<T>} list * @param {LinkedListNode<T>} node * @param {number} count * @template T */ function removeRange(list, node, count) { var next = node.next; for (var i = 0; i < count && next !== list.tail; i++) { next = next.next; } node.next = next; next.prev = node; list.length -= i; } /** * @param {LinkedList<T>} list * @returns {T[]} * @template T */ function toArray(list) { var array = []; var node = list.head.next; while (node !== list.tail) { array.push(node.value); node = node.next; } return array; } return _; }()); var prism = Prism; Prism.default = Prism; /* This content is auto-generated to include some prismjs language components: */ /* "prismjs/components/prism-markup" */ prism.languages.markup = { 'comment': { pattern: /<!--(?:(?!<!--)[\s\S])*?-->/, greedy: true }, 'prolog': { pattern: /<\?[\s\S]+?\?>/, greedy: true }, 'doctype': { // https://www.w3.org/TR/xml/#NT-doctypedecl pattern: /<!DOCTYPE(?:[^>"'[\]]|"[^"]*"|'[^']*')+(?:\[(?:[^<"'\]]|"[^"]*"|'[^']*'|<(?!!--)|<!--(?:[^-]|-(?!->))*-->)*\]\s*)?>/i, greedy: true, inside: { 'internal-subset': { pattern: /(^[^\[]*\[)[\s\S]+(?=\]>$)/, lookbehind: true, greedy: true, inside: null // see below }, 'string': { pattern: /"[^"]*"|'[^']*'/, greedy: true }, 'punctuation': /^<!|>$|[[\]]/, 'doctype-tag': /^DOCTYPE/i, 'name': /[^\s<>'"]+/ } }, 'cdata': { pattern: /<!\[CDATA\[[\s\S]*?\]\]>/i, greedy: true }, 'tag': { pattern: /<\/?(?!\d)[^\s>\/=$<%]+(?:\s(?:\s*[^\s>\/=]+(?:\s*=\s*(?:"[^"]*"|'[^']*'|[^\s'">=]+(?=[\s>]))|(?=[\s/>])))+)?\s*\/?>/, greedy: true, inside: { 'tag': { pattern: /^<\/?[^\s>\/]+/, inside: { 'punctuation': /^<\/?/, 'namespace': /^[^\s>\/:]+:/ } }, 'special-attr': [], 'attr-value': { pattern: /=\s*(?:"[^"]*"|'[^']*'|[^\s'">=]+)/, inside: { 'punctuation': [{ pattern: /^=/, alias: 'attr-equals' }, /"|'/] } }, 'punctuation': /\/?>/, 'attr-name': { pattern: /[^\s>\/]+/, inside: { 'namespace': /^[^\s>\/:]+:/ } } } }, 'entity': [{ pattern: /&[\da-z]{1,8};/i, alias: 'named-entity' }, /&#x?[\da-f]{1,8};/i] }; prism.languages.markup['tag'].inside['attr-value'].inside['entity'] = prism.languages.markup['entity']; prism.languages.markup['doctype'].inside['internal-subset'].inside = prism.languages.markup; // Plugin to make entity title show the real entity, idea by Roman Komarov prism.hooks.add('wrap', function (env) { if (env.type === 'entity') { env.attributes['title'] = env.content.replace(/&amp;/, '&'); } }); Object.defineProperty(prism.languages.markup.tag, 'addInlined', { /** * Adds an inlined language to markup. * * An example of an inlined language is CSS with `<style>` tags. * * @param {string} tagName The name of the tag that contains the inlined language. This name will be treated as * case insensitive. * @param {string} lang The language key. * @example * addInlined('style', 'css'); */ value: function addInlined(tagName, lang) { var includedCdataInside = {}; includedCdataInside['language-' + lang] = { pattern: /(^<!\[CDATA\[)[\s\S]+?(?=\]\]>$)/i, lookbehind: true, inside: prism.languages[lang] }; includedCdataInside['cdata'] = /^<!\[CDATA\[|\]\]>$/i; var inside = { 'included-cdata': { pattern: /<!\[CDATA\[[\s\S]*?\]\]>/i, inside: includedCdataInside } }; inside['language-' + lang] = { pattern: /[\s\S]+/, inside: prism.languages[lang] }; var def = {}; def[tagName] = { pattern: RegExp(/(<__[^>]*>)(?:<!\[CDATA\[(?:[^\]]|\](?!\]>))*\]\]>|(?!<!\[CDATA\[)[\s\S])*?(?=<\/__>)/.source.replace(/__/g, function () { return tagName; }), 'i'), lookbehind: true, greedy: true, inside: inside }; prism.languages.insertBefore('markup', 'cdata', def); } }); Object.defineProperty(prism.languages.markup.tag, 'addAttribute', { /** * Adds an pattern to highlight languages embedded in HTML attributes. * * An example of an inlined language is CSS with `style` attributes. * * @param {string} attrName The name of the tag that contains the inlined language. This name will be treated as * case insensitive. * @param {string} lang The language key. * @example * addAttribute('style', 'css'); */ value: function (attrName, lang) { prism.languages.markup.tag.inside['special-attr'].push({ pattern: RegExp(/(^|["'\s])/.source + '(?:' + attrName + ')' + /\s*=\s*(?:"[^"]*"|'[^']*'|[^\s'">=]+(?=[\s>]))/.source, 'i'), lookbehind: true, inside: { 'attr-name': /^[^\s=]+/, 'attr-value': { pattern: /=[\s\S]+/, inside: { 'value': { pattern: /(^=\s*(["']|(?!["'])))\S[\s\S]*(?=\2$)/, lookbehind: true, alias: [lang, 'language-' + lang], inside: prism.languages[lang] }, 'punctuation': [{ pattern: /^=/, alias: 'attr-equals' }, /"|'/] } } } }); } }); prism.languages.html = prism.languages.markup; prism.languages.mathml = prism.languages.markup; prism.languages.svg = prism.languages.markup; prism.languages.xml = prism.languages.extend('markup', {}); prism.languages.ssml = prism.languages.xml; prism.languages.atom = prism.languages.xml; prism.languages.rss = prism.languages.xml; /* "prismjs/components/prism-bash" */ (function (Prism) { // $ set | grep '^[A-Z][^[:space:]]*=' | cut -d= -f1 | tr '\n' '|' // + LC_ALL, RANDOM, REPLY, SECONDS. // + make sure PS1..4 are here as they are not always set, // - some useless things. var envVars = '\\b(?:BASH|BASHOPTS|BASH_ALIASES|BASH_ARGC|BASH_ARGV|BASH_CMDS|BASH_COMPLETION_COMPAT_DIR|BASH_LINENO|BASH_REMATCH|BASH_SOURCE|BASH_VERSINFO|BASH_VERSION|COLORTERM|COLUMNS|COMP_WORDBREAKS|DBUS_SESSION_BUS_ADDRESS|DEFAULTS_PATH|DESKTOP_SESSION|DIRSTACK|DISPLAY|EUID|GDMSESSION|GDM_LANG|GNOME_KEYRING_CONTROL|GNOME_KEYRING_PID|GPG_AGENT_INFO|GROUPS|HISTCONTROL|HISTFILE|HISTFILESIZE|HISTSIZE|HOME|HOSTNAME|HOSTTYPE|IFS|INSTANCE|JOB|LANG|LANGUAGE|LC_ADDRESS|LC_ALL|LC_IDENTIFICATION|LC_MEASUREMENT|LC_MONETARY|LC_NAME|LC_NUMERIC|LC_PAPER|LC_TELEPHONE|LC_TIME|LESSCLOSE|LESSOPEN|LINES|LOGNAME|LS_COLORS|MACHTYPE|MAILCHECK|MANDATORY_PATH|NO_AT_BRIDGE|OLDPWD|OPTERR|OPTIND|ORBIT_SOCKETDIR|OSTYPE|PAPERSIZE|PATH|PIPESTATUS|PPID|PS1|PS2|PS3|PS4|PWD|RANDOM|REPLY|SECONDS|SELINUX_INIT|SESSION|SESSIONTYPE|SESSION_MANAGER|SHELL|SHELLOPTS|SHLVL|SSH_AUTH_SOCK|TERM|UID|UPSTART_EVENTS|UPSTART_INSTANCE|UPSTART_JOB|UPSTART_SESSION|USER|WINDOWID|XAUTHORITY|XDG_CONFIG_DIRS|XDG_CURRENT_DESKTOP|XDG_DATA_DIRS|XDG_GREETER_DATA_DIR|XDG_MENU_PREFIX|XDG_RUNTIME_DIR|XDG_SEAT|XDG_SEAT_PATH|XDG_SESSION_DESKTOP|XDG_SESSION_ID|XDG_SESSION_PATH|XDG_SESSION_TYPE|XDG_VTNR|XMODIFIERS)\\b'; var commandAfterHeredoc = { pattern: /(^(["']?)\w+\2)[ \t]+\S.*/, lookbehind: true, alias: 'punctuation', // this looks reasonably well in all themes inside: null // see below }; var insideString = { 'bash': commandAfterHeredoc, 'environment': { pattern: RegExp('\\$' + envVars), alias: 'constant' }, 'variable': [// [0]: Arithmetic Environment { pattern: /\$?\(\([\s\S]+?\)\)/, greedy: true, inside: { // If there is a $ sign at the beginning highlight $(( and )) as variable 'variable': [{ pattern: /(^\$\(\([\s\S]+)\)\)/, lookbehind: true }, /^\$\(\(/], 'number': /\b0x[\dA-Fa-f]+\b|(?:\b\d+(?:\.\d*)?|\B\.\d+)(?:[Ee]-?\d+)?/, // Operators according to https://www.gnu.org/software/bash/manual/bashref.html#Shell-Arithmetic 'operator': /--|\+\+|\*\*=?|<<=?|>>=?|&&|\|\||[=!+\-*/%<>^&|]=?|[?~:]/, // If there is no $ sign at the beginning highlight (( and )) as punctuation 'punctuation': /\(\(?|\)\)?|,|;/ } }, // [1]: Command Substitution { pattern: /\$\((?:\([^)]+\)|[^()])+\)|`[^`]+`/, greedy: true, inside: { 'variable': /^\$\(|^`|\)$|`$/ } }, // [2]: Brace expansion { pattern: /\$\{[^}]+\}/, greedy: true, inside: { 'operator': /:[-=?+]?|[!\/]|##?|%%?|\^\^?|,,?/, 'punctuation': /[\[\]]/, 'environment': { pattern: RegExp('(\\{)' + envVars), lookbehind: true, alias: 'constant' } } }, /\$(?:\w+|[#?*!@$])/], // Escape sequences from echo and printf's manuals, and escaped quotes. 'entity': /\\(?:[abceEfnrtv\\"]|O?[0-7]{1,3}|U[0-9a-fA-F]{8}|u[0-9a-fA-F]{4}|x[0-9a-fA-F]{1,2})/ }; Prism.languages.bash = { 'shebang': { pattern: /^#!\s*\/.*/, alias: 'important' }, 'comment': { pattern: /(^|[^"{\\$])#.*/, lookbehind: true }, 'function-name': [// a) function foo { // b) foo() { // c) function foo() { // but not “foo {” { // a) and c) pattern: /(\bfunction\s+)[\w-]+(?=(?:\s*\(?:\s*\))?\s*\{)/, lookbehind: true, alias: 'function' }, { // b) pattern: /\b[\w-]+(?=\s*\(\s*\)\s*\{)/, alias: 'function' }], // Highlight variable names as variables in for and select beginnings. 'for-or-select': { pattern: /(\b(?:for|select)\s+)\w+(?=\s+in\s)/, alias: 'variable', lookbehind: true }, // Highlight variable names as variables in the left-hand part // of assignments (“=” and “+=”). 'assign-left': { pattern: /(^|[\s;|&]|[<>]\()\w+(?=\+?=)/, inside: { 'environment': { pattern: RegExp('(^|[\\s;|&]|[<>]\\()' + envVars), lookbehind: true, alias: 'constant' } }, alias: 'variable', lookbehind: true }, 'string': [// Support for Here-documents https://en.wikipedia.org/wiki/Here_document { pattern: /((?:^|[^<])<<-?\s*)(\w+)\s[\s\S]*?(?:\r?\n|\r)\2/, lookbehind: true, greedy: true, inside: insideString }, // Here-document with quotes around the tag // → No expansion (so no “inside”). { pattern: /((?:^|[^<])<<-?\s*)(["'])(\w+)\2\s[\s\S]*?(?:\r?\n|\r)\3/, lookbehind: true, greedy: true, inside: { 'bash': commandAfterHeredoc } }, // “Normal” string { // https://www.gnu.org/software/bash/manual/html_node/Double-Quotes.html pattern: /(^|[^\\](?:\\\\)*)"(?:\\[\s\S]|\$\([^)]+\)|\$(?!\()|`[^`]+`|[^"\\`$])*"/, lookbehind: true, greedy: true, inside: insideString }, { // https://www.gnu.org/software/bash/manual/html_node/Single-Quotes.html pattern: /(^|[^$\\])'[^']*'/, lookbehind: true, greedy: true }, { // https://www.gnu.org/software/bash/manual/html_node/ANSI_002dC-Quoting.html pattern: /\$'(?:[^'\\]|\\[\s\S])*'/, greedy: true, inside: { 'entity': insideString.entity } }], 'environment': { pattern: RegExp('\\$?' + envVars), alias: 'constant' }, 'variable': insideString.variable, 'function': { pattern: /(^|[\s;|&]|[<>]\()(?:add|apropos|apt|apt-cache|apt-get|aptitude|aspell|automysqlbackup|awk|basename|bash|bc|bconsole|bg|bzip2|cal|cat|cfdisk|chgrp|chkconfig|chmod|chown|chroot|cksum|clear|cmp|column|comm|composer|cp|cron|crontab|csplit|curl|cut|date|dc|dd|ddrescue|debootstrap|df|diff|diff3|dig|dir|dircolors|dirname|dirs|dmesg|docker|docker-compose|du|egrep|eject|env|ethtool|expand|expect|expr|fdformat|fdisk|fg|fgrep|file|find|fmt|fold|format|free|fsck|ftp|fuser|gawk|git|gparted|grep|groupadd|groupdel|groupmod|groups|grub-mkconfig|gzip|halt|head|hg|history|host|hostname|htop|iconv|id|ifconfig|ifdown|ifup|import|install|ip|jobs|join|kill|killall|less|link|ln|locate|logname|logrotate|look|lpc|lpr|lprint|lprintd|lprintq|lprm|ls|lsof|lynx|make|man|mc|mdadm|mkconfig|mkdir|mke2fs|mkfifo|mkfs|mkisofs|mknod|mkswap|mmv|more|most|mount|mtools|mtr|mutt|mv|nano|nc|netstat|nice|nl|node|nohup|notify-send|npm|nslookup|op|open|parted|passwd|paste|pathchk|ping|pkill|pnpm|podman|podman-compose|popd|pr|printcap|printenv|ps|pushd|pv|quota|quotacheck|quotactl|ram|rar|rcp|reboot|remsync|rename|renice|rev|rm|rmdir|rpm|rsync|scp|screen|sdiff|sed|sendmail|seq|service|sftp|sh|shellcheck|shuf|shutdown|sleep|slocate|sort|split|ssh|stat|strace|su|sudo|sum|suspend|swapon|sync|tac|tail|tar|tee|time|timeout|top|touch|tr|traceroute|tsort|tty|umount|uname|unexpand|uniq|units|unrar|unshar|unzip|update-grub|uptime|useradd|userdel|usermod|users|uudecode|uuencode|v|vcpkg|vdir|vi|vim|virsh|vmstat|wait|watch|wc|wget|whereis|which|who|whoami|write|xargs|xdg-open|yarn|yes|zenity|zip|zsh|zypper)(?=$|[)\s;|&])/, lookbehind: true }, 'keyword': { pattern: /(^|[\s;|&]|[<>]\()(?:case|do|done|elif|else|esac|fi|for|function|if|in|select|then|until|while)(?=$|[)\s;|&])/, lookbehind: true }, // https://www.gnu.org/software/bash/manual/html_node/Shell-Builtin-Commands.html 'builtin': { pattern: /(^|[\s;|&]|[<>]\()(?:\.|:|alias|bind|break|builtin|caller|cd|command|continue|declare|echo|enable|eval|exec|exit|export|getopts|hash|help|let|local|logout|mapfile|printf|pwd|read|readarray|readonly|return|set|shift|shopt|source|test|times|trap|type|typeset|ulimit|umask|unalias|unset)(?=$|[)\s;|&])/, lookbehind: true, // Alias added to make those easier to distinguish from strings. alias: 'class-name' }, 'boolean': { pattern: /(^|[\s;|&]|[<>]\()(?:false|true)(?=$|[)\s;|&])/, lookbehind: true }, 'file-descriptor': { pattern: /\B&\d\b/, alias: 'important' }, 'operator': { // Lots of redirections here, but not just that. pattern: /\d?<>|>\||\+=|=[=~]?|!=?|<<[<-]?|[&\d]?>>|\d[<>]&?|[<>][&=]?|&[>&]?|\|[&|]?/, inside: { 'file-descriptor': { pattern: /^\d/, alias: 'important' } } }, 'punctuation': /\$?\(\(?|\)\)?|\.\.|[{}[\];\\]/, 'number': { pattern: /(^|\s)(?:[1-9]\d*|0)(?:[.,]\d+)?\b/, lookbehind: true } }; commandAfterHeredoc.inside = Prism.languages.bash; /* Patterns in command substitution. */ var toBeCopied = ['comment', 'function-name', 'for-or-select', 'assign-left', 'string', 'environment', 'function', 'keyword', 'builtin', 'boolean', 'file-descriptor', 'operator', 'punctuation', 'number']; var inside = insideString.variable[1].inside; for (var i = 0; i < toBeCopied.length; i++) { inside[toBeCopied[i]] = Prism.languages.bash[toBeCopied[i]]; } Prism.languages.shell = Prism.languages.bash; })(prism); /* "prismjs/components/prism-clike" */ prism.languages.clike = { 'comment': [{ pattern: /(^|[^\\])\/\*[\s\S]*?(?:\*\/|$)/, lookbehind: true, greedy: true }, { pattern: /(^|[^\\:])\/\/.*/, lookbehind: true, greedy: true }], 'string': { pattern: /(["'])(?:\\(?:\r\n|[\s\S])|(?!\1)[^\\\r\n])*\1/, greedy: true }, 'class-name': { pattern: /(\b(?:class|extends|implements|instanceof|interface|new|trait)\s+|\bcatch\s+\()[\w.\\]+/i, lookbehind: true, inside: { 'punctuation': /[.\\]/ } }, 'keyword': /\b(?:break|catch|continue|do|else|finally|for|function|if|in|instanceof|new|null|return|throw|try|while)\b/, 'boolean': /\b(?:false|true)\b/, 'function': /\b\w+(?=\()/, 'number': /\b0x[\da-f]+\b|(?:\b\d+(?:\.\d*)?|\B\.\d+)(?:e[+-]?\d+)?/i, 'operator': /[<>]=?|[!=]=?=?|--?|\+\+?|&&?|\|\|?|[?*/~^%]/, 'punctuation': /[{}[\];(),.:]/ }; /* "prismjs/components/prism-c" */ prism.languages.c = prism.languages.extend('clike', { 'comment': { pattern: /\/\/(?:[^\r\n\\]|\\(?:\r\n?|\n|(?![\r\n])))*|\/\*[\s\S]*?(?:\*\/|$)/, greedy: true }, 'string': { // https://en.cppreference.com/w/c/language/string_literal pattern: /"(?:\\(?:\r\n|[\s\S])|[^"\\\r\n])*"/, greedy: true }, 'class-name': { pattern: /(\b(?:enum|struct)\s+(?:__attribute__\s*\(\([\s\S]*?\)\)\s*)?)\w+|\b[a-z]\w*_t\b/, lookbehind: true }, 'keyword': /\b(?:_Alignas|_Alignof|_Atomic|_Bool|_Complex|_Generic|_Imaginary|_Noreturn|_Static_assert|_Thread_local|__attribute__|asm|auto|break|case|char|const|continue|default|do|double|else|enum|extern|float|for|goto|if|inline|int|long|register|return|short|signed|sizeof|static|struct|switch|typedef|typeof|union|unsigned|void|volatile|while)\b/, 'function': /\b[a-z_]\w*(?=\s*\()/i, 'number': /(?:\b0x(?:[\da-f]+(?:\.[\da-f]*)?|\.[\da-f]+)(?:p[+-]?\d+)?|(?:\b\d+(?:\.\d*)?|\B\.\d+)(?:e[+-]?\d+)?)[ful]{0,4}/i, 'operator': />>=?|<<=?|->|([-+&|:])\1|[?:~]|[-+*/%&|^!=<>]=?/ }); prism.languages.insertBefore('c', 'string', { 'char': { // https://en.cppreference.com/w/c/language/character_constant pattern: /'(?:\\(?:\r\n|[\s\S])|[^'\\\r\n]){0,32}'/, greedy: true } }); prism.languages.insertBefore('c', 'string', { 'macro': { // allow for multiline macro definitions // spaces after the # character compile fine with gcc pattern: /(^[\t ]*)#\s*[a-z](?:[^\r\n\\/]|\/(?!\*)|\/\*(?:[^*]|\*(?!\/))*\*\/|\\(?:\r\n|[\s\S]))*/im, lookbehind: true, greedy: true, alias: 'property', inside: { 'string': [{ // highlight the path of the include statement as a string pattern: /^(#\s*include\s*)<[^>]+>/, lookbehind: true }, prism.languages.c['string']], 'char': prism.languages.c['char'], 'comment': prism.languages.c['comment'], 'macro-name': [{ pattern: /(^#\s*define\s+)\w+\b(?!\()/i, lookbehind: true }, { pattern: /(^#\s*define\s+)\w+\b(?=\()/i, lookbehind: true, alias: 'function' }], // highlight macro directives as keywords 'directive': { pattern: /^(#\s*)[a-z]+/, lookbehind: true, alias: 'keyword' }, 'directive-hash': /^#/, 'punctuation': /##|\\(?=[\r\n])/, 'expression': { pattern: /\S[\s\S]*/, inside: prism.languages.c } } } }); prism.languages.insertBefore('c', 'function', { // highlight predefined macros as constants 'constant': /\b(?:EOF|NULL|SEEK_CUR|SEEK_END|SEEK_SET|__DATE__|__FILE__|__LINE__|__TIMESTAMP__|__TIME__|__func__|stderr|stdin|stdout)\b/ }); delete prism.languages.c['boolean']; /* "prismjs/components/prism-cpp" */ (function (Prism) { var keyword = /\b(?:alignas|alignof|asm|auto|bool|break|case|catch|char|char16_t|char32_t|char8_t|class|co_await|co_return|co_yield|compl|concept|const|const_cast|consteval|constexpr|constinit|continue|decltype|default|delete|do|double|dynamic_cast|else|enum|explicit|export|extern|final|float|for|friend|goto|if|import|inline|int|int16_t|int32_t|int64_t|int8_t|long|module|mutable|namespace|new|noexcept|nullptr|operator|override|private|protected|public|register|reinterpret_cast|requires|return|short|signed|sizeof|static|static_assert|static_cast|struct|switch|template|this|thread_local|throw|try|typedef|typeid|typename|uint16_t|uint32_t|uint64_t|uint8_t|union|unsigned|using|virtual|void|volatile|wchar_t|while)\b/; var modName = /\b(?!<keyword>)\w+(?:\s*\.\s*\w+)*\b/.source.replace(/<keyword>/g, function () { return keyword.source; }); Prism.languages.cpp = Prism.languages.extend('c', { 'class-name': [{ pattern: RegExp(/(\b(?:class|concept|enum|struct|typename)\s+)(?!<keyword>)\w+/.source.replace(/<keyword>/g, function () { return keyword.source; })), lookbehind: true }, // This is intended to capture the class name of method implementations like: // void foo::bar() const {} // However! The `foo` in the above example could also be a namespace, so we only capture the class name if // it starts with an uppercase letter. This approximation should give decent results. /\b[A-Z]\w*(?=\s*::\s*\w+\s*\()/, // This will capture the class name before destructors like: // Foo::~Foo() {} /\b[A-Z_]\w*(?=\s*::\s*~\w+\s*\()/i, // This also intends to capture the class name of method implementations but here the class has template // parameters, so it can't be a namespace (until C++ adds generic namespaces). /\b\w+(?=\s*<(?:[^<>]|<(?:[^<>]|<[^<>]*>)*>)*>\s*::\s*\w+\s*\()/], 'keyword': keyword, 'number': { pattern: /(?:\b0b[01']+|\b0x(?:[\da-f']+(?:\.[\da-f']*)?|\.[\da-f']+)(?:p[+-]?[\d']+)?|(?:\b[\d']+(?:\.[\d']*)?|\B\.[\d']+)(?:e[+-]?[\d']+)?)[ful]{0,4}/i, greedy: true }, 'operator': />>=?|<<=?|->|--|\+\+|&&|\|\||[?:~]|<=>|[-+*/%&|^!=<>]=?|\b(?:and|and_eq|bitand|bitor|not|not_eq|or|or_eq|xor|xor_eq)\b/, 'boolean': /\b(?:false|true)\b/ }); Prism.languages.insertBefore('cpp', 'string', { 'module': { // https://en.cppreference.com/w/cpp/language/modules pattern: RegExp(/(\b(?:import|module)\s+)/.source + '(?:' + // header-name /"(?:\\(?:\r\n|[\s\S])|[^"\\\r\n])*"|<[^<>\r\n]*>/.source + '|' + // module name or partition or both /<mod-name>(?:\s*:\s*<mod-name>)?|:\s*<mod-name>/.source.replace(/<mod-name>/g, function () { return modName; }) + ')'), lookbehind: true, greedy: true, inside: { 'string': /^[<"][\s\S]+/, 'operator': /:/, 'punctuation': /\./ } }, 'raw-string': { pattern: /R"([^()\\ ]{0,16})\([\s\S]*?\)\1"/, alias: 'string', greedy: true } }); Prism.languages.insertBefore('cpp', 'keyword', { 'generic-function': { pattern: /\b(?!operator\b)[a-z_]\w*\s*<(?:[^<>]|<[^<>]*>)*>(?=\s*\()/i, inside: { 'function': /^\w+/, 'generic': { pattern: /<[\s\S]+/, alias: 'class-name', inside: Prism.languages.cpp } } } }); Prism.languages.insertBefore('cpp', 'operator', { 'double-colon': { pattern: /::/, alias: 'punctuation' } }); Prism.languages.insertBefore('cpp', 'class-name', { // the base clause is an optional list of parent classes // https://en.cppreference.com/w/cpp/language/class 'base-clause': { pattern: /(\b(?:class|struct)\s+\w+\s*:\s*)[^;{}"'\s]+(?:\s+[^;{}"'\s]+)*(?=\s*[;{])/, lookbehind: true, greedy: true, inside: Prism.languages.extend('cpp', {}) } }); Prism.languages.insertBefore('inside', 'double-colon', { // All untokenized words that are not namespaces should be class names 'class-name': /\b[a-z_]\w*\b(?!\s*::)/i }, Prism.languages.cpp['base-clause']); })(prism); /* "prismjs/components/prism-css" */ (function (Prism) { var string = /(?:"(?:\\(?:\r\n|[\s\S])|[^"\\\r\n])*"|'(?:\\(?:\r\n|[\s\S])|[^'\\\r\n])*')/; Prism.languages.css = { 'comment': /\/\*[\s\S]*?\*\//, 'atrule': { pattern: /@[\w-](?:[^;{\s]|\s+(?![\s{]))*(?:;|(?=\s*\{))/, inside: { 'rule': /^@[\w-]+/, 'selector-function-argument': { pattern: /(\bselector\s*\(\s*(?![\s)]))(?:[^()\s]|\s+(?![\s)])|\((?:[^()]|\([^()]*\))*\))+(?=\s*\))/, lookbehind: true, alias: 'selector' }, 'keyword': { pattern: /(^|[^\w-])(?:and|not|only|or)(?![\w-])/, lookbehind: true } // See rest below } }, 'url': { // https://drafts.csswg.org/css-values-3/#urls pattern: RegExp('\\burl\\((?:' + string.source + '|' + /(?:[^\\\r\n()"']|\\[\s\S])*/.source + ')\\)', 'i'), greedy: true, inside: { 'function': /^url/i, 'punctuation': /^\(|\)$/, 'string': { pattern: RegExp('^' + string.source + '$'), alias: 'url' } } }, 'selector': { pattern: RegExp('(^|[{}\\s])[^{}\\s](?:[^{};"\'\\s]|\\s+(?![\\s{])|' + string.source + ')*(?=\\s*\\{)'), lookbehind: true }, 'string': { pa