@shikijs/engine-javascript 1.17.0

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/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2021 Pine Wu
4
+ Copyright (c) 2023 Anthony Fu <https://github.com/antfu>
5
+
6
+ Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ of this software and associated documentation files (the "Software"), to deal
8
+ in the Software without restriction, including without limitation the rights
9
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ copies of the Software, and to permit persons to whom the Software is
11
+ furnished to do so, subject to the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be included in all
14
+ copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,9 @@
1
+ # @shikijs/engine-javascript
2
+
3
+ Engine for Shiki using JavaScript's native RegExp (experimental).
4
+
5
+ [Documentation](https://shiki.style/guide/regex-engines)
6
+
7
+ ## License
8
+
9
+ MIT
@@ -0,0 +1,50 @@
1
+ import { PatternScanner, RegexEngineString, RegexEngine } from '@shikijs/types';
2
+
3
+ interface JavaScriptRegexEngineOptions {
4
+ /**
5
+ * Whether to allow invalid regex patterns.
6
+ */
7
+ forgiving?: boolean;
8
+ /**
9
+ * Cache for regex patterns.
10
+ */
11
+ cache?: Map<string, RegExp | Error>;
12
+ /**
13
+ * Custom pattern to RegExp constructor.
14
+ *
15
+ * By default `oniguruma-to-js` is used.
16
+ */
17
+ regexConstructor?: (pattern: string) => RegExp;
18
+ }
19
+ /**
20
+ * The default RegExp constructor for JavaScript regex engine.
21
+ */
22
+ declare function defaultJavaScriptRegexConstructor(pattern: string): RegExp;
23
+ declare class JavaScriptScanner implements PatternScanner {
24
+ patterns: string[];
25
+ cache: Map<string, RegExp | Error>;
26
+ forgiving: boolean;
27
+ regexConstructor: (pattern: string) => RegExp;
28
+ regexps: (RegExp | null)[];
29
+ constructor(patterns: string[], cache: Map<string, RegExp | Error>, forgiving: boolean, regexConstructor?: (pattern: string) => RegExp);
30
+ findNextMatchSync(string: string | RegexEngineString, startPosition: number): {
31
+ index: number;
32
+ captureIndices: {
33
+ end: number;
34
+ start: number;
35
+ length: number;
36
+ }[];
37
+ } | null;
38
+ }
39
+ /**
40
+ * Use the modern JavaScript RegExp engine to implement the OnigScanner.
41
+ *
42
+ * As Oniguruma regex is more powerful than JavaScript regex, some patterns may not be supported.
43
+ * Errors will be thrown when parsing TextMate grammars with unsupported patterns.
44
+ * Set `forgiving` to `true` to ignore these errors and skip the unsupported patterns.
45
+ *
46
+ * @experimental
47
+ */
48
+ declare function createJavaScriptRegexEngine(options?: JavaScriptRegexEngineOptions): RegexEngine;
49
+
50
+ export { type JavaScriptRegexEngineOptions, JavaScriptScanner, createJavaScriptRegexEngine, defaultJavaScriptRegexConstructor };
@@ -0,0 +1,50 @@
1
+ import { PatternScanner, RegexEngineString, RegexEngine } from '@shikijs/types';
2
+
3
+ interface JavaScriptRegexEngineOptions {
4
+ /**
5
+ * Whether to allow invalid regex patterns.
6
+ */
7
+ forgiving?: boolean;
8
+ /**
9
+ * Cache for regex patterns.
10
+ */
11
+ cache?: Map<string, RegExp | Error>;
12
+ /**
13
+ * Custom pattern to RegExp constructor.
14
+ *
15
+ * By default `oniguruma-to-js` is used.
16
+ */
17
+ regexConstructor?: (pattern: string) => RegExp;
18
+ }
19
+ /**
20
+ * The default RegExp constructor for JavaScript regex engine.
21
+ */
22
+ declare function defaultJavaScriptRegexConstructor(pattern: string): RegExp;
23
+ declare class JavaScriptScanner implements PatternScanner {
24
+ patterns: string[];
25
+ cache: Map<string, RegExp | Error>;
26
+ forgiving: boolean;
27
+ regexConstructor: (pattern: string) => RegExp;
28
+ regexps: (RegExp | null)[];
29
+ constructor(patterns: string[], cache: Map<string, RegExp | Error>, forgiving: boolean, regexConstructor?: (pattern: string) => RegExp);
30
+ findNextMatchSync(string: string | RegexEngineString, startPosition: number): {
31
+ index: number;
32
+ captureIndices: {
33
+ end: number;
34
+ start: number;
35
+ length: number;
36
+ }[];
37
+ } | null;
38
+ }
39
+ /**
40
+ * Use the modern JavaScript RegExp engine to implement the OnigScanner.
41
+ *
42
+ * As Oniguruma regex is more powerful than JavaScript regex, some patterns may not be supported.
43
+ * Errors will be thrown when parsing TextMate grammars with unsupported patterns.
44
+ * Set `forgiving` to `true` to ignore these errors and skip the unsupported patterns.
45
+ *
46
+ * @experimental
47
+ */
48
+ declare function createJavaScriptRegexEngine(options?: JavaScriptRegexEngineOptions): RegexEngine;
49
+
50
+ export { type JavaScriptRegexEngineOptions, JavaScriptScanner, createJavaScriptRegexEngine, defaultJavaScriptRegexConstructor };
package/dist/index.mjs ADDED
@@ -0,0 +1,128 @@
1
+ import { onigurumaToRegexp } from 'oniguruma-to-js';
2
+ import { rewrite } from 'regex';
3
+
4
+ var __defProp = Object.defineProperty;
5
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
6
+ var __publicField = (obj, key, value) => {
7
+ __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
8
+ return value;
9
+ };
10
+ const MAX = 4294967295;
11
+ function defaultJavaScriptRegexConstructor(pattern) {
12
+ pattern = pattern.replaceAll("[^\\s[-?:,\\[\\]{}#&*!|>'\"%@`]]", "[^\\s\\-?:,\\[\\]{}#&*!|>'\"%@`]");
13
+ const rewritten = rewrite(pattern, {
14
+ flags: "dgm",
15
+ unicodeSetsPlugin: null,
16
+ disable: {
17
+ n: true,
18
+ v: true,
19
+ x: true
20
+ }
21
+ });
22
+ return onigurumaToRegexp(
23
+ rewritten.expression,
24
+ {
25
+ flags: "dgm",
26
+ ignoreContiguousAnchors: true
27
+ }
28
+ );
29
+ }
30
+ class JavaScriptScanner {
31
+ constructor(patterns, cache, forgiving, regexConstructor = defaultJavaScriptRegexConstructor) {
32
+ this.patterns = patterns;
33
+ this.cache = cache;
34
+ this.forgiving = forgiving;
35
+ this.regexConstructor = regexConstructor;
36
+ __publicField(this, "regexps");
37
+ this.regexps = patterns.map((p) => {
38
+ const cached = cache?.get(p);
39
+ if (cached) {
40
+ if (cached instanceof RegExp) {
41
+ return cached;
42
+ }
43
+ if (forgiving)
44
+ return null;
45
+ throw cached;
46
+ }
47
+ try {
48
+ const regex = regexConstructor(p);
49
+ cache?.set(p, regex);
50
+ return regex;
51
+ } catch (e) {
52
+ cache?.set(p, e);
53
+ if (forgiving)
54
+ return null;
55
+ throw e;
56
+ }
57
+ });
58
+ }
59
+ findNextMatchSync(string, startPosition) {
60
+ const str = typeof string === "string" ? string : string.content;
61
+ const pending = [];
62
+ function toResult(index, match) {
63
+ return {
64
+ index,
65
+ captureIndices: match.indices.map((indice) => {
66
+ if (indice == null) {
67
+ return {
68
+ end: MAX,
69
+ start: MAX,
70
+ length: 0
71
+ };
72
+ }
73
+ return {
74
+ start: indice[0],
75
+ length: indice[1] - indice[0],
76
+ end: indice[1]
77
+ };
78
+ })
79
+ };
80
+ }
81
+ for (let i = 0; i < this.regexps.length; i++) {
82
+ const regexp = this.regexps[i];
83
+ if (!regexp)
84
+ continue;
85
+ try {
86
+ regexp.lastIndex = startPosition;
87
+ const match = regexp.exec(str);
88
+ if (!match)
89
+ continue;
90
+ if (match.index === startPosition) {
91
+ return toResult(i, match);
92
+ }
93
+ pending.push([i, match]);
94
+ } catch (e) {
95
+ if (this.forgiving)
96
+ continue;
97
+ throw e;
98
+ }
99
+ }
100
+ if (pending.length) {
101
+ const minIndex = Math.min(...pending.map((m) => m[1].index));
102
+ for (const [i, match] of pending) {
103
+ if (match.index === minIndex) {
104
+ return toResult(i, match);
105
+ }
106
+ }
107
+ }
108
+ return null;
109
+ }
110
+ }
111
+ function createJavaScriptRegexEngine(options = {}) {
112
+ const {
113
+ forgiving = false,
114
+ cache = /* @__PURE__ */ new Map()
115
+ } = options;
116
+ return {
117
+ createScanner(patterns) {
118
+ return new JavaScriptScanner(patterns, cache, forgiving, options.regexConstructor);
119
+ },
120
+ createString(s) {
121
+ return {
122
+ content: s
123
+ };
124
+ }
125
+ };
126
+ }
127
+
128
+ export { JavaScriptScanner, createJavaScriptRegexEngine, defaultJavaScriptRegexConstructor };
package/package.json ADDED
@@ -0,0 +1,41 @@
1
+ {
2
+ "name": "@shikijs/engine-javascript",
3
+ "type": "module",
4
+ "version": "1.17.0",
5
+ "description": "Engine for Shiki using JavaScript's native RegExp",
6
+ "author": "Anthony Fu <anthonyfu117@hotmail.com>",
7
+ "license": "MIT",
8
+ "homepage": "https://github.com/shikijs/shiki#readme",
9
+ "repository": {
10
+ "type": "git",
11
+ "url": "git+https://github.com/shikijs/shiki.git",
12
+ "directory": "packages/transformers"
13
+ },
14
+ "bugs": "https://github.com/shikijs/shiki/issues",
15
+ "keywords": [
16
+ "shiki",
17
+ "@shikijs/transformers"
18
+ ],
19
+ "sideEffects": false,
20
+ "exports": {
21
+ ".": {
22
+ "types": "./dist/index.d.mts",
23
+ "default": "./dist/index.mjs"
24
+ }
25
+ },
26
+ "main": "./dist/index.mjs",
27
+ "module": "./dist/index.mjs",
28
+ "types": "./dist/index.d.mts",
29
+ "files": [
30
+ "dist"
31
+ ],
32
+ "dependencies": {
33
+ "oniguruma-to-js": "0.3.3",
34
+ "regex": "4.3.2",
35
+ "@shikijs/types": "1.17.0"
36
+ },
37
+ "scripts": {
38
+ "build": "unbuild",
39
+ "dev": "unbuild --stub"
40
+ }
41
+ }