javascript - Format string with line breaks into paragraphs -
i'm using contentful cms manage content , pulling in content api.
the content pulled in json object. 1 of keys in object main block of text entry pulling. string has no actual code in it, have line breaks. in chrome console these appear small return arrow. part of object looks this:
var article = { name: "some name here", content: "lorem ipsum dolor sit amet, consectetur adipiscing elit. sed lobortis libero lacus. morbi non elit purus. mauris eu dictum urna. nam vulputate venenatis diam nec feugiat. praesent dapibus viverra ullamcorper. donec euismod purus vitae risus dignissim, id pulvinar enim tristique. donec sed justo justo. sed et ornare lacus. lorem ipsum dolor sit amet, consectetur adipiscing elit. sed lobortis libero lacus. morbi non elit purus. mauris eu dictum urna. nam vulputate venenatis diam nec feugiat. praesent dapibus viverra ullamcorper. donec euismod purus vitae risus dignissim, id pulvinar enim tristique. donec sed justo justo. sed et ornare lacus. lorem ipsum dolor sit amet, consectetur adipiscing elit. sed lobortis libero lacus. morbi non elit purus. mauris eu dictum urna. nam vulputate venenatis diam nec feugiat. praesent dapibus viverra ullamcorper. donec euismod purus vitae risus dignissim, id pulvinar enim tristique. donec sed justo justo. sed et ornare lacus." }
notice line breaks within content field. how take article.content
, format these paragraphs actual <p>
tags? want render html so:
<p>lorem ipsum dolor sit amet, consectetur adipiscing elit. sed lobortis libero lacus. morbi non elit purus. mauris eu dictum urna. nam vulputate venenatis diam nec feugiat. praesent dapibus viverra ullamcorper. donec euismod purus vitae risus dignissim, id pulvinar enim tristique. donec sed justo justo. sed et ornare lacus.</p> <p>lorem ipsum dolor sit amet, consectetur adipiscing elit. sed lobortis libero lacus. morbi non elit purus. mauris eu dictum urna. nam vulputate venenatis diam nec feugiat. praesent dapibus viverra ullamcorper. donec euismod purus vitae risus dignissim, id pulvinar enim tristique. donec sed justo justo. sed et ornare lacus.</p> <p>lorem ipsum dolor sit amet, consectetur adipiscing elit. sed lobortis libero lacus. morbi non elit purus. mauris eu dictum urna. nam vulputate venenatis diam nec feugiat. praesent dapibus viverra ullamcorper. donec euismod purus vitae risus dignissim, id pulvinar enim tristique. donec sed justo justo. sed et ornare lacus.</p>
the easiest way split
on \n
, rejoin on </p><p>
:
- split: split takes string , breaks apart string, example if have string
1,2,3,4
, split on,
, end javascript array[1, 2, 3, 4]
. - rejoin: join takes javascript array, , glues string using string glue. example, if take array have
[1, 2, 3, 4]
, join on#
, end string1#2#3#4
.
so following these steps swapping out ,
\n
, #
</p><p>
able make string 1</p><p>2</p><p>3</p><p>4
. almost right, notice don't have starting <p>
or ending </p>
throw on beginning , end of string:
var paragraphs = '<p>' + article.content.split("\n").join('</p><p>') + '</p>';
check out this jsbin example. top box input, bottom output.
Comments
Post a Comment