@speed-highlight/core 1.1.6

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.
Files changed (187) hide show
  1. package/.gitattributes +2 -0
  2. package/CODE_OF_CONDUCT.md +128 -0
  3. package/CONTRIBUTING.md +34 -0
  4. package/LICENSE +121 -0
  5. package/README.md +140 -0
  6. package/assets/screenshot.png +0 -0
  7. package/dist/common.js +1 -0
  8. package/dist/detect.js +1 -0
  9. package/dist/index.js +3 -0
  10. package/dist/languages/asm.js +1 -0
  11. package/dist/languages/bash.js +1 -0
  12. package/dist/languages/bf.js +1 -0
  13. package/dist/languages/c.js +1 -0
  14. package/dist/languages/css.js +1 -0
  15. package/dist/languages/csv.js +1 -0
  16. package/dist/languages/diff.js +1 -0
  17. package/dist/languages/docker.js +1 -0
  18. package/dist/languages/git.js +1 -0
  19. package/dist/languages/go.js +1 -0
  20. package/dist/languages/html.js +1 -0
  21. package/dist/languages/http.js +1 -0
  22. package/dist/languages/ini.js +1 -0
  23. package/dist/languages/java.js +1 -0
  24. package/dist/languages/js.js +1 -0
  25. package/dist/languages/js_template_literals.js +1 -0
  26. package/dist/languages/jsdoc.js +1 -0
  27. package/dist/languages/json.js +1 -0
  28. package/dist/languages/leanpub-md.js +2 -0
  29. package/dist/languages/log.js +1 -0
  30. package/dist/languages/lua.js +1 -0
  31. package/dist/languages/make.js +1 -0
  32. package/dist/languages/md.js +2 -0
  33. package/dist/languages/pl.js +1 -0
  34. package/dist/languages/plain.js +1 -0
  35. package/dist/languages/py.js +1 -0
  36. package/dist/languages/regex.js +1 -0
  37. package/dist/languages/rs.js +1 -0
  38. package/dist/languages/sql.js +1 -0
  39. package/dist/languages/todo.js +1 -0
  40. package/dist/languages/toml.js +1 -0
  41. package/dist/languages/ts.js +1 -0
  42. package/dist/languages/uri.js +1 -0
  43. package/dist/languages/xml.js +1 -0
  44. package/dist/languages/yaml.js +1 -0
  45. package/dist/node/common.js +1 -0
  46. package/dist/node/detect.js +1 -0
  47. package/dist/node/index.js +3 -0
  48. package/dist/node/languages/asm.js +1 -0
  49. package/dist/node/languages/bash.js +1 -0
  50. package/dist/node/languages/bf.js +1 -0
  51. package/dist/node/languages/c.js +1 -0
  52. package/dist/node/languages/css.js +1 -0
  53. package/dist/node/languages/csv.js +1 -0
  54. package/dist/node/languages/diff.js +1 -0
  55. package/dist/node/languages/docker.js +1 -0
  56. package/dist/node/languages/git.js +1 -0
  57. package/dist/node/languages/go.js +1 -0
  58. package/dist/node/languages/html.js +1 -0
  59. package/dist/node/languages/http.js +1 -0
  60. package/dist/node/languages/ini.js +1 -0
  61. package/dist/node/languages/java.js +1 -0
  62. package/dist/node/languages/js.js +1 -0
  63. package/dist/node/languages/js_template_literals.js +1 -0
  64. package/dist/node/languages/jsdoc.js +1 -0
  65. package/dist/node/languages/json.js +1 -0
  66. package/dist/node/languages/leanpub-md.js +2 -0
  67. package/dist/node/languages/log.js +1 -0
  68. package/dist/node/languages/lua.js +1 -0
  69. package/dist/node/languages/make.js +1 -0
  70. package/dist/node/languages/md.js +2 -0
  71. package/dist/node/languages/pl.js +1 -0
  72. package/dist/node/languages/plain.js +1 -0
  73. package/dist/node/languages/py.js +1 -0
  74. package/dist/node/languages/regex.js +1 -0
  75. package/dist/node/languages/rs.js +1 -0
  76. package/dist/node/languages/sql.js +1 -0
  77. package/dist/node/languages/todo.js +1 -0
  78. package/dist/node/languages/toml.js +1 -0
  79. package/dist/node/languages/ts.js +1 -0
  80. package/dist/node/languages/uri.js +1 -0
  81. package/dist/node/languages/xml.js +1 -0
  82. package/dist/node/languages/yaml.js +1 -0
  83. package/dist/node/term.js +1 -0
  84. package/dist/node/themes/default.js +1 -0
  85. package/dist/term.js +1 -0
  86. package/dist/themes/atom-dark.css +1 -0
  87. package/dist/themes/dark.css +1 -0
  88. package/dist/themes/default.css +1 -0
  89. package/dist/themes/default.js +1 -0
  90. package/dist/themes/github-dark.css +1 -0
  91. package/dist/themes/github-dim.css +1 -0
  92. package/dist/themes/github-light.css +1 -0
  93. package/dist/themes/visual-studio-dark.css +1 -0
  94. package/examples/deno.js +14 -0
  95. package/examples/dist.html +27 -0
  96. package/examples/helper/data.js +51 -0
  97. package/examples/helper/elm.js +58 -0
  98. package/examples/index.html +52 -0
  99. package/examples/languages/oneline.bash +1 -0
  100. package/examples/languages/oneline.http +1 -0
  101. package/examples/languages/screenshot.js +18 -0
  102. package/examples/languages/test.asm +17 -0
  103. package/examples/languages/test.bash +32 -0
  104. package/examples/languages/test.bf +21 -0
  105. package/examples/languages/test.c +57 -0
  106. package/examples/languages/test.css +20 -0
  107. package/examples/languages/test.csv +4 -0
  108. package/examples/languages/test.diff +27 -0
  109. package/examples/languages/test.docker +23 -0
  110. package/examples/languages/test.git +10 -0
  111. package/examples/languages/test.go +21 -0
  112. package/examples/languages/test.html +27 -0
  113. package/examples/languages/test.http +17 -0
  114. package/examples/languages/test.ini +6 -0
  115. package/examples/languages/test.java +52 -0
  116. package/examples/languages/test.js +34 -0
  117. package/examples/languages/test.jsdoc +2 -0
  118. package/examples/languages/test.json +24 -0
  119. package/examples/languages/test.leanpub-md +21 -0
  120. package/examples/languages/test.log +32 -0
  121. package/examples/languages/test.lua +62 -0
  122. package/examples/languages/test.make +51 -0
  123. package/examples/languages/test.md +62 -0
  124. package/examples/languages/test.pl +25 -0
  125. package/examples/languages/test.plain +3 -0
  126. package/examples/languages/test.py +38 -0
  127. package/examples/languages/test.regex +2 -0
  128. package/examples/languages/test.rs +38 -0
  129. package/examples/languages/test.sql +6 -0
  130. package/examples/languages/test.todo +18 -0
  131. package/examples/languages/test.toml +16 -0
  132. package/examples/languages/test.ts +43 -0
  133. package/examples/languages/test.uri +9 -0
  134. package/examples/languages/test.xml +44 -0
  135. package/examples/languages/test.yaml +18 -0
  136. package/examples/node.js +11 -0
  137. package/examples/style.css +28 -0
  138. package/examples/worker.html +19 -0
  139. package/examples/worker.js +6 -0
  140. package/package.json +40 -0
  141. package/src/common.js +18 -0
  142. package/src/detect.js +33 -0
  143. package/src/index.js +129 -0
  144. package/src/languages/asm.js +30 -0
  145. package/src/languages/bash.js +44 -0
  146. package/src/languages/bf.js +18 -0
  147. package/src/languages/c.js +52 -0
  148. package/src/languages/css.js +56 -0
  149. package/src/languages/csv.js +9 -0
  150. package/src/languages/diff.js +18 -0
  151. package/src/languages/docker.js +9 -0
  152. package/src/languages/git.js +20 -0
  153. package/src/languages/go.js +28 -0
  154. package/src/languages/html.js +51 -0
  155. package/src/languages/http.js +30 -0
  156. package/src/languages/ini.js +22 -0
  157. package/src/languages/java.js +28 -0
  158. package/src/languages/js.js +48 -0
  159. package/src/languages/js_template_literals.js +34 -0
  160. package/src/languages/jsdoc.js +18 -0
  161. package/src/languages/json.js +20 -0
  162. package/src/languages/leanpub-md.js +34 -0
  163. package/src/languages/log.js +28 -0
  164. package/src/languages/lua.js +28 -0
  165. package/src/languages/make.js +36 -0
  166. package/src/languages/md.js +48 -0
  167. package/src/languages/pl.js +25 -0
  168. package/src/languages/plain.js +5 -0
  169. package/src/languages/py.js +52 -0
  170. package/src/languages/regex.js +19 -0
  171. package/src/languages/rs.js +28 -0
  172. package/src/languages/sql.js +33 -0
  173. package/src/languages/todo.js +19 -0
  174. package/src/languages/toml.js +36 -0
  175. package/src/languages/ts.js +13 -0
  176. package/src/languages/uri.js +26 -0
  177. package/src/languages/xml.js +68 -0
  178. package/src/languages/yaml.js +32 -0
  179. package/src/term.js +19 -0
  180. package/src/themes/atom-dark.css +23 -0
  181. package/src/themes/dark.css +23 -0
  182. package/src/themes/default.css +87 -0
  183. package/src/themes/default.js +26 -0
  184. package/src/themes/github-dark.css +23 -0
  185. package/src/themes/github-dim.css +3 -0
  186. package/src/themes/github-light.css +21 -0
  187. package/src/themes/visual-studio-dark.css +23 -0
