Created
March 30, 2018 05:41
-
-
Save steveblue/e53f8fcd2866e87f23db3e78c7e6d7e9 to your computer and use it in GitHub Desktop.
Register javascript, css, and xml languages with hljs
This file contains hidden or 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
| hljs.registerLanguage('javascript', function (hljs) { | |
| return { | |
| aliases: ['js'], | |
| keywords: { | |
| keyword: | |
| 'in of if for while finally var new function do return void else break catch ' + | |
| 'instanceof with throw case default try this switch continue typeof delete ' + | |
| 'let yield const export super debugger as async await ' + | |
| // ECMAScript 6 modules import | |
| 'import from as' | |
| , | |
| literal: | |
| 'true false null undefined NaN Infinity', | |
| built_in: | |
| 'eval isFinite isNaN parseFloat parseInt decodeURI decodeURIComponent ' + | |
| 'encodeURI encodeURIComponent escape unescape Object Function Boolean Error ' + | |
| 'EvalError InternalError RangeError ReferenceError StopIteration SyntaxError ' + | |
| 'TypeError URIError Number Math Date String RegExp Array Float32Array ' + | |
| 'Float64Array Int16Array Int32Array Int8Array Uint16Array Uint32Array ' + | |
| 'Uint8Array Uint8ClampedArray ArrayBuffer DataView JSON Intl arguments require ' + | |
| 'module console window document Symbol Set Map WeakSet WeakMap Proxy Reflect ' + | |
| 'Promise' | |
| }, | |
| contains: [ | |
| { | |
| className: 'meta', | |
| relevance: 10, | |
| begin: /^\s*['"]use (strict|asm)['"]/ | |
| }, | |
| { | |
| className: 'meta', | |
| begin: /^#!/, end: /$/ | |
| }, | |
| hljs.APOS_STRING_MODE, | |
| hljs.QUOTE_STRING_MODE, | |
| { // template string | |
| className: 'string', | |
| begin: '`', end: '`', | |
| contains: [ | |
| hljs.BACKSLASH_ESCAPE, | |
| { | |
| className: 'subst', | |
| begin: '\\$\\{', end: '\\}' | |
| } | |
| ] | |
| }, | |
| hljs.C_LINE_COMMENT_MODE, | |
| hljs.C_BLOCK_COMMENT_MODE, | |
| { | |
| className: 'number', | |
| variants: [ | |
| { begin: '\\b(0[bB][01]+)' }, | |
| { begin: '\\b(0[oO][0-7]+)' }, | |
| { begin: hljs.C_NUMBER_RE } | |
| ], | |
| relevance: 0 | |
| }, | |
| { // "value" container | |
| begin: '(' + hljs.RE_STARTERS_RE + '|\\b(case|return|throw)\\b)\\s*', | |
| keywords: 'return throw case', | |
| contains: [ | |
| hljs.C_LINE_COMMENT_MODE, | |
| hljs.C_BLOCK_COMMENT_MODE, | |
| hljs.REGEXP_MODE, | |
| { // E4X / JSX | |
| begin: /</, end: />\s*[);\]]/, | |
| relevance: 0, | |
| subLanguage: 'xml' | |
| } | |
| ], | |
| relevance: 0 | |
| }, | |
| { | |
| className: 'function', | |
| beginKeywords: 'function', end: /\{/, excludeEnd: true, | |
| contains: [ | |
| hljs.inherit(hljs.TITLE_MODE, { begin: /[A-Za-z$_][0-9A-Za-z$_]*/ }), | |
| { | |
| className: 'params', | |
| begin: /\(/, end: /\)/, | |
| excludeBegin: true, | |
| excludeEnd: true, | |
| contains: [ | |
| hljs.C_LINE_COMMENT_MODE, | |
| hljs.C_BLOCK_COMMENT_MODE | |
| ] | |
| } | |
| ], | |
| illegal: /\[|%/ | |
| }, | |
| { | |
| begin: /\$[(.]/ // relevance booster for a pattern common to JS libs: `$(something)` and `$.something` | |
| }, | |
| { | |
| begin: '\\.' + hljs.IDENT_RE, relevance: 0 // hack: prevents detection of keywords after dots | |
| }, | |
| { // ES6 class | |
| className: 'class', | |
| beginKeywords: 'class', end: /[{;=]/, excludeEnd: true, | |
| illegal: /[:"\[\]]/, | |
| contains: [ | |
| { beginKeywords: 'extends' }, | |
| hljs.UNDERSCORE_TITLE_MODE | |
| ] | |
| }, | |
| { | |
| beginKeywords: 'constructor', end: /\{/, excludeEnd: true | |
| } | |
| ], | |
| illegal: /#(?!!)/ | |
| }; | |
| }); | |
| hljs.registerLanguage('css', function (hljs) { | |
| var IDENT_RE = '[a-zA-Z-][a-zA-Z0-9_-]*'; | |
| var RULE = { | |
| begin: /[A-Z\_\.\-]+\s*:/, returnBegin: true, end: ';', endsWithParent: true, | |
| contains: [ | |
| { | |
| className: 'attribute', | |
| begin: /\S/, end: ':', excludeEnd: true, | |
| starts: { | |
| endsWithParent: true, excludeEnd: true, | |
| contains: [ | |
| { | |
| begin: /[\w-]+\s*\(/, returnBegin: true, | |
| contains: [ | |
| { | |
| className: 'built_in', | |
| begin: /[\w-]+/ | |
| } | |
| ] | |
| }, | |
| hljs.CSS_NUMBER_MODE, | |
| hljs.QUOTE_STRING_MODE, | |
| hljs.APOS_STRING_MODE, | |
| hljs.C_BLOCK_COMMENT_MODE, | |
| { | |
| className: 'number', begin: '#[0-9A-Fa-f]+' | |
| }, | |
| { | |
| className: 'meta', begin: '!important' | |
| } | |
| ] | |
| } | |
| } | |
| ] | |
| }; | |
| return { | |
| case_insensitive: true, | |
| illegal: /[=\/|'\$]/, | |
| contains: [ | |
| hljs.C_BLOCK_COMMENT_MODE, | |
| { | |
| className: 'selector-id', begin: /#[A-Za-z0-9_-]+/ | |
| }, | |
| { | |
| className: 'selector-class', begin: /\.[A-Za-z0-9_-]+/ | |
| }, | |
| { | |
| className: 'selector-attr', | |
| begin: /\[/, end: /\]/, | |
| illegal: '$' | |
| }, | |
| { | |
| className: 'selector-pseudo', | |
| begin: /:(:)?[a-zA-Z0-9\_\-\+\(\)"'.]+/ | |
| }, | |
| { | |
| begin: '@(font-face|page)', | |
| lexemes: '[a-z-]+', | |
| keywords: 'font-face page' | |
| }, | |
| { | |
| begin: '@', end: '[{;]', // at_rule eating first "{" is a good thing | |
| // because it doesn’t let it to be parsed as | |
| // a rule set but instead drops parser into | |
| // the default mode which is how it should be. | |
| contains: [ | |
| { | |
| className: 'keyword', | |
| begin: /\S+/ | |
| }, | |
| { | |
| begin: /\s/, endsWithParent: true, excludeEnd: true, | |
| relevance: 0, | |
| contains: [ | |
| hljs.APOS_STRING_MODE, hljs.QUOTE_STRING_MODE, | |
| hljs.CSS_NUMBER_MODE | |
| ] | |
| } | |
| ] | |
| }, | |
| { | |
| className: 'selector-tag', begin: IDENT_RE, | |
| relevance: 0 | |
| }, | |
| { | |
| begin: '{', end: '}', | |
| illegal: /\S/, | |
| contains: [ | |
| hljs.C_BLOCK_COMMENT_MODE, | |
| RULE, | |
| ] | |
| } | |
| ] | |
| }; | |
| }); | |
| hljs.registerLanguage('xml', function (hljs) { | |
| var XML_IDENT_RE = '[A-Za-z0-9\\._:-]+'; | |
| var PHP = { | |
| begin: /<\?(php)?(?!\w)/, end: /\?>/, | |
| subLanguage: 'php' | |
| }; | |
| var TAG_INTERNALS = { | |
| endsWithParent: true, | |
| illegal: /</, | |
| relevance: 0, | |
| contains: [ | |
| PHP, | |
| { | |
| className: 'attr', | |
| begin: XML_IDENT_RE, | |
| relevance: 0 | |
| }, | |
| { | |
| begin: '=', | |
| relevance: 0, | |
| contains: [ | |
| { | |
| className: 'string', | |
| contains: [PHP], | |
| variants: [ | |
| { begin: /"/, end: /"/ }, | |
| { begin: /'/, end: /'/ }, | |
| { begin: /[^\s\/>]+/ } | |
| ] | |
| } | |
| ] | |
| } | |
| ] | |
| }; | |
| return { | |
| aliases: ['html', 'xhtml', 'rss', 'atom', 'xsl', 'plist'], | |
| case_insensitive: true, | |
| contains: [ | |
| { | |
| className: 'meta', | |
| begin: '<!DOCTYPE', end: '>', | |
| relevance: 10, | |
| contains: [{ begin: '\\[', end: '\\]' }] | |
| }, | |
| hljs.COMMENT( | |
| '<!--', | |
| '-->', | |
| { | |
| relevance: 10 | |
| } | |
| ), | |
| { | |
| begin: '<\\!\\[CDATA\\[', end: '\\]\\]>', | |
| relevance: 10 | |
| }, | |
| { | |
| className: 'tag', | |
| /* | |
| The lookahead pattern (?=...) ensures that 'begin' only matches | |
| '<style' as a single word, followed by a whitespace or an | |
| ending braket. The '$' is needed for the lexeme to be recognized | |
| by hljs.subMode() that tests lexemes outside the stream. | |
| */ | |
| begin: '<style(?=\\s|>|$)', end: '>', | |
| keywords: { name: 'style' }, | |
| contains: [TAG_INTERNALS], | |
| starts: { | |
| end: '</style>', returnEnd: true, | |
| subLanguage: ['css', 'xml'] | |
| } | |
| }, | |
| { | |
| className: 'tag', | |
| // See the comment in the <style tag about the lookahead pattern | |
| begin: '<script(?=\\s|>|$)', end: '>', | |
| keywords: { name: 'script' }, | |
| contains: [TAG_INTERNALS], | |
| starts: { | |
| end: '\<\/script\>', returnEnd: true, | |
| subLanguage: ['actionscript', 'javascript', 'handlebars', 'xml'] | |
| } | |
| }, | |
| PHP, | |
| { | |
| className: 'meta', | |
| begin: /<\?\w+/, end: /\?>/, | |
| relevance: 10 | |
| }, | |
| { | |
| className: 'tag', | |
| begin: '</?', end: '/?>', | |
| contains: [ | |
| { | |
| className: 'name', begin: /[^\/><\s]+/, relevance: 0 | |
| }, | |
| TAG_INTERNALS | |
| ] | |
| } | |
| ] | |
| }; | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment