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

Fix html strip #67

Closed
wants to merge 3 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
119 changes: 119 additions & 0 deletions html_tags.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
[
"a",
"abbr",
"address",
"area",
"article",
"aside",
"audio",
"b",
"base",
"bdi",
"bdo",
"blockquote",
"body",
"br",
"button",
"canvas",
"caption",
"cite",
"code",
"col",
"colgroup",
"data",
"datalist",
"dd",
"del",
"details",
"dfn",
"dialog",
"div",
"dl",
"dt",
"em",
"embed",
"fieldset",
"figcaption",
"figure",
"footer",
"form",
"h1",
"h2",
"h3",
"h4",
"h5",
"h6",
"head",
"header",
"hgroup",
"hr",
"html",
"i",
"iframe",
"img",
"input",
"ins",
"kbd",
"label",
"legend",
"li",
"link",
"main",
"map",
"mark",
"math",
"menu",
"menuitem",
"meta",
"meter",
"nav",
"noscript",
"object",
"ol",
"optgroup",
"option",
"output",
"p",
"param",
"picture",
"pre",
"progress",
"q",
"rb",
"rp",
"rt",
"rtc",
"ruby",
"s",
"samp",
"script",
"section",
"select",
"slot",
"small",
"source",
"span",
"strong",
"style",
"sub",
"summary",
"sup",
"svg",
"table",
"tbody",
"td",
"template",
"textarea",
"tfoot",
"th",
"thead",
"time",
"title",
"tr",
"track",
"u",
"ul",
"var",
"video",
"wbr"
]
35 changes: 30 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,6 @@ module.exports = function(md, options) {
// Remove abbreviations
output = output.replace(/\*\[.*\]:.*\n/, '');
}
output = output
// Remove HTML tags
.replace(/<[^>]*>/g, '')

var htmlReplaceRegex = new RegExp('<[^>]*>', 'g');
if (options.htmlTagsToSkip.length > 0) {
Expand All @@ -53,9 +50,16 @@ module.exports = function(md, options) {
);
}

const htmlPossibleTags = output.match(htmlReplaceRegex);
if (htmlPossibleTags) {
htmlPossibleTags.forEach(function(string) {
if (isHtmlTag(string)) {
output = removeHtmltagIfNotInsideCodeblock(md, output, string);
}
});
}

output = output
// Remove HTML tags
.replace(htmlReplaceRegex, '')
// Remove setext-style headers
.replace(/^[=\-]{2,}\s*$/g, '')
// Remove footnotes?
Expand Down Expand Up @@ -94,3 +98,24 @@ module.exports = function(md, options) {
}
return output;
};

function isHtmlTag(string) {
const splittedString = string.replace('/', '').split(' ');
const htmlTags = require('./html_tags.json');

string = string.replace('<', '').replace('>', '').replace('/', '');

return !(Boolean( string.match(/\d/) ) && (!isNaN(splittedString[1]) || !isNaN(splittedString.reverse()[1]))) && htmlTags.includes(string[0]);
}

function removeHtmltagIfNotInsideCodeblock(md, output, tag, codeMark = '`') {
const re = new RegExp(tag,'gi');

while (exec = re.exec(output)){
if (md[exec.index - 1] !== codeMark || md[re.lastIndex] !== codeMark) {
output = output.substring(0, exec.index) + output.substring(re.lastIndex);
}
}

return output;
}
18 changes: 18 additions & 0 deletions test/remove-markdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,18 @@ describe('remove Markdown', function () {
expect(removeMd(string)).to.equal(expected);
});

it('should not strip arithmetic signs', function() {
const string = '`<p class="class1 class2" id="id1">The equations 2 < 3 and 3 > 1 are both true</p>`';
const expected = 'The equations 2 < 3 and 3 > 1 are both true';
expect(removeMd(string)).to.equal(expected);
});

it('should not strip html inside code element', function() {
const string = 'HTML tags look like this: `<div>`.';
const expected = 'HTML tags look like this: <div>.';
expect(removeMd(string)).to.equal(expected);
});

it('should strip anchors', function () {
const string = '*Javascript* [developers](https://engineering.condenast.io/)* are the _best_.';
const expected = 'Javascript developers* are the best.';
Expand Down Expand Up @@ -183,5 +195,11 @@ describe('remove Markdown', function () {
const duration = Date.now()-start;
expect(duration).to.be.lt(1000);
});

it('should not strip not HTML corner braces', function () {
const string = 'You can write to John Doe <john.doe AT example DOT com>';
const expected = 'You can write to John Doe <john.doe AT example DOT com>';
expect(removeMd(string)).to.equal(expected);
});
});
});