From 8119466fb223c7878e3d886a6f964fd03c867a28 Mon Sep 17 00:00:00 2001 From: allenjiang Date: Tue, 7 Jan 2025 00:23:40 +0800 Subject: [PATCH] feat: support blade highlight --- resources/ts/ckeditor/ckeditor.ts | 1 + resources/ts/highlight-blade.ts | 54 +++++++++++++++++++++++++++++++ resources/ts/highlight.ts | 2 ++ 3 files changed, 57 insertions(+) create mode 100644 resources/ts/highlight-blade.ts diff --git a/resources/ts/ckeditor/ckeditor.ts b/resources/ts/ckeditor/ckeditor.ts index 72cc30c1..65d61648 100644 --- a/resources/ts/ckeditor/ckeditor.ts +++ b/resources/ts/ckeditor/ckeditor.ts @@ -171,6 +171,7 @@ ClassicEditor.defaultConfig = { languages: [ { language: 'text', label: 'Plain text' }, // The default language. { language: 'bash', label: 'Bash' }, + { language: 'blade', label: 'Blade' }, { language: 'c', label: 'C' }, { language: 'cs', label: 'C#' }, { language: 'cpp', label: 'C++' }, diff --git a/resources/ts/highlight-blade.ts b/resources/ts/highlight-blade.ts new file mode 100644 index 00000000..172aa869 --- /dev/null +++ b/resources/ts/highlight-blade.ts @@ -0,0 +1,54 @@ +import { HLJSApi } from 'highlight.js'; + +export default function (hljs: HLJSApi) { + return { + aliases: ['blade'], + case_insensitive: false, + subLanguage: 'xml', + contains: [ + hljs.COMMENT(/\{\{--/, /--}}/), + + // output with HTML escaping + { + scope: 'template-variable', + subLanguage: 'php', + begin: /\{\{/, + end: /}}/, + excludeEnd: false, + }, + + // output with no HTML escaping + { + scope: 'template-variable', + subLanguage: 'php', + begin: /\{!!/, + end: /!!}/, + excludeEnd: false, + }, + + // 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', + }, + ], + }; +} diff --git a/resources/ts/highlight.ts b/resources/ts/highlight.ts index 02680053..413be067 100644 --- a/resources/ts/highlight.ts +++ b/resources/ts/highlight.ts @@ -1,5 +1,6 @@ import hljs from 'highlight.js/lib/core'; import bash from 'highlight.js/lib/languages/bash'; +import blade from './highlight-blade.js'; import c from 'highlight.js/lib/languages/c'; import cs from 'highlight.js/lib/languages/csharp'; import cpp from 'highlight.js/lib/languages/cpp'; @@ -33,6 +34,7 @@ declare global { } hljs.registerLanguage('bash', bash); +hljs.registerLanguage('blade', blade); hljs.registerLanguage('c', c); hljs.registerLanguage('cs', cs); hljs.registerLanguage('cpp', cpp);