Skip to content

Instantly share code, notes, and snippets.

@Shagshag
Created November 24, 2020 08:45
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 Shagshag/19c83d4168579e35f22ea44be5536fd4 to your computer and use it in GitHub Desktop.
Save Shagshag/19c83d4168579e35f22ea44be5536fd4 to your computer and use it in GitHub Desktop.
nl2br for vue3
// Adapted from https://github.com/inouetakuya/vue-nl2br/
{
props: {
tag: {
type: String,
required: true,
},
text: {
type: String,
required: true,
},
className: {
type: String,
required: false,
},
},
setup(props) {
return () =>
Vue.h(
props.tag,
{
'class': props.className
},
props.text.split('\n').reduce((accumulator, string) => {
if (!Array.isArray(accumulator)) {
return [accumulator, Vue.h('br'), string]
}
return accumulator.concat([Vue.h('br'), string])
})
);
},
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment