From fdba2b58a4d03428c96ba3dfb5046963c36f393c Mon Sep 17 00:00:00 2001 From: techxplorer Date: Sat, 2 Nov 2024 12:35:50 +1030 Subject: [PATCH] Fix double blank lines in content --- src/lib/content-creator.js | 29 ++++++++++++++++- tests/lib/content-creator.test.js | 53 +++++++++++++++++++++++++++++++ 2 files changed, 81 insertions(+), 1 deletion(-) diff --git a/src/lib/content-creator.js b/src/lib/content-creator.js index ba01e5e..f3700ec 100644 --- a/src/lib/content-creator.js +++ b/src/lib/content-creator.js @@ -142,7 +142,10 @@ class ContentCreator { return this.contentCache.get( status.id ); } - const content = this.convertContent( status.content ); + const content = this.fixDoubleLineSpaces( + this.convertContent( status.content ) + ); + this.contentCache.set( status.id, content ); return content; } @@ -344,6 +347,30 @@ class ContentCreator { } + /** + * Use the first sentences to make a post description. + * See this {@linkhttps://stackoverflow.com/a/49432524|Stack Overflow} post for + * original implementation. + * @param {string} markdownContent The content in Markdown format. + * @throws {TypeError} If the parameter is incorrect. + * @returns {string} The description of the content using the first sentences. + */ + fixDoubleLineSpaces( markdownContent ) { + + if ( markdownContent === undefined ) { + throw new TypeError( "The markdownContent parameter is required" ); + } + + if ( typeof markdownContent !== "string" ) { + throw new TypeError( "The markdownContent parameter must be a string" ); + } + + // eslint-disable-next-line no-control-regex + var regExp = new RegExp( "(\n){3,}", "gm" ); + return markdownContent.replace( regExp, "\n\n" ); + + } + /** * Make it easy to throw a consistent error message. * @param {string} propertyName The name of the property that wasn't found. diff --git a/tests/lib/content-creator.test.js b/tests/lib/content-creator.test.js index 76a7610..c80b75a 100644 --- a/tests/lib/content-creator.test.js +++ b/tests/lib/content-creator.test.js @@ -59,6 +59,15 @@ const expectedTitle = "Hello Blowerians! There's been an uptick in reported post const expectedDescr = "Hello Blowerians! There's been an uptick in reported posts lately, for probably obvious reasons…"; /* eslint-enable max-len, no-useless-escape */ +const testDoubleLineSpace = `Lorem ipsum odor amet, consectetuer adipiscing elit. + + +Ornare dis malesuada vestibulum maximus aliquet etiam.`; + +const expectedDoubleLineSpace = `Lorem ipsum odor amet, consectetuer adipiscing elit. + +Ornare dis malesuada vestibulum maximus aliquet etiam.`; + describe( "ContentCreator", () => { describe( "constructor", () => { @@ -517,4 +526,48 @@ describe( "ContentCreator", () => { } ); } ); + + describe( "fixDoubleLineSpaces", () => { + + it( "should throw an error when the parameter is incorrect", () => { + const contentCreator = new ContentCreator(); + + assert.throws( + () => { + contentCreator.fixDoubleLineSpaces(); + }, + { + name: "TypeError", + message: /is required/ + } + ); + + assert.throws( + () => { + contentCreator.fixDoubleLineSpaces( 1234 ); + }, + { + name: "TypeError", + message: /must be a string/ + } + ); + + assert.doesNotThrow( + () => { + contentCreator.fixDoubleLineSpaces( testLongContent ); + } + ); + + } ); + + it( "should make the expected description", () => { + const contentCreator = new ContentCreator(); + const testContent = contentCreator.fixDoubleLineSpaces( testDoubleLineSpace ); + assert.equal( + testContent, + expectedDoubleLineSpace + ); + } ); + + } ); } );