15 |
16 |
17 |
18 |
19 | 4
20 | ≤
21 | x
22 | <
23 | 10
24 |
25 | "
26 |
27 |
28 |
29 |
30 |
31 | This element and attribute is here to show that speed-highlight is running.
32 |
33 |
34 |
35 |
36 | this.code = 'highlighted on a web worker';
11 | this.isAwesome = new Boolean(.5);
12 |
18 |
19 |
20 |
--------------------------------------------------------------------------------
/examples/worker.js:
--------------------------------------------------------------------------------
1 | import { highlightText } from '../src/index.js'
2 |
3 | onmessage = async e => {
4 | const result = await highlightText(e.data[0], e.data[1] || 'js');
5 | postMessage(result);
6 | };
7 |
--------------------------------------------------------------------------------
/src/common.js:
--------------------------------------------------------------------------------
1 | /**
2 | * Commonly used match pattern
3 | */
4 |
5 | export default {
6 | num: {
7 | type: 'num',
8 | match: /(\.e?|\b)\d(e-|[\d.oxa-fA-F_])*(\.|\b)/g
9 | },
10 | str: {
11 | type: 'str',
12 | match: /(["'])(\\[^]|(?!\1)[^\r\n\\])*\1?/g
13 | },
14 | strDouble: {
15 | type: 'str',
16 | match: /"((?!")[^\r\n\\]|\\[^])*"?/g
17 | }
18 | }
19 |
--------------------------------------------------------------------------------
/src/languages/asm.js:
--------------------------------------------------------------------------------
1 | export default [
2 | {
3 | type: 'cmnt',
4 | match: /(;|#).*/gm
5 | },
6 | {
7 | expand: 'str'
8 | },
9 | {
10 | expand: 'num'
11 | },
12 | {
13 | // value (ex: "$0x1")
14 | type: 'num',
15 | match: /\$[\da-fA-F]*\b/g
16 | },
17 | {
18 | type: 'kwd',
19 | // ex: "section .data"
20 | match: /^[a-z]+\s+[a-z.]+\b/gm,
21 | sub: [
22 | {
23 | // keyword (ex: "section")
24 | type: 'func',
25 | match: /^[a-z]+/g
26 | }
27 | ]
28 | },
29 | {
30 | // instruction (ex: "mov")
31 | type: 'kwd',
32 | match: /^\t*[a-z][a-z\d]*\b/gm,
33 | },
34 | {
35 | match: /%|\$/g,
36 | type: 'oper'
37 | }
38 | ]
39 |
--------------------------------------------------------------------------------
/src/languages/bash.js:
--------------------------------------------------------------------------------
1 | let variable = {
2 | type: 'var',
3 | match: /\$\w+|\${[^}]*}|\$\([^)]*\)/g
4 | };
5 |
6 | export default [
7 | {
8 | sub: 'todo',
9 | match: /#.*/g
10 | },
11 | {
12 | type: 'str',
13 | match: /(["'])((?!\1)[^\r\n\\]|\\[^])*\1?/g,
14 | sub: [ variable ]
15 | },
16 | {
17 | // relative or absolute path
18 | type: "oper",
19 | match: /(?<=\s|^)\.*\/[a-z/_.-]+/gi,
20 | },
21 | {
22 | type: 'kwd',
23 | match: /\s-[a-zA-Z]+|$<|[&|;]+|\b(unset|readonly|shift|export|if|fi|else|elif|while|do|done|for|until|case|esac|break|continue|exit|return|trap|wait|eval|exec|then|declare|enable|local|select|typeset|time|add|remove|install|update|delete)(?=\s|$)/g
24 | },
25 | {
26 | expand: 'num'
27 | },
28 | {
29 | // command
30 | type: 'func',
31 | match: /(?<=(^|\||\&\&|\;)\s*)[a-z_.-]+(?=\s|$)/gmi
32 | },
33 | {
34 | type: 'bool',
35 | match: /(?<=\s|^)(true|false)(?=\s|$)/g
36 | },
37 | // {
38 | // // function definition
39 | // type: 'func',
40 | // match: /(?<=\s|^)[a-z_]+(?=\s*\()/g
41 | // },
42 | {
43 | type: 'oper',
44 | match: /[=(){}<>!]+/g
45 | },
46 | {
47 | type: 'var',
48 | match: /(?<=\s|^)[\w_]+(?=\s*=)/g
49 | },
50 | variable
51 | ]
52 |
--------------------------------------------------------------------------------
/src/languages/bf.js:
--------------------------------------------------------------------------------
1 | export default [
2 | {
3 | match: /[^\[\->+.<\]\s].*/g,
4 | sub: 'todo'
5 | },
6 | {
7 | type: 'func',
8 | match: /\.+/g
9 | },
10 | {
11 | type: 'kwd',
12 | match: /[<>]+/g
13 | },
14 | {
15 | type: 'oper',
16 | match: /[+-]+/g
17 | }
18 | ]
19 |
--------------------------------------------------------------------------------
/src/languages/c.js:
--------------------------------------------------------------------------------
1 | export default [
2 | {
3 | match: /\/\/.*\n?|\/\*((?!\*\/)[^])*(\*\/)?/g,
4 | sub: 'todo'
5 | },
6 | {
7 | expand: 'str'
8 | },
9 | {
10 | expand: 'num'
11 | },
12 | {
13 | type: 'kwd',
14 | match: /#\s*include (<.*>|".*")/g,
15 | sub: [
16 | {
17 | type: 'str',
18 | match: /(<|").*/g
19 | }
20 | ]
21 | },
22 | {
23 | match: /asm\s*{[^}]*}/g,
24 | sub: [
25 | {
26 | type: 'kwd',
27 | match: /^asm/g
28 | },
29 | {
30 | //type: 'str',
31 | match: /[^{}]*(?=}$)/g,
32 | sub: 'asm'
33 | }
34 | ]
35 | },
36 | {
37 | type: 'kwd',
38 | match: /\*|&|#[a-z]+\b|\b(asm|auto|double|int|struct|break|else|long|switch|case|enum|register|typedef|char|extern|return|union|const|float|short|unsigned|continue|for|signed|void|default|goto|sizeof|volatile|do|if|static|while)\b/g
39 | },
40 | {
41 | type: 'oper',
42 | match: /[/*+:?&|%^~=!,<>.^-]+/g
43 | },
44 | {
45 | type: 'func',
46 | match: /[a-zA-Z_][\w_]*(?=\s*\()/g
47 | },
48 | {
49 | type: 'class',
50 | match: /\b[A-Z][\w_]*\b/g
51 | }
52 | ]
53 |
--------------------------------------------------------------------------------
/src/languages/css.js:
--------------------------------------------------------------------------------
1 | export default [
2 | {
3 | match: /\/\*((?!\*\/)[^])*(\*\/)?/g,
4 | sub: 'todo'
5 | },
6 | {
7 | expand: 'str'
8 | },
9 | {
10 | type: 'kwd',
11 | match: /@\w+\b|\b(and|not|only|or)\b|\b[a-z-]+(?=[^{}]*{)/g
12 | },
13 | {
14 | type: 'var',
15 | match: /\b[\w-]+(?=\s*:)|(::?|\.)[\w-]+(?=[^{}]*{)/g
16 | },
17 | {
18 | type: 'func',
19 | match: /#[\w-]+(?=[^{}]*{)/g
20 | },
21 | {
22 | type: 'num',
23 | match: /#[\da-f]{3,8}/g
24 | },
25 | {
26 | type: 'num',
27 | match: /\d+(\.\d+)?(cm|mm|in|px|pt|pc|em|ex|ch|rem|vm|vh|vmin|vmax|%)?/g,
28 | sub: [
29 | {
30 | type: 'var',
31 | match: /[a-z]+|%/g
32 | }
33 | ]
34 | },
35 | {
36 | match: /url\([^)]*\)/g,
37 | sub: [
38 | {
39 | type: 'func',
40 | match: /url(?=\()/g
41 | },
42 | {
43 | type: 'str',
44 | match: /[^()]+/g
45 | }
46 | ]
47 | },
48 | {
49 | type: 'func',
50 | match: /\b[a-zA-Z]\w*(?=\s*\()/g
51 | },
52 | {
53 | type: 'num',
54 | match: /\b[a-z-]+\b/g
55 | }
56 | ]
57 |
--------------------------------------------------------------------------------
/src/languages/csv.js:
--------------------------------------------------------------------------------
1 | export default [
2 | {
3 | expand: 'strDouble'
4 | },
5 | {
6 | type: 'oper',
7 | match: /,/g
8 | }
9 | ]
10 |
--------------------------------------------------------------------------------
/src/languages/diff.js:
--------------------------------------------------------------------------------
1 | export default [
2 | {
3 | type: 'deleted',
4 | match: /^[-<].*/gm
5 | },
6 | {
7 | type: 'insert',
8 | match: /^[+>].*/gm
9 | },
10 | {
11 | type: 'kwd',
12 | match: /!.*/gm
13 | },
14 | {
15 | type: 'section',
16 | match: /^@@.*@@$|^\d.*|^([*-+])\1\1.*/gm
17 | }
18 | ]
19 |
--------------------------------------------------------------------------------
/src/languages/docker.js:
--------------------------------------------------------------------------------
1 | import bash from "./bash.js"
2 |
3 | export default [
4 | {
5 | type: 'kwd',
6 | match: /^(FROM|RUN|CMD|LABEL|MAINTAINER|EXPOSE|ENV|ADD|COPY|ENTRYPOINT|VOLUME|USER|WORKDIR|ARG|ONBUILD|STOPSIGNAL|HEALTHCHECK|SHELL)\b/gmi
7 | },
8 | ...bash
9 | ]
--------------------------------------------------------------------------------
/src/languages/git.js:
--------------------------------------------------------------------------------
1 | import diff from './diff.js'
2 |
3 | export default [
4 | {
5 | match: /^#.*/gm,
6 | sub: 'todo'
7 | },
8 | {
9 | expand: 'str'
10 | },
11 | ...diff,
12 | {
13 | type: 'func',
14 | match: /^(\$ )?git(\s.*)?$/gm
15 | },
16 | {
17 | type: 'kwd',
18 | match: /^commit \w+$/gm
19 | }
20 | ]
21 |
--------------------------------------------------------------------------------
/src/languages/go.js:
--------------------------------------------------------------------------------
1 | export default [
2 | {
3 | match: /\/\/.*\n?|\/\*((?!\*\/)[^])*(\*\/)?/g,
4 | sub: 'todo'
5 | },
6 | {
7 | expand: 'str'
8 | },
9 | {
10 | expand: 'num'
11 | },
12 | {
13 | type: 'kwd',
14 | match: /\*|&|\b(break|case|chan|const|continue|default|defer|else|fallthrough|for|func|go|goto|if|import|interface|map|package|range|return|select|struct|switch|type|var)\b/g
15 | },
16 | {
17 | type: 'func',
18 | match: /[a-zA-Z_][\w_]*(?=\s*\()/g
19 | },
20 | {
21 | type: 'class',
22 | match: /\b[A-Z][\w_]*\b/g
23 | },
24 | {
25 | type: 'oper',
26 | match: /[+\-*\/%&|^~=!<>.^-]+/g
27 | }
28 | ]
--------------------------------------------------------------------------------
/src/languages/html.js:
--------------------------------------------------------------------------------
1 | import xml, { properties, xmlElement } from './xml.js'
2 |
3 | export default [
4 | {
5 | type: 'class',
6 | match: /])*>/gi,
7 | sub: [
8 | {
9 | type: 'str',
10 | match: /"[^"]*"|'[^']*'/g
11 | },
12 | {
13 | type: 'oper',
14 | match: /^$/g
15 | },
16 | {
17 | type: 'var',
18 | match: /DOCTYPE/gi
19 | }
20 | ]
21 | },
22 | {
23 | match: RegExp(`)[^])*`, 'g'),
24 | sub: [
25 | {
26 | match: RegExp(`^$)`, 'g'),
31 | sub: 'css'
32 | },
33 | xmlElement
34 | ]
35 | },
36 | {
37 | match: RegExp(`)[^])*`, 'g'),
38 | sub: [
39 | {
40 | match: RegExp(`^$)`, 'g'),
45 | sub: 'js'
46 | },
47 | xmlElement
48 | ]
49 | },
50 | ...xml
51 | ]
52 |
--------------------------------------------------------------------------------
/src/languages/http.js:
--------------------------------------------------------------------------------
1 | import { detectLanguage } from '../detect.js'
2 |
3 | export default [
4 | {
5 | type: 'kwd',
6 | match: /^(GET|HEAD|POST|PUT|DELETE|CONNECT|OPTIONS|TRACE|PATCH|PRI|SEARCH)\b/gm
7 | },
8 | {
9 | expand: 'str'
10 | },
11 | {
12 | type: 'section',
13 | match: /\bHTTP\/[\d.]+\b/g
14 | },
15 | {
16 | expand: 'num'
17 | },
18 | {
19 | type: 'oper',
20 | match: /[,;:=]/g
21 | },
22 | {
23 | type: 'var',
24 | match: /[a-zA-Z][\w-]*(?=:)/g
25 | },
26 | {
27 | match: /\n\n[^]*/g,
28 | sub: detectLanguage
29 | }
30 | ]
31 |
--------------------------------------------------------------------------------
/src/languages/ini.js:
--------------------------------------------------------------------------------
1 | export default [
2 | {
3 | match: /(^[ \f\t\v]*)[#;].*/gm,
4 | sub: 'todo'
5 | },
6 | {
7 | type: 'str',
8 | match: /.*/g
9 | },
10 | {
11 | type: 'var',
12 | match: /.*(?==)/g
13 | },
14 | {
15 | type: 'section',
16 | match: /^\s*\[.+\]\s*$/gm
17 | },
18 | {
19 | type: 'oper',
20 | match: /=/g
21 | }
22 | ]
23 |
--------------------------------------------------------------------------------
/src/languages/java.js:
--------------------------------------------------------------------------------
1 | export default [
2 | {
3 | match: /\/\/.*\n?|\/\*((?!\*\/)[^])*(\*\/)?/g,
4 | sub: 'todo'
5 | },
6 | {
7 | expand: 'str'
8 | },
9 | {
10 | expand: 'num'
11 | },
12 | {
13 | type: 'kwd',
14 | match: /\b(abstract|assert|boolean|break|byte|case|catch|char|class|continue|const|default|do|double|else|enum|exports|extends|final|finally|float|for|goto|if|implements|import|instanceof|int|interface|long|module|native|new|package|private|protected|public|requires|return|short|static|strictfp|super|switch|synchronized|this|throw|throws|transient|try|var|void|volatile|while)\b/g
15 | },
16 | {
17 | type: 'oper',
18 | match: /[/*+:?&|%^~=!,<>.^-]+/g
19 | },
20 | {
21 | type: 'func',
22 | match: /[a-zA-Z_][\w_]*(?=\s*\()/g
23 | },
24 | {
25 | type: 'class',
26 | match: /\b[A-Z][\w_]*\b/g
27 | }
28 | ]
--------------------------------------------------------------------------------
/src/languages/js.js:
--------------------------------------------------------------------------------
1 | export default [
2 | {
3 | match: /\/\*\*((?!\*\/)[^])*(\*\/)?/g,
4 | sub: 'jsdoc'
5 | },
6 | {
7 | match: /\/\/.*\n?|\/\*((?!\*\/)[^])*(\*\/)?/g,
8 | sub: 'todo'
9 | },
10 | {
11 | expand: 'str'
12 | },
13 | {
14 | match: /`((?!`)[^]|\\[^])*`?/g,
15 | sub: 'js_template_literals'
16 | },
17 | {
18 | type: 'kwd',
19 | match: /=>|\b(this|set|get|as|async|await|break|case|catch|class|const|constructor|continue|debugger|default|delete|do|else|enum|export|extends|finally|for|from|function|if|implements|import|in|instanceof|interface|let|var|of|new|package|private|protected|public|return|static|super|switch|throw|throws|try|typeof|void|while|with|yield)\b/g
20 | },
21 | {
22 | match: /\/((?!\/)[^\r\n\\]|\\.)+\/[dgimsuy]*/g,
23 | sub: 'regex'
24 | },
25 | {
26 | expand: 'num'
27 | },
28 | {
29 | type: 'num',
30 | match: /\b(NaN|null|undefined|[A-Z][A-Z_]*)\b/g
31 | },
32 | {
33 | type: 'bool',
34 | match: /\b(true|false)\b/g
35 | },
36 | {
37 | type: 'oper',
38 | match: /[/*+:?&|%^~=!,<>.^-]+/g
39 | },
40 | {
41 | type: 'class',
42 | match: /\b[A-Z][\w_]*\b/g
43 | },
44 | {
45 | type: 'func',
46 | match: /[a-zA-Z$_][\w$_]*(?=\s*((\?\.)?\s*\(|=\s*(\(?[\w,{}\[\])]+\)? =>|function\b)))/g
47 | }
48 | ]
49 |
--------------------------------------------------------------------------------
/src/languages/js_template_literals.js:
--------------------------------------------------------------------------------
1 | export default [
2 | {
3 | match: new class {
4 | exec(str) {
5 | let i = this.lastIndex,
6 | j,
7 | f = _ => {
8 | while (++i < str.length - 2)
9 | if (str[i] == '{') f();
10 | else if (str[i] == '}') return;
11 | };
12 | for (; i < str.length; ++i)
13 | if (str[i - 1] != '\\' && str[i] == '$' && str[i + 1] == '{') {
14 | j = i++;
15 | f(i);
16 | this.lastIndex = i + 1;
17 | return { index: j, 0: str.slice(j, i + 1) };
18 | }
19 | return null;
20 | }
21 | }(),
22 | sub: [
23 | {
24 | type: 'kwd',
25 | match: /^\${|}$/g
26 | },
27 | {
28 | match: /(?!^\$|{)[^]+(?=}$)/g,
29 | sub: 'js'
30 | },
31 | ],
32 | },
33 | ];
34 | export let type = 'str';
35 |
--------------------------------------------------------------------------------
/src/languages/jsdoc.js:
--------------------------------------------------------------------------------
1 | import todo from './todo.js';
2 |
3 | export default [
4 | {
5 | type: 'kwd',
6 | match: /@\w+/g
7 | },
8 | {
9 | type: 'class',
10 | match: /{[\w\s|<>,.@\[\]]+}/g
11 | },
12 | {
13 | type: 'var',
14 | match: /\[[\w\s="']+\]/g
15 | },
16 | ...todo
17 | ];
18 | export let type = 'cmnt';
19 |
--------------------------------------------------------------------------------
/src/languages/json.js:
--------------------------------------------------------------------------------
1 | export default [
2 | {
3 | type: 'var',
4 | match: /("|')?[a-zA-Z]\w*\1(?=\s*:)/g
5 | },
6 | {
7 | expand: 'str'
8 | },
9 | {
10 | expand: 'num'
11 | },
12 | {
13 | type: 'num',
14 | match: /\bnull\b/g
15 | },
16 | {
17 | type: 'bool',
18 | match: /\b(true|false)\b/g
19 | }
20 | ]
21 |
--------------------------------------------------------------------------------
/src/languages/leanpub-md.js:
--------------------------------------------------------------------------------
1 | import md from './md.js'
2 | import { detectLanguage } from '../detect.js'
3 |
4 | export default [
5 | {
6 | type: 'insert',
7 | match: /(leanpub-start-insert)((?!leanpub-end-insert)[^])*(leanpub-end-insert)?/g,
8 | sub: [
9 | {
10 | type: 'insert',
11 | match: /leanpub-(start|end)-insert/g
12 | },
13 | {
14 | match: /(?!leanpub-start-insert)((?!leanpub-end-insert)[^])*/g,
15 | sub: detectLanguage
16 | }
17 | ]
18 | },
19 | {
20 | type: 'deleted',
21 | match: /(leanpub-start-delete)((?!leanpub-end-delete)[^])*(leanpub-end-delete)?/g,
22 | sub: [
23 | {
24 | type: 'deleted',
25 | match: /leanpub-(start|end)-delete/g
26 | },
27 | {
28 | match: /(?!leanpub-start-delete)((?!leanpub-end-delete)[^])*/g,
29 | sub: detectLanguage
30 | }
31 | ]
32 | },
33 | ...md
34 | ]
35 |
--------------------------------------------------------------------------------
/src/languages/log.js:
--------------------------------------------------------------------------------
1 | export default [
2 | {
3 | type: 'cmnt',
4 | match: /^#.*/gm
5 | },
6 | {
7 | expand: 'strDouble'
8 | },
9 | {
10 | expand: 'num'
11 | },
12 | {
13 | type: 'err',
14 | match: /\b(err(or)?|[a-z_-]*exception|warn|warning|failed|ko|invalid|not ?found|alert|fatal)\b/gi
15 | },
16 | {
17 | type: 'num',
18 | match: /\b(null|undefined)\b/gi
19 | },
20 | {
21 | type: 'bool',
22 | match: /\b(false|true|yes|no)\b/gi
23 | },
24 | {
25 | type: 'oper',
26 | match: /\.|,/g
27 | }
28 | ]
29 |
--------------------------------------------------------------------------------
/src/languages/lua.js:
--------------------------------------------------------------------------------
1 | export default [
2 | {
3 | match: /^#!.*|--(\[(=*)\[((?!--\]\2\])[^])*--\]\2\]|.*)/g,
4 | sub: 'todo'
5 | },
6 | {
7 | expand: 'str'
8 | },
9 | {
10 | type: 'kwd',
11 | match: /\b(and|break|do|else|elseif|end|for|function|if|in|local|not|or|repeat|return|then|until|while)\b/g
12 | },
13 | {
14 | type: 'bool',
15 | match: /\b(true|false|nil)\b/g
16 | },
17 | {
18 | type: 'oper',
19 | match: /[+*/%^#=~<>:,.-]+/g
20 | },
21 | {
22 | expand: 'num'
23 | },
24 | {
25 | type: 'func',
26 | match: /[a-z_]+(?=\s*[({])/g
27 | }
28 | ]
29 |
--------------------------------------------------------------------------------
/src/languages/make.js:
--------------------------------------------------------------------------------
1 | export default [
2 | {
3 | match: /^\s*#.*/gm,
4 | sub: 'todo'
5 | },
6 | {
7 | expand: 'str'
8 | },
9 | {
10 | type: 'oper',
11 | match: /[${}()]+/g
12 | },
13 | {
14 | type: 'class',
15 | match: /.PHONY:/gm
16 | },
17 | {
18 | type: 'section',
19 | match: /^[\w.]+:/gm
20 | },
21 | {
22 | type: 'kwd',
23 | match: /\b(ifneq|endif)\b/g
24 | },
25 | {
26 | expand: 'num'
27 | },
28 | {
29 | type: 'var',
30 | match: /[A-Z_]+(?=\s*=)/g
31 | },
32 | {
33 | match: /^.*$/gm,
34 | sub: 'bash'
35 | }
36 | ]
37 |
--------------------------------------------------------------------------------
/src/languages/md.js:
--------------------------------------------------------------------------------
1 | import { detectLanguage } from '../detect.js'
2 |
3 | export default [
4 | {
5 | type: 'cmnt',
6 | match: /^>.*|(=|-)\1+/gm
7 | },
8 | {
9 | type: 'class',
10 | match: /\*\*((?!\*\*).)*\*\*/g
11 | },
12 | {
13 | match: /```((?!```)[^])*\n```/g,
14 | sub: code => ({
15 | type: 'kwd',
16 | sub: [
17 | {
18 | match: /\n[^]*(?=```)/g,
19 | sub: code.split('\n')[0].slice(3) || detectLanguage(code)
20 | }
21 | ]
22 | })
23 | },
24 | {
25 | type: 'str',
26 | match: /`[^`]*`/g
27 | },
28 | {
29 | type: 'var',
30 | match: /~~((?!~~).)*~~/g
31 | },
32 | {
33 | type: 'kwd',
34 | match: /_[^_]*_|\*[^*]*\*/g
35 | },
36 | {
37 | type: 'kwd',
38 | match: /^\s*(\*|\d+\.)\s/gm
39 | },
40 | {
41 | type: 'oper',
42 | match: /\[[^\]]*]/g
43 | },
44 | {
45 | type: 'func',
46 | match: /\([^)]*\)/g
47 | }
48 | ]
49 |
--------------------------------------------------------------------------------
/src/languages/pl.js:
--------------------------------------------------------------------------------
1 | export default [
2 | {
3 | match: /#.*/g,
4 | sub: 'todo'
5 | },
6 | {
7 | type: 'str',
8 | match: /(["'])(\\[^]|(?!\1)[^])*\1?/g
9 | },
10 | {
11 | expand: 'num'
12 | },
13 | {
14 | type: 'kwd',
15 | match: /\b(any|break|continue|default|delete|die|do|else|elsif|eval|for|foreach|given|goto|if|last|local|my|next|our|package|print|redo|require|return|say|state|sub|switch|undef|unless|until|use|when|while|not|and|or|xor)\b/g
16 | },
17 | {
18 | type: 'oper',
19 | match: /[-+*/%~!&<>|=?,]+/g
20 | },
21 | {
22 | type: 'func',
23 | match: /[a-z_]+(?=\s*\()/g
24 | }
25 | ]
26 |
--------------------------------------------------------------------------------
/src/languages/plain.js:
--------------------------------------------------------------------------------
1 | export default [
2 | {
3 | expand: 'strDouble'
4 | }
5 | ]
6 |
--------------------------------------------------------------------------------
/src/languages/py.js:
--------------------------------------------------------------------------------
1 | export default [
2 | {
3 | match: /#.*/g,
4 | sub: 'todo'
5 | },
6 | {
7 | match: /("""|''')(\\[^]|(?!\1)[^])*\1?/g,
8 | sub: 'todo'
9 | },
10 | {
11 | type: 'str',
12 | match: /f("|')(\\[^]|(?!\1).)*\1?|f((["'])\4\4)(\\[^]|(?!\3)[^])*\3?/gi,
13 | sub: [
14 | {
15 | type: 'var',
16 | match: /{[^{}]*}/g,
17 | sub: [
18 | {
19 | match: /(?!^{)[^]*(?=}$)/g,
20 | sub: 'py'
21 | }
22 | ]
23 | }
24 | ]
25 | },
26 | {
27 | expand: 'str'
28 | },
29 | {
30 | type: 'kwd',
31 | match: /\b(and|as|assert|break|class|continue|def|del|elif|else|except|finally|for|from|global|if|import|in|is|lambda|nonlocal|not|or|pass|raise|return|try|while|with|yield)\b/g
32 | },
33 | {
34 | type: 'bool',
35 | match: /\b(False|True|None)\b/g
36 | },
37 | {
38 | expand: 'num'
39 | },
40 | {
41 | type: 'func',
42 | match: /[a-z_]+(?=\s*\()/g
43 | },
44 | {
45 | type: 'oper',
46 | match: /[-/*+<>,=!&|^%]+/g
47 | },
48 | {
49 | type: 'class',
50 | match: /\b[A-Z][\w_]*\b/g
51 | }
52 | ]
53 |
--------------------------------------------------------------------------------
/src/languages/regex.js:
--------------------------------------------------------------------------------
1 | export default [
2 | {
3 | match: /^(?!\/).*/gm,
4 | sub: 'todo'
5 | },
6 | {
7 | type: 'num',
8 | match: /\[((?!\])[^\\]|\\.)*\]/g
9 | },
10 | {
11 | type: 'kwd',
12 | match: /\||\^|\$|\\.|\w+($|\r|\n)/g
13 | },
14 | {
15 | type: 'var',
16 | match: /\*|\+|\{\d+,\d+\}/g
17 | }
18 | ];
19 | export let type = 'oper';
20 |
--------------------------------------------------------------------------------
/src/languages/rs.js:
--------------------------------------------------------------------------------
1 | export default [
2 | {
3 | match: /\/\/.*\n?|\/\*((?!\*\/)[^])*(\*\/)?/g,
4 | sub: 'todo'
5 | },
6 | {
7 | expand: 'str'
8 | },
9 | {
10 | expand: 'num'
11 | },
12 | {
13 | type: 'kwd',
14 | match: /\b(as|break|const|continue|crate|else|enum|extern|false|fn|for|if|impl|in|let|loop|match|mod|move|mut|pub|ref|return|self|Self|static|struct|super|trait|true|type|unsafe|use|where|while|async|await|dyn|abstract|become|box|do|final|macro|override|priv|typeof|unsized|virtual|yield|try)\b/g
15 | },
16 | {
17 | type: 'oper',
18 | match: /[/*+:?&|%^~=!,<>.^-]+/g
19 | },
20 | {
21 | type: 'class',
22 | match: /\b[A-Z][\w_]*\b/g
23 | },
24 | {
25 | type: 'func',
26 | match: /[a-zA-Z_][\w_]*(?=\s*!?\s*\()/g
27 | }
28 | ]
--------------------------------------------------------------------------------
/src/languages/todo.js:
--------------------------------------------------------------------------------
1 | export default [
2 | {
3 | type: 'err',
4 | match: /\b(TODO|FIXME|DEBUG|OPTIMIZE|WARNING|XXX|BUG)\b/g
5 | },
6 | {
7 | type: 'class',
8 | match: /\bIDEA\b/g
9 | },
10 | {
11 | type: 'insert',
12 | match: /\b(CHANGED|FIX|CHANGE)\b/g
13 | },
14 | {
15 | type: 'oper',
16 | match: /\bQUESTION\b/g
17 | }
18 | ];
19 | export let type = 'cmnt';
20 |
--------------------------------------------------------------------------------
/src/languages/toml.js:
--------------------------------------------------------------------------------
1 | export default [
2 | {
3 | match: /#.*/g,
4 | sub: 'todo'
5 | },
6 | {
7 | type: 'str',
8 | match: /("""|''')((?!\1)[^]|\\[^])*\1?/g
9 | },
10 | {
11 | expand: 'str'
12 | },
13 | {
14 | type: 'section',
15 | match: /^\[.+\]\s*$/gm
16 | },
17 | {
18 | type: 'num',
19 | match: /\b(inf|nan)\b|\d[\d:ZT.-]*/g
20 | },
21 | {
22 | expand: 'num'
23 | },
24 | {
25 | type: 'bool',
26 | match: /\b(true|false)\b/g
27 | },
28 | {
29 | type: 'oper',
30 | match: /[+,.=-]/g
31 | },
32 | {
33 | type: 'var',
34 | match: /\w+(?= \=)/g
35 | }
36 | ]
37 |
--------------------------------------------------------------------------------
/src/languages/ts.js:
--------------------------------------------------------------------------------
1 | import js from './js.js'
2 |
3 | export default [
4 | {
5 | type: 'type',
6 | match: /:\s*(any|void|number|boolean|string|object|never|enum)\b/g
7 | },
8 | {
9 | type: 'kwd',
10 | match: /\b(type|namespace|typedef|interface|public|private|protected|implements|declare|abstract|readonly)\b/g
11 | },
12 | ...js
13 | ]
14 |
--------------------------------------------------------------------------------
/src/languages/uri.js:
--------------------------------------------------------------------------------
1 | export default [
2 | {
3 | match: /^#.*/gm,
4 | sub: 'todo'
5 | },
6 | {
7 | type: 'class',
8 | match: /^\w+(?=:?)/gm
9 | },
10 | {
11 | type: 'num',
12 | match: /:\d+/g
13 | },
14 | {
15 | type: 'oper',
16 | match: /[:/&?]|\w+=/g
17 | },
18 | {
19 | type: 'func',
20 | match: /[.\w]+@|#[\w]+$/gm
21 | },
22 | {
23 | type: 'var',
24 | match: /\w+\.\w+(\.\w+)*/g
25 | }
26 | ]
27 |
--------------------------------------------------------------------------------
/src/languages/xml.js:
--------------------------------------------------------------------------------
1 | let
2 | nameStartChar = ":A-Z_a-z\u{C0}-\u{D6}\u{D8}-\u{F6}\u{F8}-\u{2FF}\u{370}-\u{37D}\u{37F}-\u{1FFF}\u{200C}-\u{200D}\u{2070}-\u{218F}\u{2C00}-\u{2FEF}\u{3001}-\u{D7FF}\u{F900}-\u{FDCF}\u{FDF0}-\u{FFFD}",
3 | nameChar = nameStartChar + "\\-\\.0-9\u{B7}\u{0300}-\u{036F}\u{203F}-\u{2040}";
4 | export let
5 | name = `[${nameStartChar}][${nameChar}]*`,
6 | properties = `\\s*(\\s+${name}\\s*(=\\s*([^"']\\S*|("|')(\\\\[^]|(?!\\4)[^])*\\4?)?)?\\s*)*`,
7 | xmlElement = {
8 | match: RegExp(`<[\/!?]?${name}${properties}[\/!?]?>`, 'g'),
9 | sub: [
10 | {
11 | type: 'var',
12 | match: RegExp(`^<[\/!?]?${name}`, 'g'),
13 | sub: [
14 | {
15 | type: 'oper',
16 | match: /^<[\/!?]?/g
17 | }
18 | ]
19 | },
20 | {
21 | type: 'str',
22 | match: /=\s*([^"']\S*|("|')(\\[^]|(?!\2)[^])*\2?)/g,
23 | sub: [
24 | {
25 | type: 'oper',
26 | match: /^=/g
27 | }
28 | ]
29 | },
30 | {
31 | type: 'oper',
32 | match: /[\/!?]?>/g
33 | },
34 | {
35 | type: 'class',
36 | match: RegExp(name, 'g')
37 | }
38 | ]
39 | };
40 |
41 | export default [
42 | {
43 | match: /)[^])*-->/g,
44 | sub: 'todo'
45 | },
46 | {
47 | type: 'class',
48 | match: //gi
49 | },
50 | xmlElement,
51 | // https://github.com/speed-highlight/core/issues/49
52 | {
53 | type: 'str',
54 | match: RegExp(`<\\?${name}([^?]|\\?[^?>])*\\?+>`, 'g'),
55 | sub: [
56 | {
57 | type: 'var',
58 | match: RegExp(`^<\\?${name}`, 'g'),
59 | sub: [
60 | {
61 | type: 'oper',
62 | match: /^<\?/g
63 | }
64 | ]
65 | },
66 | {
67 | type: 'oper',
68 | match: /\?+>$/g
69 | }
70 | ]
71 | },
72 | {
73 | type: 'var',
74 | match: /&(#x?)?[\da-z]{1,8};/gi
75 | }
76 | ]
77 |
--------------------------------------------------------------------------------
/src/languages/yaml.js:
--------------------------------------------------------------------------------
1 | export default [
2 | {
3 | match: /#.*/g,
4 | sub: 'todo'
5 | },
6 | {
7 | expand: 'str'
8 | },
9 | {
10 | type: 'str',
11 | match: /(>|\|)\r?\n((\s[^\n]*)?(\r?\n|$))*/g
12 | },
13 | {
14 | type: 'type',
15 | match: /!![a-z]+/g
16 | },
17 | {
18 | type: 'bool',
19 | match: /\b(Yes|No)\b/g
20 | },
21 | {
22 | type: 'oper',
23 | match: /[+:-]/g
24 | },
25 | {
26 | expand: 'num'
27 | },
28 | {
29 | type: 'var',
30 | match: /[a-zA-Z]\w*(?=:)/g
31 | }
32 | ]
33 |
--------------------------------------------------------------------------------
/src/terminal.js:
--------------------------------------------------------------------------------
1 | /**
2 | * @module terminal
3 | * (Terminal adaptor)
4 | */
5 |
6 | /**
7 | * @typedef {import('./index.js').ShjLanguage} ShjLanguage
8 | */
9 |
10 | /**
11 | * Languages supported
12 | * @typedef {('default'|'atom-dark')} ShjTerminalTheme
13 | */
14 |
15 | import { tokenize } from './index.js';
16 |
17 | let theme = import('./themes/default.js');
18 |
19 | /**
20 | * Highlight a string passed as argument and return a string that can directly be printed
21 | *
22 | * @async
23 | * @function highlightText
24 | * @param {string} src The code
25 | * @param {ShjLanguage} lang The language of the code
26 | * @returns {Promise