Created
December 18, 2017 13:37
-
-
Save brnpimentel/3660b71dbc68691cdc8ac41cec379e2f to your computer and use it in GitHub Desktop.
JS Beautify hack to works with blade directives (laravel)
This file contains 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 style_html(html_source, options, js_beautify, css_beautify) { | |
html_source = html_source.replace(/\@([^\n\s]*)/ig, "<blade $1/>"); | |
... | |
sweet_code = sweet_code.replace(/<blade ([^\n]*)\/>/ig, "@$1"); | |
sweet_code = sweet_code.replace(/\(\ \'/ig, "('"); | |
... | |
return sweet_code; | |
} |
I made some improvement from codes above, this is not perfect but makes blade formatter cleaner
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]+)([^\n]*)$/gim, function (m, d, c) {
if (c) {
c = c.replace(/'/g, ''');
c = c.replace(/"/g, '"');
c = "|" + encodeURIComponent(c);
}
switch (d) {
case 'break':
case 'continue':
case 'empty':
case 'else':
case 'elseif':
case 'extends':
case 'case':
case 'csrf':
case 'include':
case 'json':
case 'method':
case 'parent':
case 'stack':
case 'yield':
return "<blade " + d + c + "/>";
break;
default:
if (d.startsWith('end')) {
return "</blade " + d + c + ">";
} else {
return "<blade " + d + c + ">";
}
break;
}
});
// 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;
}
hope this will help
happy coding!
I also make the gist for js-beautify hack for HookyQR.beautify version 1.4.2 here the gist
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
ugh this is very frustrating.. several blade extensions in vscode but not one properly handles formatting inside blade files.. and this solution here while great and thanks for sharing is much too complicated
here's to hoping someone takes this solution and makes it into an easy-to-install extension