Last active
January 2, 2018 01:47
-
-
Save FireNeslo/e9c9be191ce90b8f7005 to your computer and use it in GitHub Desktop.
Simple es6 heredoc thingy
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
var html = heredoc ` | |
<section> | |
<h1> title</h1> | |
<p>content</p> | |
</section> | |
` |
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 leastSpaces(length, line) { | |
if(!line.length) return length | |
var spaces = line.match(/^\s+/).pop().length | |
return spaces < length ? spaces : length | |
} | |
function heredoc(strings) { | |
var lines = strings.join('').split(/\r?\n/) | |
var spaces = lines.reduce(leastSpaces, Infinity) | |
return lines.map(line => line.substring(spaces)).join('\n') | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment