feat(codemirror): add Caddyfile StreamLanguage mode
Defines a minimal StreamLanguage tokenizer for CodeMirror 6 that recognises Caddy directives, named-matchers (@-prefix), comments, strings and braces via cm6 highlight-tags. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
parent
7d5a7576bf
commit
97420b93cf
1 changed files with 31 additions and 0 deletions
31
lib/codemirror/caddyfile-mode.ts
Normal file
31
lib/codemirror/caddyfile-mode.ts
Normal file
|
|
@ -0,0 +1,31 @@
|
||||||
|
import { StreamLanguage, type StreamParser } from '@codemirror/language'
|
||||||
|
|
||||||
|
const CADDY_DIRECTIVES = new Set([
|
||||||
|
'reverse_proxy', 'encode', 'file_server', 'handle', 'handle_errors',
|
||||||
|
'root', 'header', 'redir', 'rewrite', 'respond', 'route', 'tls',
|
||||||
|
'log', 'basicauth', 'request_body', 'try_files', 'php_fastcgi',
|
||||||
|
'templates', 'import', 'bind', 'metrics', 'admin', 'auto_https',
|
||||||
|
])
|
||||||
|
const CADDY_GLOBAL = new Set(['email', 'storage', 'order', 'servers', 'log'])
|
||||||
|
|
||||||
|
const parser: StreamParser<unknown> = {
|
||||||
|
token(stream) {
|
||||||
|
if (stream.eatSpace()) return null
|
||||||
|
if (stream.match(/^#.*/)) return 'comment'
|
||||||
|
if (stream.match(/^"(?:[^"\\]|\\.)*"/)) return 'string'
|
||||||
|
if (stream.match(/^@[A-Za-z_][\w-]*/)) return 'variableName'
|
||||||
|
if (stream.match(/^[{}]/)) return 'brace'
|
||||||
|
const word = stream.match(/^[A-Za-z_][\w.-]*/) as RegExpMatchArray | null
|
||||||
|
if (word) {
|
||||||
|
const w = word[0]
|
||||||
|
if (CADDY_DIRECTIVES.has(w)) return 'keyword'
|
||||||
|
if (CADDY_GLOBAL.has(w)) return 'typeName'
|
||||||
|
return 'variableName'
|
||||||
|
}
|
||||||
|
stream.next()
|
||||||
|
return null
|
||||||
|
},
|
||||||
|
languageData: { commentTokens: { line: '#' } },
|
||||||
|
}
|
||||||
|
|
||||||
|
export const caddyfileLanguage = StreamLanguage.define(parser)
|
||||||
Loading…
Add table
Add a link
Reference in a new issue