-
-
Save mpryvkin/0c46e2493b450f92492e8e9a46ad5d97 to your computer and use it in GitHub Desktop.
JS Beautify hack to work with Blade directives (Laravel)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Beautifier(html_source, options, js_beautify, css_beautify) { | |
//Wrapper function to invoke all the necessary constructors and deal with the output. | |
html_source = html_source || ''; | |
// BEGIN | |
html_source = html_source.replace(/\{\{((?:(?!\}\}).)+)\}\}/g, function (m, c) { | |
if (c) { | |
c = c.replace(/(^[ \t]*|[ \t]*$)/g, ''); | |
c = c.replace(/'/g, '''); | |
c = c.replace(/"/g, '"'); | |
c = encodeURIComponent(c); | |
} | |
return "{{" + c + "}}"; | |
}); | |
html_source = html_source.replace(/^[ \t]*@([a-z]+)([^\r\n]*)$/gim, function (m, d, c) { | |
var ce = c; | |
if (ce) { | |
ce = ce.replace(/'/g, '''); | |
ce = ce.replace(/"/g, '"'); | |
ce = "|" + encodeURIComponent(ce); | |
} | |
switch (d) { | |
case 'break': | |
case 'case': | |
case 'continue': | |
case 'csrf': | |
case 'else': | |
case 'elseif': | |
case 'empty': | |
case 'extends': | |
case 'include': | |
case 'includeFirst': | |
case 'includeIf': | |
case 'includeWhen': | |
case 'inject': | |
case 'json': | |
case 'method': | |
case 'parent': | |
case 'stack': | |
case 'yield': | |
return "<blade " + d + ce + "/>"; | |
case 'section': | |
if(c.match(/[ \t]*\([ \t]*['"][^'"]*['"][ \t]*\)/i)){ | |
return "<blade " + d + ce + ">"; | |
} else { | |
return "<blade " + d + ce + "/>"; | |
} | |
case 'show': | |
case 'stop': | |
return "</blade " + d + ce + ">"; | |
default: | |
if (d.startsWith('end')) { | |
return "</blade " + d + ce + ">"; | |
} else { | |
return "<blade " + d + ce + ">"; | |
} | |
} | |
}); | |
// END | |
... | |
var sweet_code = multi_parser.output.join('').replace(/[\r\n\t ]+$/, ''); | |
// BEGIN | |
sweet_code = sweet_code.replace(/^([ \t]*)<\/?blade ([a-z]+)\|?([^>\/]+)?\/?>$/gim, function (m, s, d, c) { | |
if (c) { | |
c = decodeURIComponent(c); | |
c = c.replace(/'/g, "'"); | |
c = c.replace(/"/g, '"'); | |
c = c.replace(/^[ \t]*/g, ''); | |
} else { | |
c = ""; | |
} | |
if (!s) { | |
s = ""; | |
} | |
return s + "@" + d + c; | |
}); | |
sweet_code = sweet_code.replace(/\{\{((?:(?!\}\}).)+)\}\}/g, function (m, c) { | |
if (c) { | |
c = decodeURIComponent(c); | |
c = c.replace(/'/g, "'"); | |
c = c.replace(/"/g, '"'); | |
c = c.replace(/(^[ \t]*|[ \t]*$)/g, ' '); | |
} | |
return "{{" + c + "}}"; | |
}); | |
// END | |
... | |
return sweet_code; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@valeryan
I don't see how it's out of scope for js-beautify since VSCode uses js-beautify internally. The most elegant solution is for the native reindent of VSC to function with templating DSL in popular web frameworks, and this makes sense, since people are using VSCode for popular web frameworks, not for C, C++ and Go. That being said, a 3rd party reindent is fine too.