Skip to content

Commit

Permalink
refactor: highlight-blade.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
yilanboy committed Jan 8, 2025
1 parent e18bfe4 commit ffc8c85
Showing 1 changed file with 53 additions and 44 deletions.
97 changes: 53 additions & 44 deletions resources/ts/highlight-blade.ts
Original file line number Diff line number Diff line change
@@ -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 ? '<p>true</p>' : '<p>false</p>' }}
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,
],
};
}

0 comments on commit ffc8c85

Please sign in to comment.