diff --git a/resources/ts/highlight-blade.ts b/resources/ts/highlight-blade.ts index 0e8d685c..117ec63f 100644 --- a/resources/ts/highlight-blade.ts +++ b/resources/ts/highlight-blade.ts @@ -1,54 +1,63 @@ -import { HLJSApi } from 'highlight.js'; +import type { HLJSApi, Language } from 'highlight.js'; + +export default function (hljs: HLJSApi): Language { + // {{ $escaped ? 'true' : 'false' }} + const ESCAPED_TEMPLATE_VARIABLE = { + begin: /\{\{/, + beginScope: 'template-variable', + end: /}}/, + endScope: 'template-variable', + subLanguage: 'php', + }; + + // {{ $unescaped ? '
true
' : 'false
' }} + const UNESCAPED_TEMPLATE_VARIABLE = { + begin: /\{!!/, + beginScope: 'template-variable', + end: /!!}/, + endScope: 'template-variable', + subLanguage: 'php', + }; + + // @php + // $foo = 'bar'; + // @endphp + const RAW_PHP = { + begin: /@php/, + beginScope: 'keyword', + end: /@endphp/, + endScope: 'keyword', + subLanguage: 'php', + }; + + // @somethingLikeThis + const BLADE_DIRECTIVES = { + scope: 'keyword', + match: /@[a-zA-Z]+/, + }; + + // @foreach ($list as $item) + // or + // @foreach($list as $item) + const STATEMENT_AFTER_BLADE_DIRECTIVES = { + begin: /(?<=@[a-zA-Z]+\s?)\(/, + excludeBegin: true, + end: /\)/, + excludeEnd: true, + subLanguage: 'php', + }; -export default function (hljs: HLJSApi) { return { aliases: ['blade'], case_insensitive: false, subLanguage: 'xml', contains: [ hljs.COMMENT(/\{\{--/, /--}}/), - - // output with HTML escaping - { - begin: /\{\{/, - beginScope: 'template-tag', - end: /}}/, - endScope: 'template-tag', - subLanguage: 'php', - }, - - // output with no HTML escaping - { - begin: /\{!!/, - beginScope: 'template-tag', - end: /!!}/, - endScope: 'template-tag', - subLanguage: 'php', - }, - - // directly inserted PHP code - { - begin: /@php/, - beginScope: 'keyword', - end: /@endphp/, - endScope: 'keyword', - subLanguage: 'php', - }, - - // blade syntax - { - scope: 'keyword', - match: /@[a-zA-Z]+/, - }, - - // parameter in blade syntax - { - begin: /(?<=@[a-zA-Z]+\s?)\(/, - excludeBegin: true, - end: /\)/, - excludeEnd: true, - subLanguage: 'php', - }, + ESCAPED_TEMPLATE_VARIABLE, + UNESCAPED_TEMPLATE_VARIABLE, + RAW_PHP, + BLADE_DIRECTIVES, + STATEMENT_AFTER_BLADE_DIRECTIVES, ], }; }