Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove empty whitespace-only blocks from posts #1524

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/backend/utils/html/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ const syntaxHighlight = require('./syntax-highlight');
const replaceCodeEntities = require('./replace-entities');
const fixEmptyPre = require('./modify-pre');
const toDOM = require('./dom');
const removeEmptyParagraphs = require('./remove-empty-paragraphs');
const replaceBreaklines = require('./replace-redundant-breaklines');

const { JSDOM } = jsdom;

Expand Down Expand Up @@ -42,6 +44,10 @@ module.exports = function process(html) {
lazyLoad(dom);
// Replace <code> elements with encoded entities to use characters
replaceCodeEntities(dom);
// Clean repeated and nested <br> elements
replaceBreaklines(dom);
// Remove empty <p> elements
removeEmptyParagraphs(dom);

// Return the resulting HTML
return dom.window.document.body.innerHTML;
Expand Down
12 changes: 12 additions & 0 deletions src/backend/utils/html/remove-empty-paragraphs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module.exports = function (dom) {
if (!(dom && dom.window && dom.window.document)) {
return;
}

dom.window.document.querySelectorAll('p').forEach((p) => {
const paragraphInnerHTML = p.innerHTML;
if (!paragraphInnerHTML.replace(/&nbsp;/gm, '').trim()) {
p.remove();
}
});
};
20 changes: 20 additions & 0 deletions src/backend/utils/html/replace-redundant-breaklines.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
module.exports = function (html) {
// Cleans repeated <br> elements
html.window.document.querySelectorAll('br + br').forEach((elem) => {
elem.remove();
});

// Removes single nested <br> elements
html.window.document.querySelectorAll('br').forEach((elem) => {
if (!elem.parentNode.textContent.trim() && elem.parentNode.children.length === 1) {
elem.parentNode.parentNode.removeChild(elem.parentNode);
}
});

// Clean up of empty <div> elements that should only occur once
html.window.document.querySelectorAll('div').forEach((elem) => {
if (!elem.children.length && !elem.textContent.trim()) {
elem.remove();
}
});
};
28 changes: 28 additions & 0 deletions test/remove-empty-paragraphs.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
const toDom = require('../src/backend/utils/html/dom');
const removeEmptyParagraphs = require('../src/backend/utils/html/remove-empty-paragraphs');

describe('Remove no content anchor tags', () => {
test('should remove <p></p>', () => {
const htmlData = toDom('<div><p></p></div>');
removeEmptyParagraphs(htmlData);

const expectedHtml = '<div></div>';
expect(htmlData.window.document.body.innerHTML).toEqual(expectedHtml);
});

test('should remove <p> </p> (spaces)', () => {
const htmlData = toDom('<div><p> </p></div>');
removeEmptyParagraphs(htmlData);

const expectedHtml = '<div></div>';
expect(htmlData.window.document.body.innerHTML).toEqual(expectedHtml);
});

test('should remove <p> </p> (tabs)', () => {
const htmlData = toDom('<div><p> </p></div>');
removeEmptyParagraphs(htmlData);

const expectedHtml = '<div></div>';
expect(htmlData.window.document.body.innerHTML).toEqual(expectedHtml);
});
});
54 changes: 54 additions & 0 deletions test/replace-redundant-breaklines.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
const toDom = require('../src/backend/utils/html/dom');
const replaceBreaklines = require('../src/backend/utils/html/replace-redundant-breaklines');

describe('Replace redundant breaklines', () => {
test('Replace multiple <br> elements with one', () => {
const htmlData = `<div>
<br>
<br>
<p>text</p>
<br>
<br>
</div>`;

const html = toDom(htmlData);

replaceBreaklines(html);

const expectedHtml = `<head></head><body><div>
<br>

<p>text</p>
<br>

</div></body>`;

expect(html.window.document.documentElement.innerHTML).toEqual(expectedHtml);
});

test('Remove nested single <br> elements', () => {
const htmlData = `<div>
<div>
<br>
<br>
</div>
<div>
<div>
<br>
</div>
</div>
<p>text</p>
</div>`;

const html = toDom(htmlData);

replaceBreaklines(html);

const expectedHtml = `<head></head><body><div>


<p>text</p>
</div></body>`;
expect(html.window.document.documentElement.innerHTML).toEqual(expectedHtml);
});
});