Comparing version 0.0.1 to 0.0.2
18
index.js
@@ -72,5 +72,7 @@ (function(exportTo) { | ||
} | ||
//Ensure double newline is treated as a paragraph | ||
if(options.TruncateBy.match(/(paragraph(s)?)/)){ | ||
text = String(text).replace('\n\n','<p></p>').replace('\r\n\r\n', '<p></p>'); | ||
//Remove newline seperating paragraphs | ||
text = String(text).replace(/<\/p>(\r?\n)+<p>/gm, '</p><p>'); | ||
//Replace double newlines with paragraphs | ||
if(options.StripHTML && String(text).match(/\r?\n\r?\n/)){ | ||
text = String(text).replace(/((.+)(\r?\n\r?\n|$))/gi, "<p>$2</p>"); | ||
} | ||
@@ -152,4 +154,2 @@ | ||
truncatedText = truncatedText + options.Suffix; | ||
if(!options.StripHTML && tagStack.length > 0){ | ||
@@ -163,4 +163,10 @@ while(tagStack.length > 0){ | ||
} | ||
return truncatedText; | ||
if(truncatedText.match(/<\/p>$/gi)){ | ||
truncatedText = truncatedText.replace(/(<\/p>)$/gi, options.Suffix + "$1"); | ||
}else{ | ||
truncatedText = truncatedText + options.Suffix; | ||
} | ||
return truncatedText.trim(); | ||
}; | ||
@@ -167,0 +173,0 @@ |
{ | ||
"name": "truncatise", | ||
"version": "0.0.1", | ||
"version": "0.0.2", | ||
"description": "Truncate HTML based on characters, words or paragraphs. Has the ability to strip tags.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
27
test.js
var truncatise = require("./"), | ||
chai = require("chai"), | ||
assert = require("chai").assert, | ||
mocha = require("mocha"); | ||
@@ -9,3 +10,3 @@ chai.should(); | ||
truncatise("<p>This is a test of <b>html</b> <strong>tag</strong> <span class='cssClass'>stripping</span></p>", {TruncateLength: 10, TruncateBy : "characters", Strict : true, StripHTML : true, Suffix : ''}) | ||
.should.equal("This is a "); | ||
.should.equal("This is a"); | ||
}); | ||
@@ -15,3 +16,3 @@ | ||
truncatise("<p>This<img src=\"test.jpg\" /> is a test of self-closing tags such as <img src=\"test.jpg\" /></p>", {TruncateLength: 10, TruncateBy : "characters", Strict : true, StripHTML : true, Suffix : ''}) | ||
.should.equal("This is a "); | ||
.should.equal("This is a"); | ||
}); | ||
@@ -80,3 +81,3 @@ | ||
truncatise("<p>This is a test of <b>html</b> <strong>tag</strong> <span class='cssClass'>stripping</span></p><p>With multiple paragraphs</p>", {TruncateLength: 1, TruncateBy : "paragraph", StripHTML : true, Suffix : ''}) | ||
.should.equal("This is a test of html tag stripping "); | ||
.should.equal("This is a test of html tag stripping"); | ||
}); | ||
@@ -91,9 +92,19 @@ | ||
truncatise("<p>This</p><p>is</p><p>a</p><p>test</p><p>of</p><p>multiple</p><p>paragraphs</p>", {TruncateLength: 3, TruncateBy : "paragraph", StripHTML : true, Suffix : ''}) | ||
.should.equal("This is a "); | ||
.should.equal("This is a"); | ||
}); | ||
it("should be able to handle double newline as paragraphs", function(){ | ||
truncatise("This is the first paragraph.\r\n\r\nThis is the second.\n\nThis is the third.", {TruncateLength: 2, TruncateBy : "paragraph", StripHTML : true, Suffix : ''}) | ||
.should.equal("This is the first paragraph. This is the second. "); | ||
it("should append the suffix inside the paragraph", function(){ | ||
truncatise("<p>This</p><p>is</p><p>a</p><p>test</p><p>of</p><p>multiple</p><p>paragraphs</p>", {TruncateLength: 3, TruncateBy : "paragraph", StripHTML : false, Suffix : '...'}) | ||
.should.equal("<p>This</p><p>is</p><p>a...</p>"); | ||
}); | ||
it("should be able to handle double newline", function(){ | ||
truncatise("This\n\nIs\r\n\r\nA\n\nTest", {TruncateLength: 3, TruncateBy : "paragraph", StripHTML : true, Suffix : ''}) | ||
.should.equal("This Is A"); | ||
}); | ||
it("should be able to handle double newline", function(){ | ||
truncatise("<p>This</p>\n\n<p>Is</p>\n\n<p>A</p>\n\n<p>Test</p>", {TruncateLength: 3, TruncateBy : "paragraph", StripHTML : true, Suffix : ''}) | ||
.should.equal("This Is A"); | ||
}); | ||
}); | ||
@@ -159,2 +170,2 @@ | ||
}); | ||
}); | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
22421
299