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:
Scrum4Me Agent 2026-05-13 23:37:30 +02:00
parent 7d5a7576bf
commit 97420b93cf

View 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)