@@ -0,0 +1,6 @@
1
+ -- Comment
2
+ SELECT 'Geeks' || ' ' || @test_hello FROM dual;
3
+ INSERT INTO database (geek_id, geek_name) VALUES (5000, 'abc');
4
+ REPLACE('123geeks123', '123', TRUE);
5
+ /* FIXME Multiline
6
+ Comment */
@@ -0,0 +1,18 @@
1
+ TODO ...
2
+ FIXME ...
3
+ CHANGED ...
4
+ CHANGE ...
5
+ FIX ...
6
+ XXX ...
7
+ IDEA ...
8
+ HACK ...
9
+ NOTE ...
10
+ REVIEW ...
11
+ NB ...
12
+ BUG ...
13
+ QUESTION ...
14
+ COMBAK ...
15
+ TEMP ...
16
+ DEBUG ...
17
+ OPTIMIZE ...
18
+ WARNING ...
@@ -0,0 +1,16 @@
1
+ # CHANGED This is a TOML document
2
+
3
+ title = "TOML Example"
4
+
5
+ [owner]
6
+ name = "Tom Preston-Werner"
7
+ dob = 1979-05-27T07:32:00-08:00
8
+
9
+ [database]
10
+ enabled = true
11
+ ports = [ 8000, inf, 8002 ]
12
+ data = [ ["delta", "phi"], [3.14] ]
13
+ temp_targets = { cpu = 79.5, case = 72.0 }
14
+ re = """
15
+ test
16
+ """
@@ -0,0 +1,43 @@
1
+ type Result = "pass" | "fail"
2
+
3
+ interface User {
4
+ id: number
5
+ firstName: string
6
+ lastName: string
7
+ role: string
8
+ result: Result
9
+ }
10
+
11
+ const test = {
12
+ test: value
13
+ };
14
+
15
+ function updateUser(id: number, update: Partial<User>) {
16
+ const user = getUser(id)
17
+ const newUser = { ...user, ...update }
18
+ saveUser(id, newUser)
19
+ }
20
+
21
+ function compact(arr: string[]) {
22
+ if (arr.length > 10)
23
+ return arr.slice(0, 10)
24
+ return arr
25
+ }
26
+
27
+ namespace Validation {
28
+ export interface StringValidator {
29
+ isAcceptable(s: string): boolean;
30
+ }
31
+ const lettersRegexp = /^[A-Za-z]+$/;
32
+ const numberRegexp = /^[0-9]+$/;
33
+ export class LettersOnlyValidator implements StringValidator {
34
+ isAcceptable(s: string) {
35
+ return lettersRegexp.test(s);
36
+ }
37
+ }
38
+ export class ZipCodeValidator implements StringValidator {
39
+ isAcceptable(s: string) {
40
+ return s.length === 5 && numberRegexp.test(s);
41
+ }
42
+ }
43
+ }
@@ -0,0 +1,9 @@
1
+ https://john.doe@www.example.com:123/forum/questions/?tag=networking&order=newest#top
2
+ # TODO more uri
3
+ http://github.matubu.com/shj/examples/index.html
4
+ ldap://[2001:db8::7]/c=GB?objectClass?one
5
+ mailto:John.Doe@example.com
6
+ news:comp.infosystems.www.servers.unix
7
+ tel:+1-816-555-1212
8
+ telnet://192.0.2.16:80/
9
+ urn:oasis:names:specification:docbook:dtd:xml:4.1.2
@@ -0,0 +1,44 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <!--Simple example from w3schools-->
3
+ <breakfast_menu>
4
+ <food>
5
+ <name>Belgian Waffles</name>
6
+ <price>$5.95</price>
7
+ <description>
8
+ Two of our famous Belgian Waffles with plenty of real maple syrup
9
+ </description>
10
+ <calories>650</calories>
11
+ </food>
12
+ <food>
13
+ <name>Strawberry Belgian Waffles</name>
14
+ <price>$7.95</price>
15
+ <description>
16
+ Light Belgian waffles covered with strawberries and whipped cream
17
+ </description>
18
+ <calories>900</calories>
19
+ </food>
20
+ <food>
21
+ <name>Berry-Berry Belgian Waffles</name>
22
+ <price>$8.95</price>
23
+ <description>
24
+ Belgian waffles covered with assorted fresh berries and whipped cream
25
+ </description>
26
+ <calories>900</calories>
27
+ </food>
28
+ <food>
29
+ <name>French Toast</name>
30
+ <price>$4.50</price>
31
+ <description>
32
+ Thick slices made from our homemade sourdough bread
33
+ </description>
34
+ <calories>600</calories>
35
+ </food>
36
+ <food>
37
+ <name>Homestyle Breakfast</name>
38
+ <price>$6.95</price>
39
+ <description>
40
+ Two eggs, bacon or sausage, toast, and our ever-popular hash browns
41
+ </description>
42
+ <calories>950</calories>
43
+ </food>
44
+ </breakfast_menu>
@@ -0,0 +1,18 @@
1
+ --- # The Smiths
2
+ - {name: John Smith, age: 33}
3
+ - name: Mary Smith
4
+ age: !!str 27
5
+ - [name, age]: [Rae Smith, 4] # sequences as kwds are supported
6
+ --- # TODO People, by gender
7
+ men: ["John Smith", 'Bill Jones', Yes]
8
+ data: >
9
+ Wrapped text
10
+ will be folded
11
+ into a single
12
+ paragraph
13
+
14
+ Blank lines denote
15
+ paragraph breaks
16
+ women:
17
+ - Mary Smith
18
+ - Susan Williams
@@ -0,0 +1,11 @@
1
+ const fs = require('fs'),
2
+ path = require('path'),
3
+ { setTheme, printHighlight } = require('../dist/node/term.js');
4
+
5
+ const lang = process.argv[2] ?? 'js';
6
+
7
+ setTheme('default').then(_ => {
8
+ const code = fs.readFileSync(path.resolve(__dirname, `./languages/test.${lang}`));
9
+
10
+ printHighlight(code, lang);
11
+ });
@@ -0,0 +1,28 @@
1
+ label, .chrono {font: 14px Arial}
2
+ .radio-list {
3
+ margin: 10px 0;
4
+ display: flex;
5
+ flex-wrap: wrap;
6
+ gap: 5px
7
+ }
8
+ input {
9
+ position: absolute;
10
+ visibility: hidden
11
+ }
12
+ label {
13
+ cursor: pointer;
14
+ transition: .2s;
15
+ background: #c5c5e022;
16
+ border-radius: 5px;
17
+ padding: 10px
18
+ }
19
+ label:hover {background: #c5c5e033}
20
+ input:checked + label {
21
+ color: #58f;
22
+ background: #58f3
23
+ }
24
+ .chrono {
25
+ padding: 3px;
26
+ border-radius: 2px;
27
+ color: #889
28
+ }
@@ -0,0 +1,19 @@
1
+ <!DOCTYPE html>
2
+ <html lang="en">
3
+ <head>
4
+ <meta charset="UTF-8">
5
+ <title>Web worker | Speed highlight JS</title>
6
+ <link rel="stylesheet" href="../src/themes/atom-dark.css">
7
+ </head>
8
+ <body>
9
+ <div class="shj-lang-js shj-multiline shj-mode-noheader" id="code">this.code = 'highlighted on a web worker';
10
+ this.isAwesome = new Boolean(.5);
11
+ this.doesNotWorkOnFirefox = '💩'</div>
12
+ <script type="module">
13
+ const code = document.querySelector('#code');
14
+ const worker = new Worker('./worker.js', { type: "module" });
15
+ worker.onmessage = e => code.innerHTML = e.data;
16
+ worker.postMessage([code.textContent, 'js']);
17
+ </script>
18
+ </body>
19
+ </html>
@@ -0,0 +1,6 @@
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
+ };
package/package.json ADDED
@@ -0,0 +1,40 @@
1
+ {
2
+ "name": "@speed-highlight/core",
3
+ "version": "1.1.6",
4
+ "description": "🌈 Light, fast, and easy to use, dependencies free javascript syntax highlighter, with automatic language detection",
5
+ "main": "dist/node/index.js",
6
+ "directories": {
7
+ "example": "examples"
8
+ },
9
+ "scripts": {
10
+ "test": "echo \"Error: no test specified\" && exit 1"
11
+ },
12
+ "repository": {
13
+ "type": "git",
14
+ "url": "git://github.com/speed-highlight/core.git"
15
+ },
16
+ "keywords": [
17
+ "javascript",
18
+ "syntax-highlighting",
19
+ "language",
20
+ "fast",
21
+ "js",
22
+ "simple",
23
+ "highlighter",
24
+ "regex",
25
+ "highlighting",
26
+ "highlightjs",
27
+ "small",
28
+ "deno"
29
+ ],
30
+ "author": "matubu",
31
+ "license": "CC0-1.0",
32
+ "bugs": {
33
+ "url": "https://github.com/speed-highlight/core/issues"
34
+ },
35
+ "homepage": "https://github.com/speed-highlight/core#readme",
36
+ "publishConfig": {
37
+ "registry": "https://npm.pkg.github.com/",
38
+ "access": "public"
39
+ }
40
+ }
package/src/common.js ADDED
@@ -0,0 +1,18 @@
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
+ }
package/src/detect.js ADDED
@@ -0,0 +1,33 @@
1
+ const languages = [
2
+ ['bash', [/#!(\/usr)?\/bin\/bash/g, 500], [/\b(if|elif|then|fi|echo)\b|\$/g, 10]],
3
+ ['html', [/<\/?[a-z-]+[^\n>]*>/g, 10], [/^\s+<!DOCTYPE\s+html/g, 500]],
4
+ ['http', [/^(GET|HEAD|POST|PUT|DELETE|PATCH|HTTP)\b/g, 500]],
5
+ ['js', [/\b(console|await|async|function|export|import|this|class|for|let|const|map|join|require)\b/g, 10]],
6
+ ['ts', [/\b(console|await|async|function|export|import|this|class|for|let|const|map|join|require|implements|interface|namespace)\b/g, 10]],
7
+ ['py', [/\b(def|print|class|and|or|lambda)\b/g, 10]],
8
+ ['sql', [/\b(SELECT|INSERT|FROM)\b/g, 50]],
9
+ ['pl', [/#!(\/usr)?\/bin\/perl/g, 500], [/\b(use|print)\b|\$/g, 10]],
10
+ ['lua', [/#!(\/usr)?\/bin\/lua/g, 500]],
11
+ ['make', [/\b(ifneq|endif|if|elif|then|fi|echo|.PHONY|^[a-z]+ ?:$)\b|\$/gm, 10]],
12
+ ['uri', [/https?:|mailto:|tel:|ftp:/g, 30]],
13
+ ['css', [/^(@import|@page|@media|(\.|#)[a-z]+)/gm, 20]],
14
+ ['diff', [/^[+><-]/gm, 10], [/^@@ ?[-+,0-9 ]+ ?@@/gm, 25]],
15
+ ['md', [/^(>|\t\*|\t\d+.)/gm, 10], [/\[.*\](.*)/g, 10]],
16
+ ['docker', [/^(FROM|ENTRYPOINT|RUN)/gm, 500]],
17
+ ['xml', [/<\/?[a-z-]+[^\n>]*>/g, 10], [/^<\?xml/g, 500]],
18
+ ['c', [/#include\b|\bprintf\s+\(/g, 100]],
19
+ ['rs', [/^\s+(use|fn|mut|match)\b/gm, 100]],
20
+ ['go', [/\b(func|fmt|package)\b/g, 100]],
21
+ ['java', [/^import\s+java/gm, 500]],
22
+ ['asm', [/^(section|global main|extern|\t(call|mov|ret))/gm, 100]],
23
+ ]
24
+
25
+ export const detectLanguage = code => {
26
+ return (languages
27
+ .map(([lang, ...features]) => [
28
+ features.reduce((acc, [match, score]) => acc + [...code.matchAll(match)].length * score, 0),
29
+ lang
30
+ ])
31
+ .filter(([score, lang]) => score > 10)
32
+ .sort((a, b) => b[0] - a[0])[0]?.[1] || 'plain');
33
+ }
package/src/index.js ADDED
@@ -0,0 +1,129 @@
1
+ /**
2
+ * Base script
3
+ */
4
+
5
+ import expandData from './common.js';
6
+
7
+ const langs = {},
8
+ sanitize = (str = '') =>
9
+ str.replaceAll('&', '&#38;').replaceAll?.('<', '&lt;').replaceAll?.('>', '&gt;'),
10
+ /**
11
+ * A function that turn text in HTML to apply a class to it
12
+ * @param {String} str need to be sanitize to be safe
13
+ * @param {String} [token] token type
14
+ * @returns A string with HTML semantics
15
+ */
16
+ toSpan = (str, token) => token ? `<span class="shj-syn-${token}">${str}</span>` : str;
17
+
18
+ /**
19
+ * A
20
+ * @param {String} src
21
+ * @param {String|Object} lang
22
+ * @param {Function} token the callback that will be call for every token found.
23
+ * the arguments pass will be:
24
+ * * the token as a string (can be an empty string)
25
+ * * the token type also as a string like 'err', 'cmnt', ...
26
+ */
27
+ export async function tokenize(src, lang, token) {
28
+ try {
29
+ let m,
30
+ part,
31
+ first = {},
32
+ match,
33
+ cache = [],
34
+ i = 0,
35
+ data = typeof lang === 'string' ? (await (langs[lang] ??= import(`./languages/${lang}.js`))) : lang,
36
+ // make a fast shallow copy to bee able to splice lang without change the original one
37
+ arr = [...typeof lang === 'string' ? data.default : lang.sub];
38
+
39
+ while (i < src.length) {
40
+ first.index = null;
41
+ for (m = arr.length; m-- > 0;) {
42
+ part = arr[m].expand ? expandData[arr[m].expand] : arr[m];
43
+ // do not call again exec if the previous result is sufficient
44
+ if (cache[m] === undefined || cache[m].match.index < i) {
45
+ part.match.lastIndex = i;
46
+ match = part.match.exec(src);
47
+ if (match === null) {
48
+ // no more match with this regex can be disposed
49
+ arr.splice(m, 1);
50
+ cache.splice(m, 1);
51
+ continue;
52
+ }
53
+ // save match for later use to decrease performance cost
54
+ cache[m] = { match, lastIndex: part.match.lastIndex };
55
+ }
56
+ // check if it the first match in the string
57
+ if (cache[m].match[0] && (cache[m].match.index <= first.index || first.index === null))
58
+ first = {
59
+ part: part,
60
+ index: cache[m].match.index,
61
+ match: cache[m].match[0],
62
+ end: cache[m].lastIndex
63
+ }
64
+ }
65
+ if (first.index === null)
66
+ break;
67
+ token(src.slice(i, first.index), data.type);
68
+ i = first.end;
69
+ if (first.part.sub)
70
+ await tokenize(first.match, typeof first.part.sub === 'string' ? first.part.sub : (typeof first.part.sub === 'function' ? first.part.sub(first.match) : first.part), token);
71
+ else
72
+ token(first.match, first.part.type);
73
+ }
74
+ token(src.slice(i, src.length), data.type);
75
+ }
76
+ catch {
77
+ token(src);
78
+ }
79
+ }
80
+
81
+ /**
82
+ * @async
83
+ * @function highlightText A function that highlight a string text and return it
84
+ * @example
85
+ * elm.innerHTML = await highlightText(code, 'js');
86
+ * @param {String} src the text content to be highlighted
87
+ * @param {String} lang the lang name ex: 'js'
88
+ * @param {Boolean} [multiline=true] inline mode
89
+ * @returns {String} the highlighted as String text
90
+ */
91
+ export async function highlightText(src, lang, multiline = true) {
92
+ let res = multiline ? `<div><div class="shj-numbers">${'<div></div>'.repeat(src.split('\n').length)}</div><div>` : '';
93
+
94
+ await tokenize(src, lang, (str, type) => res += toSpan(sanitize(str), type))
95
+
96
+ if (multiline)
97
+ res += '</div></div>';
98
+ return res;
99
+ }
100
+
101
+ /**
102
+ * @async
103
+ * @function highlightElement highlight a element code with a 'pre' parent
104
+ * @param {HTMLElement} elm the code elm
105
+ * @param {String} [lang] the lang used for syntax highlighting by default is found in the className of the parent or the elm it self
106
+ * @param {String} [mode] can be set to inline multiline or oneline by default it's auto detected: inline if `code` tag else depends of the number of lines
107
+ */
108
+ export async function highlightElement(elm, lang = elm.className.match(/shj-lang-([\w-]+)/)?.[1], mode) {
109
+ let txt = elm.textContent;
110
+ mode ??= `${elm.tagName == 'CODE' ? 'in' : (txt.split('\n').length < 2 ? 'one' : 'multi')}line`;
111
+ elm.dataset.lang = lang;
112
+ elm.className = `${[...elm.classList].filter(className => !className.startsWith('shj-') || className.startsWith('shj-mode-')).join(' ')} shj-lang-${lang} shj-${mode}`;
113
+ elm.innerHTML = await highlightText(txt, lang, mode == 'multiline');
114
+ }
115
+
116
+ /**
117
+ * for all element that have a class name starting with "shj-lang-"
118
+ * this function will call highlightElement with the html element as argument
119
+ * The function will select those scheme for example:
120
+ * ```html
121
+ * <div class='shj-lang-[code-language]'>[code]</div>
122
+ * or
123
+ * <code class='shj-lang-[code-language]'>[inline code]</code>
124
+ * ```
125
+ */
126
+ export let highlightAll = async () =>
127
+ document
128
+ .querySelectorAll('[class*="shj-lang-"]')
129
+ .forEach(elm => highlightElement(elm));
@@ -0,0 +1,30 @@
1
+ export default [
2
+ {
3
+ type: 'cmnt',
4
+ match: /;.*/gm
5
+ },
6
+ {
7
+ expand: 'str'
8
+ },
9
+ {
10
+ expand: 'num'
11
+ },
12
+ {
13
+ type: 'kwd',
14
+ match: /^[a-z]+\s+[a-z.]+/gm,
15
+ sub: [
16
+ {
17
+ type: 'func',
18
+ match: /^[a-z]+/g
19
+ }
20
+ ]
21
+ },
22
+ {
23
+ type: 'kwd',
24
+ match: /^\t*[a-z]+/gm,
25
+ },
26
+ {
27
+ match: /%|\$/g,
28
+ type: 'oper'
29
+ }
30
+ ]
@@ -0,0 +1,44 @@
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
+ type: 'kwd',
18
+ match: /-[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)\b/g
19
+ },
20
+ {
21
+ expand: 'num'
22
+ },
23
+ {
24
+ type: 'func',
25
+ match: /\b(set|alias|bg|bind|builtin|caller|command|compgen|complete|dirs|disown|echo|enable|eval|exec|exit|fc|fg|getopts|hash|help|history|jobs|kill|let|logout|popd|printf|pushd|pwd|read|set|shift|shopt|source|suspend|test|times|trap|type|ulimit|umask|unalias|unset)\b/g
26
+ },
27
+ {
28
+ type: 'bool',
29
+ match: /\b(true|false)\b/g
30
+ },
31
+ {
32
+ type: 'func',
33
+ match: /[a-z_]+(?=\s*\()/g
34
+ },
35
+ {
36
+ type: 'oper',
37
+ match: /[=(){}<>+*/!?~^-]+/g
38
+ },
39
+ {
40
+ type: 'var',
41
+ match: /\w+(?=\s*=)/g
42
+ },
43
+ variable
44
+ ]
@@ -0,0 +1,18 @@
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
+ ]
@@ -0,0 +1,52 @@
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: /[A-Z][\w_]*/g
51
+ }
52
+ ]
@@ -0,0 +1,56 @@
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
+ ]
@@ -0,0 +1,9 @@
1
+ export default [
2
+ {
3
+ expand: 'strDouble'
4
+ },
5
+ {
6
+ type: 'oper',
7
+ match: /,/g
8
+ }
9
+ ]
@@ -0,0 +1,18 @@
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
+ ]