Created
May 6, 2021 22:33
-
-
Save bendera/8e97ab2cca4a7e6872238d7a7d3f54a2 to your computer and use it in GitHub Desktop.
lit-html newline to <br> directive
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
import {directive, NodePart, html} from 'lit-html'; | |
export const nl2br = directive((val: string) => (part: NodePart) => { | |
if (!(part instanceof NodePart)) { | |
throw new Error('nl2br can only be used in content bindings'); | |
} | |
const lines = val.split('\n'); | |
const l = lines.length; | |
part.setValue(lines.map((line: string, i) => i === l - 1 ? line : html`${line}<br>`)); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment