@lume/live-code
Version:
A `<live-code>` element for editable code with live output.
15 lines (13 loc) • 473 B
text/typescript
// Adapted from http://npmjs.com/min-indent
export function minIndent(string: string) {
const match = string.match(/^[ \t]*(?=\S)/gm)
if (!match) return 0
return match.reduce((r, a) => Math.min(r, a.length), Infinity)
}
// Adapted from http://npmjs.com/strip-indent
export function stripIndent(string: string) {
const indent = minIndent(string)
if (indent === 0) return string
const regex = new RegExp(`^[ \\t]{${indent}}`, 'gm')
return string.replace(regex, '')
}