Skip to content

Instantly share code, notes, and snippets.

@coolreader18
Last active January 30, 2018 20:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save coolreader18/40e875bf2a400d4968d0a30cc840d885 to your computer and use it in GitHub Desktop.
Save coolreader18/40e875bf2a400d4968d0a30cc840d885 to your computer and use it in GitHub Desktop.
A template literal tag that lets you do indented multiline template strings.

Indented Multiline Template Literals

Look at example.js

It bases the indentation of the second line of the template literal.

Make sure you put the m in front of it!

import m from 'multiline-templiteral.js';
console.log(m`hey there
this is a multiline
${"expressions work to".toUpperCase()}`)
// is equivalent to
`hey there
this is a multiline
EXPRESSIONS WORK TO`
function m(strings,...reps) {
let lines = strings.reduce((str, cur, i) => str + cur + (reps[i] || ""), "").split("\n");
let ws = new RegExp(`^${/^(\s+)/.exec(lines[1])[0]}`);
return lines.map(line => line.replace(ws, "")).join("\n");
}
try{export m as default}catch(e){try{module.exports=m}catch(r){}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment