@speed-highlight/core 1.1.7 → 1.1.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +56 -9
- package/dist/color.js +1 -0
- package/dist/detect.js +1 -1
- package/dist/index.js +3 -3
- package/dist/languages/c.js +1 -1
- package/dist/languages/go.js +1 -1
- package/dist/languages/http.js +1 -1
- package/dist/languages/java.js +1 -1
- package/dist/languages/js.js +1 -1
- package/dist/languages/leanpub-md.js +1 -1
- package/dist/languages/md.js +2 -2
- package/dist/languages/py.js +1 -1
- package/dist/languages/rs.js +1 -1
- package/dist/languages/ts.js +1 -1
- package/dist/node/color.js +1 -0
- package/dist/node/common.js +1 -1
- package/dist/node/detect.js +1 -1
- package/dist/node/index.js +3 -3
- package/dist/node/languages/asm.js +1 -1
- package/dist/node/languages/bash.js +1 -1
- package/dist/node/languages/bf.js +1 -1
- package/dist/node/languages/c.js +1 -1
- package/dist/node/languages/css.js +1 -1
- package/dist/node/languages/csv.js +1 -1
- package/dist/node/languages/diff.js +1 -1
- package/dist/node/languages/docker.js +1 -1
- package/dist/node/languages/git.js +1 -1
- package/dist/node/languages/go.js +1 -1
- package/dist/node/languages/html.js +1 -1
- package/dist/node/languages/http.js +1 -1
- package/dist/node/languages/ini.js +1 -1
- package/dist/node/languages/java.js +1 -1
- package/dist/node/languages/js.js +1 -1
- package/dist/node/languages/js_template_literals.js +1 -1
- package/dist/node/languages/jsdoc.js +1 -1
- package/dist/node/languages/json.js +1 -1
- package/dist/node/languages/leanpub-md.js +2 -2
- package/dist/node/languages/log.js +1 -1
- package/dist/node/languages/lua.js +1 -1
- package/dist/node/languages/make.js +1 -1
- package/dist/node/languages/md.js +2 -2
- package/dist/node/languages/pl.js +1 -1
- package/dist/node/languages/plain.js +1 -1
- package/dist/node/languages/py.js +1 -1
- package/dist/node/languages/regex.js +1 -1
- package/dist/node/languages/rs.js +1 -1
- package/dist/node/languages/sql.js +1 -1
- package/dist/node/languages/todo.js +1 -1
- package/dist/node/languages/toml.js +1 -1
- package/dist/node/languages/ts.js +1 -1
- package/dist/node/languages/uri.js +1 -1
- package/dist/node/languages/xml.js +1 -1
- package/dist/node/languages/yaml.js +1 -1
- package/dist/node/terminal.js +1 -0
- package/dist/node/themes/atom-dark.js +1 -0
- package/dist/node/themes/default.js +1 -1
- package/dist/terminal.js +1 -0
- package/dist/themes/atom-dark.js +1 -0
- package/dist/themes/default.js +1 -1
- package/doc/common.js.html +87 -0
- package/doc/css/jsdoc-template.css +1 -0
- package/doc/css/tomorrow.min.css +2 -0
- package/doc/detect%0ALanguage%20detectormodule_.html +309 -0
- package/doc/detect.js.html +113 -0
- package/doc/global.html +1826 -0
- package/doc/index%0ABase%20scriptmodule_.html +1179 -0
- package/doc/index.html +402 -0
- package/doc/index.js.html +209 -0
- package/doc/js/jsdoc-template.js +2 -0
- package/doc/js/jsdoc-template.js.map +1 -0
- package/doc/js/prettify.min.js +47 -0
- package/doc/term%0ATerminal%20adaptormodule_.html +596 -0
- package/doc/term.js.html +108 -0
- package/examples/{helper/data.js → data.js} +28 -16
- package/examples/deno.js +53 -7
- package/examples/index.html +10 -7
- package/examples/languages/test.c +1 -1
- package/examples/languages/test.go +4 -4
- package/examples/languages/test.java +3 -3
- package/examples/node.js +1 -1
- package/jsdoc.json +16 -0
- package/package.json +42 -38
- package/src/color.js +11 -0
- package/src/detect.js +21 -5
- package/src/index.js +44 -38
- package/src/languages/c.js +1 -1
- package/src/languages/go.js +1 -1
- package/src/languages/java.js +1 -1
- package/src/languages/js.js +1 -1
- package/src/languages/py.js +1 -1
- package/src/languages/rs.js +1 -1
- package/src/terminal.js +42 -0
- package/src/themes/atom-dark.js +18 -0
- package/src/themes/default.js +16 -24
- package/typedef.js +34 -0
- package/assets/screenshot.png +0 -0
- package/dist/node/term.js +0 -1
- package/dist/term.js +0 -1
- package/examples/dist.html +0 -26
- package/src/term.js +0 -19
|
@@ -1,51 +1,63 @@
|
|
|
1
|
+
import '../typedef.js'
|
|
2
|
+
|
|
3
|
+
/** @type {ShjLanguage[]} */
|
|
1
4
|
export const languages = [
|
|
5
|
+
'js',
|
|
6
|
+
'py',
|
|
2
7
|
'bash',
|
|
3
|
-
'
|
|
8
|
+
'ts',
|
|
9
|
+
'c',
|
|
4
10
|
'css',
|
|
11
|
+
'asm',
|
|
5
12
|
'csv',
|
|
6
13
|
'diff',
|
|
14
|
+
'docker',
|
|
7
15
|
'git',
|
|
16
|
+
'go',
|
|
8
17
|
'html',
|
|
9
18
|
'http',
|
|
10
19
|
'ini',
|
|
11
|
-
'
|
|
20
|
+
'java',
|
|
12
21
|
'jsdoc',
|
|
13
22
|
'json',
|
|
14
23
|
'leanpub-md',
|
|
24
|
+
'bf',
|
|
15
25
|
'log',
|
|
16
26
|
'lua',
|
|
17
27
|
'make',
|
|
18
28
|
'md',
|
|
19
29
|
'pl',
|
|
20
30
|
'plain',
|
|
21
|
-
'py',
|
|
22
31
|
'regex',
|
|
32
|
+
'rs',
|
|
23
33
|
'sql',
|
|
24
34
|
'todo',
|
|
25
35
|
'toml',
|
|
26
|
-
'ts',
|
|
27
36
|
'uri',
|
|
28
|
-
'yaml',
|
|
29
|
-
'docker',
|
|
30
37
|
'xml',
|
|
31
|
-
'
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
export const themes = [
|
|
38
|
+
'yaml'
|
|
39
|
+
]
|
|
40
|
+
|
|
41
|
+
/** @type {ShjBrowserTheme[]} */
|
|
42
|
+
export const themesBrowser = [
|
|
43
|
+
'default',
|
|
38
44
|
'atom-dark',
|
|
39
45
|
'github-dark',
|
|
40
46
|
'github-dim',
|
|
41
47
|
'dark',
|
|
42
|
-
'default',
|
|
43
48
|
'github-light',
|
|
44
49
|
'visual-studio-dark'
|
|
45
|
-
]
|
|
50
|
+
]
|
|
51
|
+
|
|
52
|
+
/** @type {ShjTerminalTheme[]} */
|
|
53
|
+
export const themesTerminal = [
|
|
54
|
+
'default',
|
|
55
|
+
'atom-dark'
|
|
56
|
+
]
|
|
57
|
+
|
|
46
58
|
export const testFiles = [
|
|
47
59
|
...languages,
|
|
48
60
|
'oneline.bash',
|
|
49
61
|
'oneline.http',
|
|
50
62
|
'screenshot.js'
|
|
51
|
-
]
|
|
63
|
+
]
|
package/examples/deno.js
CHANGED
|
@@ -1,14 +1,60 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { languages, themesTerminal } from './data.js'
|
|
2
|
+
import { setTheme, printHighlight } from '../src/terminal.js';
|
|
2
3
|
import { fromFileUrl } from 'https://deno.land/std/path/mod.ts';
|
|
4
|
+
import { parse } from "https://deno.land/std/flags/mod.ts"
|
|
3
5
|
|
|
4
|
-
|
|
6
|
+
let args = parse(Deno.args)
|
|
5
7
|
|
|
6
|
-
|
|
8
|
+
if (args.help)
|
|
9
|
+
{
|
|
10
|
+
console.log(`Usage deno run --allow-read deno.js [OPTION]... [TEST_FILE]`)
|
|
11
|
+
console.log(``)
|
|
12
|
+
console.log(` --help display this help and exit`)
|
|
13
|
+
console.log(` --theme=[-]THEME change the used THEME`)
|
|
14
|
+
console.log(` --lang=[-]LANGUAGE change the used LANGUAGE`)
|
|
15
|
+
console.log(` --stdin highlight code given from the stdin`)
|
|
16
|
+
console.log(``)
|
|
17
|
+
console.log(`THEME may be:`)
|
|
18
|
+
console.log(`${themesTerminal.join(', ')}`)
|
|
19
|
+
console.log(`LANGUAGE may be:`)
|
|
20
|
+
console.log(`${languages.join(', ')}`)
|
|
21
|
+
Deno.exit(0)
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
if (args.theme)
|
|
25
|
+
{
|
|
26
|
+
if (!themesTerminal.includes(args.theme))
|
|
27
|
+
{
|
|
28
|
+
console.error(`'${args.theme}' is not a supported try on of the following theme:`)
|
|
29
|
+
console.log(`${themesTerminal.join(', ')}`)
|
|
30
|
+
Deno.exit(1)
|
|
31
|
+
}
|
|
32
|
+
await setTheme(args.theme)
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
if (args.lang && !languages.includes(args.lang))
|
|
36
|
+
{
|
|
37
|
+
console.error(`'${args.lang}' is not a supported try on of the following languages:`)
|
|
38
|
+
console.log(`${languages.join(', ')}`)
|
|
39
|
+
Deno.exit(1)
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
if (args.stdin)
|
|
43
|
+
{
|
|
44
|
+
const buf = new Uint8Array(1024)
|
|
45
|
+
let n;
|
|
46
|
+
do {
|
|
47
|
+
n = await Deno.stdin.read(buf)
|
|
48
|
+
const code = new TextDecoder().decode(buf.subarray(0, n))
|
|
49
|
+
await printHighlight(code, args.lang ?? 'js');
|
|
50
|
+
} while (n)
|
|
51
|
+
Deno.exit(0)
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
const absolutePath = args._[0] ?? fromFileUrl(import.meta.url.replace(/[^\\\/]+$/, './languages/test.js'))
|
|
7
55
|
.replace(Deno.cwd(), '')
|
|
8
56
|
.slice(1)
|
|
9
57
|
|
|
10
|
-
const code = await Deno.readTextFile(
|
|
11
|
-
|
|
12
|
-
await setTheme('default');
|
|
58
|
+
const code = await Deno.readTextFile(absolutePath);
|
|
13
59
|
|
|
14
|
-
printHighlight(code, lang);
|
|
60
|
+
printHighlight(code, args.lang ?? args._[0]?.split?.('.')?.[1] ?? 'js');
|
package/examples/index.html
CHANGED
|
@@ -7,11 +7,14 @@
|
|
|
7
7
|
<title>Languages | Speed-highlight JS</title>
|
|
8
8
|
<style>
|
|
9
9
|
@import url('https://fonts.googleapis.com/css2?family=Fira+Code:wght@400&display=swap');
|
|
10
|
+
main {
|
|
11
|
+
max-width: 1000px;
|
|
12
|
+
margin: 0 auto
|
|
13
|
+
}
|
|
10
14
|
.fira {font: 18px 'Fira Code', monospace !important}
|
|
11
15
|
|
|
12
16
|
body {margin: 0}
|
|
13
17
|
html {background: #001220}
|
|
14
|
-
main {margin: 10px}
|
|
15
18
|
h1 {
|
|
16
19
|
text-align: center;
|
|
17
20
|
font: bold 60px Arial;
|
|
@@ -56,7 +59,7 @@
|
|
|
56
59
|
<script type="module">
|
|
57
60
|
import { highlightElement } from '../src/index.js'
|
|
58
61
|
import { detectLanguage } from '../src/detect.js'
|
|
59
|
-
import {
|
|
62
|
+
import { themesBrowser, testFiles } from './data.js'
|
|
60
63
|
|
|
61
64
|
const main = document.querySelector('main')
|
|
62
65
|
const elm = (type, opt) => Object.assign(document.createElement(type), opt)
|
|
@@ -86,8 +89,8 @@
|
|
|
86
89
|
|
|
87
90
|
async function highlight(language) {
|
|
88
91
|
let path = language.includes('.') ? language : `test.${language}`,
|
|
89
|
-
txt = code.textContent = await (await fetch(`./languages/${path}`)).text()
|
|
90
|
-
|
|
92
|
+
txt = code.textContent = await (await fetch(`./languages/${path}`)).text(),
|
|
93
|
+
start = performance.now()
|
|
91
94
|
await highlightElement(code, detect ? detectLanguage(txt) : path.split('.')[1], undefined, { hideLineNumbers })
|
|
92
95
|
chrono.textContent = `${(performance.now() - start).toFixed(3)}ms (counting loading time)`
|
|
93
96
|
chrono.animate([ { background: '#25f3' }, { background: 'unset' }],{ duration: 150 })
|
|
@@ -103,13 +106,13 @@
|
|
|
103
106
|
|
|
104
107
|
document.head.append(stylesheet)
|
|
105
108
|
|
|
106
|
-
new Radio('theme', theme => stylesheet.href = `../src/themes/${theme}.css`,
|
|
109
|
+
new Radio('theme', theme => stylesheet.href = `../src/themes/${theme}.css`, themesBrowser)
|
|
107
110
|
new Checkbox('detect language', on => { detect = on; highlight(lang.selected) }, container)
|
|
108
111
|
new Checkbox('header mode', mode => code.classList.toggle('shj-mode-header', mode), container)
|
|
109
112
|
new Checkbox('use fira font', use => code.classList.toggle('fira', use), container).on = true
|
|
110
|
-
new Checkbox('hide line numbering', ln => { hideLineNumbers = ln; highlight(lang.selected) }, container)
|
|
113
|
+
new Checkbox('hide line numbering', ln => { hideLineNumbers = ln; highlight(lang.selected) }, container).on = true
|
|
111
114
|
|
|
112
115
|
main.append(container, chrono, code)
|
|
113
116
|
</script>
|
|
114
117
|
</body>
|
|
115
|
-
</html>
|
|
118
|
+
</html>
|
|
@@ -7,15 +7,15 @@ import "fmt"
|
|
|
7
7
|
// main is the entry point for the program
|
|
8
8
|
func main() {
|
|
9
9
|
// declare a variable named num and assign it the value of 10
|
|
10
|
-
|
|
10
|
+
numTest := 10
|
|
11
11
|
|
|
12
12
|
// sanity check
|
|
13
|
-
fmt.Println("num is",
|
|
13
|
+
fmt.Println("num is", numTest)
|
|
14
14
|
|
|
15
15
|
//scan from the console and assign the value to the variable num
|
|
16
|
-
fmt.Scanln(&
|
|
16
|
+
fmt.Scanln(&numTest)
|
|
17
17
|
|
|
18
18
|
|
|
19
19
|
// print the value of num
|
|
20
|
-
fmt.Println(
|
|
20
|
+
fmt.Println(numTest)
|
|
21
21
|
}
|
|
@@ -17,7 +17,7 @@ class Token {
|
|
|
17
17
|
|
|
18
18
|
//lexer function that take a string and return a list of tokens
|
|
19
19
|
List<Token> lexer(String s) {
|
|
20
|
-
List<Token>
|
|
20
|
+
List<Token> tokensList = new ArrayList<Token>();
|
|
21
21
|
int line = 1;
|
|
22
22
|
int column = 0;
|
|
23
23
|
int i = 0;
|
|
@@ -47,6 +47,6 @@ void run(List<Token>) {
|
|
|
47
47
|
//main function
|
|
48
48
|
public static void main(String[] args) {
|
|
49
49
|
String s = "1+2+3";
|
|
50
|
-
List<Token>
|
|
51
|
-
run(
|
|
50
|
+
List<Token> tokensList = lexer(s);
|
|
51
|
+
run(tokensList);
|
|
52
52
|
}
|
package/examples/node.js
CHANGED
package/jsdoc.json
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
{
|
|
2
|
+
"plugins": [],
|
|
3
|
+
"recurseDepth": 10,
|
|
4
|
+
"source": {
|
|
5
|
+
"includePattern": ".+\\.js$"
|
|
6
|
+
},
|
|
7
|
+
"sourceType": "module",
|
|
8
|
+
"tags": {
|
|
9
|
+
"allowUnknownTags": true,
|
|
10
|
+
"dictionaries": ["jsdoc", "closure"]
|
|
11
|
+
},
|
|
12
|
+
"templates": {
|
|
13
|
+
"cleverLinks": false,
|
|
14
|
+
"monospaceLinks": false
|
|
15
|
+
}
|
|
16
|
+
}
|
package/package.json
CHANGED
|
@@ -1,40 +1,44 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
2
|
+
"name": "@speed-highlight/core",
|
|
3
|
+
"version": "1.1.9",
|
|
4
|
+
"description": "🌈 Light, fast, and easy to use, dependencies free javascript syntax highlighter, with automatic language detection",
|
|
5
|
+
"main": "dist/node/index.js",
|
|
6
|
+
"exports": {
|
|
7
|
+
"./terminal": "./dist/node/terminal.js",
|
|
8
|
+
"./terminal.js": "./dist/node/terminal.js"
|
|
9
|
+
},
|
|
10
|
+
"directories": {
|
|
11
|
+
"example": "examples"
|
|
12
|
+
},
|
|
13
|
+
"scripts": {
|
|
14
|
+
"build": "./.github/workflows/minify.sh"
|
|
15
|
+
},
|
|
16
|
+
"repository": {
|
|
17
|
+
"type": "git",
|
|
18
|
+
"url": "git://github.com/speed-highlight/core.git"
|
|
19
|
+
},
|
|
20
|
+
"keywords": [
|
|
21
|
+
"javascript",
|
|
22
|
+
"syntax-highlighting",
|
|
23
|
+
"language",
|
|
24
|
+
"fast",
|
|
25
|
+
"js",
|
|
26
|
+
"simple",
|
|
27
|
+
"highlighter",
|
|
28
|
+
"regex",
|
|
29
|
+
"highlighting",
|
|
30
|
+
"highlightjs",
|
|
31
|
+
"small",
|
|
32
|
+
"deno"
|
|
33
|
+
],
|
|
34
|
+
"author": "matubu",
|
|
35
|
+
"license": "CC0-1.0",
|
|
36
|
+
"bugs": {
|
|
37
|
+
"url": "https://github.com/speed-highlight/core/issues"
|
|
38
|
+
},
|
|
39
|
+
"homepage": "https://github.com/speed-highlight/core#readme",
|
|
40
|
+
"publishConfig": {
|
|
41
|
+
"registry": "https://npm.pkg.github.com/",
|
|
42
|
+
"access": "public"
|
|
43
|
+
}
|
|
40
44
|
}
|
package/src/color.js
ADDED
package/src/detect.js
CHANGED
|
@@ -1,3 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module detect
|
|
3
|
+
* (Language detector)
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import '../typedef.js'
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* @type {Array.<[ShjLanguage, ...[RegExp, Number][]]>}
|
|
10
|
+
*/
|
|
1
11
|
const languages = [
|
|
2
12
|
['bash', [/#!(\/usr)?\/bin\/bash/g, 500], [/\b(if|elif|then|fi|echo)\b|\$/g, 10]],
|
|
3
13
|
['html', [/<\/?[a-z-]+[^\n>]*>/g, 10], [/^\s+<!DOCTYPE\s+html/g, 500]],
|
|
@@ -22,12 +32,18 @@ const languages = [
|
|
|
22
32
|
['asm', [/^(section|global main|extern|\t(call|mov|ret))/gm, 100]],
|
|
23
33
|
]
|
|
24
34
|
|
|
35
|
+
/**
|
|
36
|
+
* @function detectLanguage
|
|
37
|
+
* Try to find the language the given code belong to
|
|
38
|
+
* @param {String} code The code
|
|
39
|
+
* @returns {ShjLanguage} The language of the code
|
|
40
|
+
*/
|
|
25
41
|
export const detectLanguage = code => {
|
|
26
42
|
return (languages
|
|
27
43
|
.map(([lang, ...features]) => [
|
|
28
|
-
|
|
29
|
-
|
|
44
|
+
lang,
|
|
45
|
+
features.reduce((acc, [match, score]) => acc + [...code.matchAll(match)].length * score, 0)
|
|
30
46
|
])
|
|
31
|
-
.filter(([
|
|
32
|
-
.sort((a, b) => b[
|
|
33
|
-
}
|
|
47
|
+
.filter(([lang, score]) => score > 20)
|
|
48
|
+
.sort((a, b) => b[1] - a[1])[0]?.[0] || 'plain');
|
|
49
|
+
}
|
package/src/index.js
CHANGED
|
@@ -1,28 +1,34 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
2
|
+
* @module index
|
|
3
|
+
* (Base script)
|
|
3
4
|
*/
|
|
4
5
|
|
|
6
|
+
import '../typedef.js'
|
|
7
|
+
|
|
5
8
|
import expandData from './common.js';
|
|
6
9
|
|
|
7
10
|
const langs = {},
|
|
8
11
|
sanitize = (str = '') =>
|
|
9
12
|
str.replaceAll('&', '&').replaceAll?.('<', '<').replaceAll?.('>', '>'),
|
|
10
13
|
/**
|
|
11
|
-
*
|
|
12
|
-
* @
|
|
13
|
-
*
|
|
14
|
-
* @
|
|
14
|
+
* @function
|
|
15
|
+
* @ignore
|
|
16
|
+
* Create a HTML element with the right token styling
|
|
17
|
+
* @param {String} str The content (need to be sanitized)
|
|
18
|
+
* @param {ShjToken} [token] The type of token
|
|
19
|
+
* @returns A HMTL string
|
|
15
20
|
*/
|
|
16
21
|
toSpan = (str, token) => token ? `<span class="shj-syn-${token}">${str}</span>` : str;
|
|
17
22
|
|
|
18
23
|
/**
|
|
19
|
-
*
|
|
20
|
-
*
|
|
21
|
-
* @param {String
|
|
22
|
-
* @param {
|
|
23
|
-
*
|
|
24
|
-
*
|
|
25
|
-
* * the
|
|
24
|
+
* @function tokenize
|
|
25
|
+
* Find the tokens in the given code and call the callback
|
|
26
|
+
* @param {String} src The code
|
|
27
|
+
* @param {ShjLanguage|Array} lang The language of the code
|
|
28
|
+
* @param {function(String, ShjToken=):void} token The callback function
|
|
29
|
+
* this function will be given
|
|
30
|
+
* * the text of the token
|
|
31
|
+
* * the type of the token
|
|
26
32
|
*/
|
|
27
33
|
export async function tokenize(src, lang, token) {
|
|
28
34
|
try {
|
|
@@ -79,20 +85,16 @@ export async function tokenize(src, lang, token) {
|
|
|
79
85
|
}
|
|
80
86
|
|
|
81
87
|
/**
|
|
82
|
-
* @
|
|
83
|
-
* @property {boolean} [hideLineNumbers=false] - Indicates whether to hide line numbers.
|
|
84
|
-
*/
|
|
85
|
-
|
|
86
|
-
/**
|
|
88
|
+
* @function highlightText
|
|
87
89
|
* @async
|
|
88
|
-
*
|
|
90
|
+
* Highlight a string passed as argument and return it
|
|
89
91
|
* @example
|
|
90
92
|
* elm.innerHTML = await highlightText(code, 'js');
|
|
91
|
-
* @param {String} src
|
|
92
|
-
* @param {
|
|
93
|
-
* @param {Boolean} [multiline=true]
|
|
94
|
-
* @param {
|
|
95
|
-
* @returns {String}
|
|
93
|
+
* @param {String} src The code
|
|
94
|
+
* @param {ShjLanguage} lang The language of the code
|
|
95
|
+
* @param {Boolean} [multiline=true] If it is multiline, it will add a wrapper for the line numbering and header
|
|
96
|
+
* @param {ShjOptions} [opt={}] Customization options
|
|
97
|
+
* @returns {Promise<String>} The highlighted string
|
|
96
98
|
*/
|
|
97
99
|
export async function highlightText(src, lang, multiline = true, opt = {}) {
|
|
98
100
|
let tmp = ''
|
|
@@ -104,12 +106,13 @@ export async function highlightText(src, lang, multiline = true, opt = {}) {
|
|
|
104
106
|
}
|
|
105
107
|
|
|
106
108
|
/**
|
|
109
|
+
* @function highlightElement
|
|
107
110
|
* @async
|
|
108
|
-
*
|
|
109
|
-
* @param {
|
|
110
|
-
* @param {
|
|
111
|
-
* @param {
|
|
112
|
-
* @param {
|
|
111
|
+
* Highlight a DOM element by getting the new innerHTML with highlightText
|
|
112
|
+
* @param {Element} elm The DOM element
|
|
113
|
+
* @param {ShjLanguage} [lang] The language of the code (seaching by default on `elm` for a 'shj-lang-' class)
|
|
114
|
+
* @param {ShjDisplayMode} [mode] The display mode (guessed by default)
|
|
115
|
+
* @param {ShjOptions} [opt={}] Customization options
|
|
113
116
|
*/
|
|
114
117
|
export async function highlightElement(elm, lang = elm.className.match(/shj-lang-([\w-]+)/)?.[1], mode, opt) {
|
|
115
118
|
let txt = elm.textContent;
|
|
@@ -120,19 +123,22 @@ export async function highlightElement(elm, lang = elm.className.match(/shj-lang
|
|
|
120
123
|
}
|
|
121
124
|
|
|
122
125
|
/**
|
|
123
|
-
* @async
|
|
124
126
|
* @function highlightAll
|
|
125
|
-
*
|
|
126
|
-
*
|
|
127
|
-
*
|
|
128
|
-
* ```html
|
|
129
|
-
* <div class='shj-lang-[code-language]'>[code]</div>
|
|
130
|
-
* or
|
|
131
|
-
* <code class='shj-lang-[code-language]'>[inline code]</code>
|
|
132
|
-
* ```
|
|
133
|
-
* @param {HighlightOptions} [opt={}] other options
|
|
127
|
+
* @async
|
|
128
|
+
* Call highlightElement on element with a css class starting with `shj-lang-`
|
|
129
|
+
* @param {ShjOptions} [opt={}] Customization options
|
|
134
130
|
*/
|
|
135
131
|
export let highlightAll = async (opt) =>
|
|
136
132
|
document
|
|
137
133
|
.querySelectorAll('[class*="shj-lang-"]')
|
|
138
134
|
.forEach(elm => highlightElement(elm, undefined, undefined, opt))
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* @function loadLanguage
|
|
138
|
+
* Load a language and add it to the langs object
|
|
139
|
+
* @param {String} languageName The name of the language
|
|
140
|
+
* @param {ShjLanguage} language The language
|
|
141
|
+
*/
|
|
142
|
+
export let loadLanguage = (languageName, language) => {
|
|
143
|
+
langs[languageName] = language;
|
|
144
|
+
}
|
package/src/languages/c.js
CHANGED
package/src/languages/go.js
CHANGED
package/src/languages/java.js
CHANGED
package/src/languages/js.js
CHANGED
package/src/languages/py.js
CHANGED
package/src/languages/rs.js
CHANGED
package/src/terminal.js
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module terminal
|
|
3
|
+
* (Terminal adaptor)
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import '../typedef.js'
|
|
7
|
+
|
|
8
|
+
import { tokenize } from './index.js';
|
|
9
|
+
|
|
10
|
+
let theme = import('./themes/default.js');
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* @function highlightText
|
|
14
|
+
* @async
|
|
15
|
+
* Highlight a string passed as argument and return a string that can directly be printed
|
|
16
|
+
* @param {String} src The code
|
|
17
|
+
* @param {ShjLanguage} lang The language of the code
|
|
18
|
+
* @returns {Promise<String>} The highlighted string
|
|
19
|
+
*/
|
|
20
|
+
export const highlightText = async (src, lang) => {
|
|
21
|
+
let res = '', themeMap = (await theme).default;
|
|
22
|
+
|
|
23
|
+
await tokenize(src, lang, (str, token) => res += token ? `${themeMap[token] ?? ''}${str}\x1b[0m` : str);
|
|
24
|
+
|
|
25
|
+
return res;
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* @function printHighlight
|
|
30
|
+
* @async
|
|
31
|
+
* Highlight and print a given string
|
|
32
|
+
* @param {String} src The code
|
|
33
|
+
* @param {ShjLanguage} lang The language of the code
|
|
34
|
+
*/
|
|
35
|
+
export const printHighlight = async (src, lang) => console.log(await highlightText(src, lang));
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* @function setTheme
|
|
39
|
+
* Change the current used theme for highlighting
|
|
40
|
+
* @param {ShjTerminalTheme} name The name of the theme
|
|
41
|
+
*/
|
|
42
|
+
export const setTheme = async name => theme = import(`./themes/${name}.js`);
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import col from '../color.js'
|
|
2
|
+
|
|
3
|
+
export default {
|
|
4
|
+
deleted: col.red,
|
|
5
|
+
var: col.red,
|
|
6
|
+
err: col.red,
|
|
7
|
+
kwd: col.magenta,
|
|
8
|
+
num: col.yellow,
|
|
9
|
+
class: col.yellow,
|
|
10
|
+
cmnt: col.gray,
|
|
11
|
+
insert: col.green,
|
|
12
|
+
str: col.green,
|
|
13
|
+
bool: col.cyan,
|
|
14
|
+
type: col.blue,
|
|
15
|
+
oper: col.blue,
|
|
16
|
+
section: col.magenta,
|
|
17
|
+
func: col.blue
|
|
18
|
+
}
|