@getpaseo/highlight 0.7.0-beta.2 → 0.7.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/dist/astro/parser.d.ts +11 -0
- package/dist/astro/parser.js +228 -0
- package/dist/parsers.js +3 -0
- package/package.json +3 -2
- package/src/astro/LICENSE +21 -0
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { type Input, type PartialParse, Parser, type TreeFragment } from "@lezer/common";
|
|
2
|
+
interface Range {
|
|
3
|
+
from: number;
|
|
4
|
+
to: number;
|
|
5
|
+
}
|
|
6
|
+
declare class AstroParser extends Parser {
|
|
7
|
+
createParse(input: Input, fragments: readonly TreeFragment[], ranges: readonly Range[]): PartialParse;
|
|
8
|
+
}
|
|
9
|
+
export declare const astroParser: AstroParser;
|
|
10
|
+
export {};
|
|
11
|
+
//# sourceMappingURL=parser.d.ts.map
|
|
@@ -0,0 +1,228 @@
|
|
|
1
|
+
// Adapted from @fazelstudio/codemirror-lang-astro@0.2.0 (MIT).
|
|
2
|
+
// The parser is kept pure so server-side diff highlighting does not load editor-only modules.
|
|
3
|
+
import { Parser, parseMixed, } from "@lezer/common";
|
|
4
|
+
import { parser as cssParser } from "@lezer/css";
|
|
5
|
+
import { parser as htmlParser } from "@lezer/html";
|
|
6
|
+
import { parser as jsParser } from "@lezer/javascript";
|
|
7
|
+
const jsxParser = jsParser.configure({ dialect: "ts jsx" });
|
|
8
|
+
const typescriptParser = jsParser.configure({ dialect: "ts" });
|
|
9
|
+
function isSpace(code) {
|
|
10
|
+
return code === 32 || code === 9 || code === 10 || code === 13;
|
|
11
|
+
}
|
|
12
|
+
function isRegexStart(text, position) {
|
|
13
|
+
let previous = position - 1;
|
|
14
|
+
while (previous >= 0 && isSpace(text.charCodeAt(previous)))
|
|
15
|
+
previous--;
|
|
16
|
+
if (previous < 0)
|
|
17
|
+
return true;
|
|
18
|
+
const character = text[previous];
|
|
19
|
+
if (/[)\]}<"'`\d]/.test(character))
|
|
20
|
+
return false;
|
|
21
|
+
const code = text.charCodeAt(previous);
|
|
22
|
+
if (code === 62)
|
|
23
|
+
return previous > 0 && text.charCodeAt(previous - 1) === 61;
|
|
24
|
+
if (!/[A-Za-z_$]/.test(character))
|
|
25
|
+
return true;
|
|
26
|
+
let start = previous;
|
|
27
|
+
while (start >= 0 && /[A-Za-z0-9_$]/.test(text[start]))
|
|
28
|
+
start--;
|
|
29
|
+
const keyword = text.slice(start + 1, previous + 1);
|
|
30
|
+
return /^(return|typeof|instanceof|in|of|new|void|delete|yield|await|case|do|else|throw|extends|assert|with)$/.test(keyword);
|
|
31
|
+
}
|
|
32
|
+
function skipQuotedText(text, opening) {
|
|
33
|
+
const quote = text.charCodeAt(opening);
|
|
34
|
+
for (let position = opening + 1; position < text.length; position++) {
|
|
35
|
+
const code = text.charCodeAt(position);
|
|
36
|
+
if (code === 92)
|
|
37
|
+
position++;
|
|
38
|
+
else if (code === quote)
|
|
39
|
+
return position;
|
|
40
|
+
}
|
|
41
|
+
return text.length - 1;
|
|
42
|
+
}
|
|
43
|
+
function skipLineComment(text, opening) {
|
|
44
|
+
const newline = text.indexOf("\n", opening + 2);
|
|
45
|
+
return newline >= 0 ? newline : text.length - 1;
|
|
46
|
+
}
|
|
47
|
+
function skipBlockComment(text, opening) {
|
|
48
|
+
const closing = text.indexOf("*/", opening + 2);
|
|
49
|
+
return closing >= 0 ? closing + 1 : text.length - 1;
|
|
50
|
+
}
|
|
51
|
+
function skipRegex(text, opening) {
|
|
52
|
+
let isInCharacterClass = false;
|
|
53
|
+
for (let position = opening + 1; position < text.length; position++) {
|
|
54
|
+
const code = text.charCodeAt(position);
|
|
55
|
+
if (code === 10 || code === 13)
|
|
56
|
+
return position;
|
|
57
|
+
if (code === 92)
|
|
58
|
+
position++;
|
|
59
|
+
else if (isInCharacterClass && code === 93)
|
|
60
|
+
isInCharacterClass = false;
|
|
61
|
+
else if (!isInCharacterClass && code === 91)
|
|
62
|
+
isInCharacterClass = true;
|
|
63
|
+
else if (!isInCharacterClass && code === 47)
|
|
64
|
+
return position;
|
|
65
|
+
}
|
|
66
|
+
return text.length - 1;
|
|
67
|
+
}
|
|
68
|
+
function findClosingBrace(text, opening) {
|
|
69
|
+
let depth = 0;
|
|
70
|
+
for (let position = opening; position < text.length; position++) {
|
|
71
|
+
const code = text.charCodeAt(position);
|
|
72
|
+
if (code === 47 && text.charCodeAt(position + 1) === 47) {
|
|
73
|
+
position = skipLineComment(text, position);
|
|
74
|
+
}
|
|
75
|
+
else if (code === 47 && text.charCodeAt(position + 1) === 42) {
|
|
76
|
+
position = skipBlockComment(text, position);
|
|
77
|
+
}
|
|
78
|
+
else if (code === 47 && isRegexStart(text, position)) {
|
|
79
|
+
position = skipRegex(text, position);
|
|
80
|
+
}
|
|
81
|
+
else if (code === 34 || code === 39 || code === 96) {
|
|
82
|
+
position = skipQuotedText(text, position);
|
|
83
|
+
}
|
|
84
|
+
else if (code === 123) {
|
|
85
|
+
depth++;
|
|
86
|
+
}
|
|
87
|
+
else if (code === 125 && --depth === 0) {
|
|
88
|
+
return position;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
return -1;
|
|
92
|
+
}
|
|
93
|
+
function findExpressions(text) {
|
|
94
|
+
const ranges = [];
|
|
95
|
+
for (let position = 0; position < text.length; position++) {
|
|
96
|
+
if (text.charCodeAt(position) !== 123)
|
|
97
|
+
continue;
|
|
98
|
+
const closing = findClosingBrace(text, position);
|
|
99
|
+
if (closing < 0)
|
|
100
|
+
break;
|
|
101
|
+
ranges.push({ from: position, to: closing });
|
|
102
|
+
position = closing;
|
|
103
|
+
}
|
|
104
|
+
return ranges;
|
|
105
|
+
}
|
|
106
|
+
function maskExpressions(text) {
|
|
107
|
+
const characters = text.split("");
|
|
108
|
+
for (const { from, to } of findExpressions(text)) {
|
|
109
|
+
for (let position = from + 1; position < to; position++)
|
|
110
|
+
characters[position] = "a";
|
|
111
|
+
}
|
|
112
|
+
return characters.join("");
|
|
113
|
+
}
|
|
114
|
+
function expressionOverlays(node, input) {
|
|
115
|
+
const overlays = findExpressions(input.read(node.from, node.to)).map(({ from, to }) => ({
|
|
116
|
+
from: node.from + from + 1,
|
|
117
|
+
to: node.from + to,
|
|
118
|
+
}));
|
|
119
|
+
return overlays.length > 0 ? overlays : null;
|
|
120
|
+
}
|
|
121
|
+
function isFenceEnd(text, position) {
|
|
122
|
+
if (position >= text.length)
|
|
123
|
+
return true;
|
|
124
|
+
const code = text.charCodeAt(position);
|
|
125
|
+
return code === 10 || code === 13 || code === 32 || code === 9;
|
|
126
|
+
}
|
|
127
|
+
function findFrontmatter(text) {
|
|
128
|
+
const from = text.charCodeAt(0) === 0xfeff ? 1 : 0;
|
|
129
|
+
if (!text.startsWith("---", from) || !isFenceEnd(text, from + 3))
|
|
130
|
+
return null;
|
|
131
|
+
let newline = text.indexOf("\n", from + 3);
|
|
132
|
+
while (newline >= 0) {
|
|
133
|
+
const closing = newline + 1;
|
|
134
|
+
if (text.startsWith("---", closing) && isFenceEnd(text, closing + 3)) {
|
|
135
|
+
return { from, to: closing + 3 };
|
|
136
|
+
}
|
|
137
|
+
newline = text.indexOf("\n", closing);
|
|
138
|
+
}
|
|
139
|
+
return null;
|
|
140
|
+
}
|
|
141
|
+
function maskDocument(text, frontmatter) {
|
|
142
|
+
if (!frontmatter)
|
|
143
|
+
return maskExpressions(text);
|
|
144
|
+
return (text.slice(0, frontmatter.from) +
|
|
145
|
+
"<!--" +
|
|
146
|
+
text.slice(frontmatter.from + 4, frontmatter.to - 3) +
|
|
147
|
+
"-->" +
|
|
148
|
+
maskExpressions(text.slice(frontmatter.to)));
|
|
149
|
+
}
|
|
150
|
+
function getOpenTagAttributes(node, input) {
|
|
151
|
+
const attributes = Object.create(null);
|
|
152
|
+
const openTag = node.getChild("OpenTag");
|
|
153
|
+
if (!openTag)
|
|
154
|
+
return attributes;
|
|
155
|
+
for (const attribute of openTag.getChildren("Attribute")) {
|
|
156
|
+
const name = attribute.getChild("AttributeName");
|
|
157
|
+
if (!name)
|
|
158
|
+
continue;
|
|
159
|
+
const value = attribute.getChild("AttributeValue") || attribute.getChild("UnquotedAttributeValue");
|
|
160
|
+
const key = input.read(name.from, name.to).toLowerCase();
|
|
161
|
+
attributes[key] = value ? input.read(value.from, value.to).replace(/^["']|["']$/g, "") : "";
|
|
162
|
+
}
|
|
163
|
+
return attributes;
|
|
164
|
+
}
|
|
165
|
+
function nestedLanguage(node, input) {
|
|
166
|
+
if (node.name === "Comment") {
|
|
167
|
+
const isFrontmatter = input.read(node.from, node.from + 3) === "---" && input.read(node.to - 3, node.to) === "---";
|
|
168
|
+
const from = node.from + 4;
|
|
169
|
+
const to = node.to - 3;
|
|
170
|
+
return isFrontmatter && to > from
|
|
171
|
+
? { parser: typescriptParser, overlay: [{ from, to }] }
|
|
172
|
+
: null;
|
|
173
|
+
}
|
|
174
|
+
const canContainExpression = node.name === "Text" ||
|
|
175
|
+
node.name === "UnquotedAttributeValue" ||
|
|
176
|
+
node.name === "AttributeValue";
|
|
177
|
+
if (canContainExpression) {
|
|
178
|
+
const overlay = expressionOverlays(node, input);
|
|
179
|
+
return overlay ? { parser: jsxParser, overlay } : null;
|
|
180
|
+
}
|
|
181
|
+
if (node.name === "StyleText")
|
|
182
|
+
return { parser: cssParser };
|
|
183
|
+
if (node.name !== "ScriptText" || !node.node.parent)
|
|
184
|
+
return null;
|
|
185
|
+
const attributes = getOpenTagAttributes(node.node.parent, input);
|
|
186
|
+
if (attributes.src)
|
|
187
|
+
return null;
|
|
188
|
+
const language = (attributes.lang || attributes.type || "").toLowerCase();
|
|
189
|
+
let dialect = "";
|
|
190
|
+
if (language.includes("tsx"))
|
|
191
|
+
dialect = "ts jsx";
|
|
192
|
+
else if (language.includes("typescript") || language === "ts")
|
|
193
|
+
dialect = "ts";
|
|
194
|
+
else if (language.includes("jsx"))
|
|
195
|
+
dialect = "jsx";
|
|
196
|
+
return { parser: dialect ? jsParser.configure({ dialect }) : jsParser };
|
|
197
|
+
}
|
|
198
|
+
class CompletedParse {
|
|
199
|
+
constructor(tree) {
|
|
200
|
+
this.tree = tree;
|
|
201
|
+
this.isDone = false;
|
|
202
|
+
}
|
|
203
|
+
advance() {
|
|
204
|
+
if (this.isDone)
|
|
205
|
+
return null;
|
|
206
|
+
this.isDone = true;
|
|
207
|
+
return this.tree;
|
|
208
|
+
}
|
|
209
|
+
get parsedPos() {
|
|
210
|
+
return this.tree.length;
|
|
211
|
+
}
|
|
212
|
+
stopAt() { }
|
|
213
|
+
get stoppedAt() {
|
|
214
|
+
return null;
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
const mountNestedLanguages = parseMixed(nestedLanguage);
|
|
218
|
+
class AstroParser extends Parser {
|
|
219
|
+
createParse(input, fragments, ranges) {
|
|
220
|
+
const from = ranges[0]?.from ?? 0;
|
|
221
|
+
const to = ranges[0]?.to ?? input.length;
|
|
222
|
+
const text = input.read(from, to);
|
|
223
|
+
const tree = htmlParser.parse(maskDocument(text, findFrontmatter(text)));
|
|
224
|
+
return mountNestedLanguages(new CompletedParse(tree), input, fragments, ranges);
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
export const astroParser = new AstroParser();
|
|
228
|
+
//# sourceMappingURL=parser.js.map
|
package/dist/parsers.js
CHANGED
|
@@ -16,6 +16,7 @@ import { parser as xmlParser } from "@lezer/xml";
|
|
|
16
16
|
import { parser as yamlParser } from "@lezer/yaml";
|
|
17
17
|
import { parser as elixirParser } from "lezer-elixir";
|
|
18
18
|
import { csharpLanguage } from "./csharp/language.js";
|
|
19
|
+
import { astroParser } from "./astro/parser.js";
|
|
19
20
|
import { nixLanguage } from "./nix/language.js";
|
|
20
21
|
import { parser as svelteBaseParser } from "./svelte/parser.js";
|
|
21
22
|
import { configureNesting, defaultNesting } from "./svelte/nesting.js";
|
|
@@ -50,6 +51,8 @@ const languagesByExtension = {
|
|
|
50
51
|
htm: language(htmlParser),
|
|
51
52
|
// Svelte
|
|
52
53
|
svelte: language(svelteBaseParser.configure({ wrap: configureNesting(defaultNesting) })),
|
|
54
|
+
// Astro
|
|
55
|
+
astro: language(astroParser),
|
|
53
56
|
// XML
|
|
54
57
|
xml: language(xmlParser),
|
|
55
58
|
// Java
|
package/package.json
CHANGED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Zulfazli (Fazelllyyy)
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|