This time I will share jQuery Plugin and tutorial about Convert A Long Paragraph To Short Paragraphs – Paragraphier, hope it will help you in programming stack.
Paragraphier is a tiny jQuery based app for writers that converts/splits a long paragraph to short paragraphs for better readability.
Type the number of sentences per paragraph and click the Convert button. That’s it.
How to use it:
1. Create a textarea element to accept long paragraphs.
1 |
< textarea id = "text" rows = "5" ></ textarea > |
2. Create a select element from which you can select the amount of sentences per paragraph.
1 |
< select id = "sentences" > |
2 |
< option value = "2" >2</ option > |
3 |
< option value = "4" >4</ option > |
4 |
< option value = "6" >6</ option > |
3. Load the necessary jQuery JavaScript library at the end of the document.
1 |
< script src = "/path/to/cdn/jquery.slim.min.js" ></ script > |
4. The main JavaScript (jQuery script) to enable the long paragraph converter.
01 |
function paragrphier(){ |
03 |
let text = $( '#text' ).val(); |
04 |
let sentences = $( "#sentences option:selected" ).val(); |
06 |
const expectedSentenceCount = sentences; |
08 |
let sentenceCount = 0; |
09 |
let previousIndex = 0; |
11 |
const totalFullStops = (text.match(/,/g) || []).length; |
14 |
$( '#sentenceCount' ).html( 'Total Number Sentences: ' + totalFullStops); |
18 |
for (let i = 0; i < text.length; i++){ |
19 |
let value = text.charAt(i); |
21 |
if (sentenceCount == expectedSentenceCount){ |
22 |
newText += text.substring(previousIndex, i) + '</br></br>' ; |
26 |
if ((totalFullStops - overallCount) < expectedSentenceCount ){ |
27 |
newText += text.substring(previousIndex, (text.length)) + '</br></br>' ; |
40 |
$( '#outputText' ).html(newText); |
5. Create a convert button on the page.
1 |
< button onclick = "paragrphier()" >Paragraphy</ button > |
6. Create an empty container to hold the output.