@render-props/paragraphs
Version:
A function as child component which provides an interface for creating paragraphs with line breaks from raw text with `\n` new lines. The default render child is: ```js const defaultParagraph = props => <p key={props.key} children={props.text}/> ```
20 lines (13 loc) • 407 B
JavaScript
import React from 'react'
import limitLines from './limitLines'
const _breakRe = /(\n)/g
export default function toBreaks (rawText) {
if (!rawText) {
return rawText
}
rawText = limitLines(rawText)
function breaker(line, x) {
return line.match(_breakRe) ? React.createElement('br', {key: `${x}--${line}`}) : line
}
return rawText.split(_breakRe).map(breaker).filter(line => line)
}