2023-12-13 18:54:32 +01:00
|
|
|
package lexers
|
2022-07-05 03:51:57 +02:00
|
|
|
|
|
|
|
import (
|
2023-12-13 18:54:32 +01:00
|
|
|
. "github.com/alecthomas/chroma/v2" // nolint
|
2022-07-05 03:51:57 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// Svelte lexer.
|
2023-12-13 18:54:32 +01:00
|
|
|
var Svelte = Register(DelegatingLexer(HTML, MustNewLexer(
|
2022-07-05 03:51:57 +02:00
|
|
|
&Config{
|
|
|
|
Name: "Svelte",
|
|
|
|
Aliases: []string{"svelte"},
|
|
|
|
Filenames: []string{"*.svelte"},
|
|
|
|
MimeTypes: []string{"application/x-svelte"},
|
|
|
|
DotAll: true,
|
|
|
|
},
|
|
|
|
svelteRules,
|
|
|
|
)))
|
|
|
|
|
|
|
|
func svelteRules() Rules {
|
|
|
|
return Rules{
|
|
|
|
"root": {
|
|
|
|
// Let HTML handle the comments, including comments containing script and style tags
|
|
|
|
{`<!--`, Other, Push("comment")},
|
|
|
|
{
|
|
|
|
// Highlight script and style tags based on lang attribute
|
|
|
|
// and allow attributes besides lang
|
|
|
|
`(<\s*(?:script|style).*?lang\s*=\s*['"])` +
|
|
|
|
`(.+?)(['"].*?>)` +
|
|
|
|
`(.+?)` +
|
|
|
|
`(<\s*/\s*(?:script|style)\s*>)`,
|
2023-12-13 18:54:32 +01:00
|
|
|
UsingByGroup(2, 4, Other, Other, Other, Other, Other),
|
2022-07-05 03:51:57 +02:00
|
|
|
nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// Make sure `{` is not inside script or style tags
|
|
|
|
`(?<!<\s*(?:script|style)(?:(?!(?:script|style)\s*>).)*?)` +
|
|
|
|
`{` +
|
|
|
|
`(?!(?:(?!<\s*(?:script|style)).)*?(?:script|style)\s*>)`,
|
|
|
|
Punctuation,
|
|
|
|
Push("templates"),
|
|
|
|
},
|
|
|
|
// on:submit|preventDefault
|
|
|
|
{`(?<=\s+on:\w+(?:\|\w+)*)\|(?=\w+)`, Operator, nil},
|
|
|
|
{`.+?`, Other, nil},
|
|
|
|
},
|
|
|
|
"comment": {
|
|
|
|
{`-->`, Other, Pop(1)},
|
|
|
|
{`.+?`, Other, nil},
|
|
|
|
},
|
|
|
|
"templates": {
|
|
|
|
{`}`, Punctuation, Pop(1)},
|
|
|
|
// Let TypeScript handle strings and the curly braces inside them
|
2023-12-13 18:54:32 +01:00
|
|
|
{`(?<!(?<!\\)\\)(['"` + "`])" + `.*?(?<!(?<!\\)\\)\1`, Using("TypeScript"), nil},
|
2022-07-05 03:51:57 +02:00
|
|
|
// If there is another opening curly brace push to templates again
|
|
|
|
{"{", Punctuation, Push("templates")},
|
|
|
|
{`@(debug|html)\b`, Keyword, nil},
|
|
|
|
{
|
|
|
|
`(#await)(\s+)(\w+)(\s+)(then|catch)(\s+)(\w+)`,
|
2023-12-13 18:54:32 +01:00
|
|
|
ByGroups(Keyword, Text, Using("TypeScript"), Text,
|
|
|
|
Keyword, Text, Using("TypeScript"),
|
2022-07-05 03:51:57 +02:00
|
|
|
),
|
|
|
|
nil,
|
|
|
|
},
|
|
|
|
{`(#|/)(await|each|if|key)\b`, Keyword, nil},
|
|
|
|
{`(:else)(\s+)(if)?\b`, ByGroups(Keyword, Text, Keyword), nil},
|
|
|
|
{`:(catch|then)\b`, Keyword, nil},
|
2023-12-13 18:54:32 +01:00
|
|
|
{`[^{}]+`, Using("TypeScript"), nil},
|
2022-07-05 03:51:57 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|