Skip to content

Instantly share code, notes, and snippets.

@fulopattila122
Created January 12, 2025 11:12
Show Gist options
  • Save fulopattila122/bbceea3d09d8c7356d53065c6f3e223c to your computer and use it in GitHub Desktop.
Save fulopattila122/bbceea3d09d8c7356d53065c6f3e223c to your computer and use it in GitHub Desktop.
PrismJS Blade Highlighter (minimal feature set)
Prism.languages.blade = {
'comment': /^\{\{\-\-[\s\S]*?\-\-\}\}$/,
'directive-name': {
pattern: /^@[\w]+/,
alias: 'keyword'
},
'keyword': /\b(?:if|elseif|else|endif|foreach|endforeach|for|endfor|forelse|endforelse|unless|endunless|isset|endisset|empty|endempty|auth|endauth|guest|endguest|production|endproduction|env|endenv|session|endession|while|endwhile|switch|endswitch|case|default|continue|break|class|php|endphp|checked|selected|disabled|readonly|required|include|each|once|push|endpush)\b/,
'string': {
pattern: /("|')(?:\\.|(?!\1)[^\\\r\n])*\1/,
inside: {
'punctuation': /^['"]|['"]$/
}
},
'boolean': /\b(?:false|null|true)\b/,
'number': /\b0x[\dA-Fa-f]+|(?:\b\d+(?:\.\d*)?|\B\.\d+)(?:[Ee][-+]?\d+)?/,
'operator': [
{
pattern: /(\s)(?:and|b-and|b-or|b-xor|ends with|in|is|matches|not|or|same as|starts with)(?=\s)/,
lookbehind: true
},
/[=<>]=?|!=|\*\*?|\/\/?|\?:?|[-+~%|]/
],
'punctuation': /[()\[\]{}:.,]/
};
Prism.hooks.add('before-tokenize', function (env) {
if (env.language !== 'blade') {
return;
}
var pattern = /\{(?:\{[\s\S]*?\}|\!\![\s\S]*?\!\!|\{[\s\S]*?\})\}|@[\w]+/g;
Prism.languages['markup-templating'].buildPlaceholders(env, 'blade', pattern);
});
Prism.hooks.add('after-tokenize', function (env) {
Prism.languages['markup-templating'].tokenizePlaceholders(env, 'blade');
});
@fulopattila122
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment