Skip to content

Commit

Permalink
Fix double blank lines in content
Browse files Browse the repository at this point in the history
  • Loading branch information
techxplorer committed Nov 2, 2024
1 parent eea10f9 commit fdba2b5
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 1 deletion.
29 changes: 28 additions & 1 deletion src/lib/content-creator.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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.
Expand Down
53 changes: 53 additions & 0 deletions tests/lib/content-creator.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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", () => {
Expand Down Expand Up @@ -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
);
} );

} );
} );

0 comments on commit fdba2b5

Please sign in to comment.