remove-markdown 0.3.0 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
File without changes
package/LICENSE CHANGED
File without changes
package/README.md CHANGED
File without changes
package/index.js CHANGED
@@ -4,11 +4,14 @@ module.exports = function(md, options) {
4
4
  options.stripListLeaders = options.hasOwnProperty('stripListLeaders') ? options.stripListLeaders : true;
5
5
  options.gfm = options.hasOwnProperty('gfm') ? options.gfm : true;
6
6
  options.useImgAltText = options.hasOwnProperty('useImgAltText') ? options.useImgAltText : true;
7
+ options.abbr = options.hasOwnProperty('abbr') ? options.abbr : false;
8
+ options.replaceLinksWithURL = options.hasOwnProperty('replaceLinksWithURL') ? options.replaceLinksWithURL : false;
9
+ options.htmlTagsToSkip = options.hasOwnProperty('htmlTagsToSkip') ? options.htmlTagsToSkip : [];
7
10
 
8
11
  var output = md || '';
9
12
 
10
13
  // Remove horizontal rules (stripListHeaders conflict with this rule, which is why it has been moved to the top)
11
- output = output.replace(/^(-\s*?|\*\s*?|_\s*?){3,}\s*$/gm, '');
14
+ output = output.replace(/^(-\s*?|\*\s*?|_\s*?){3,}\s*/gm, '');
12
15
 
13
16
  try {
14
17
  if (options.stripListLeaders) {
@@ -19,7 +22,7 @@ module.exports = function(md, options) {
19
22
  }
20
23
  if (options.gfm) {
21
24
  output = output
22
- // Header
25
+ // Header
23
26
  .replace(/\n={2,}/g, '\n')
24
27
  // Fenced codeblocks
25
28
  .replace(/~{3}.*\n/g, '')
@@ -28,9 +31,31 @@ module.exports = function(md, options) {
28
31
  // Fenced codeblocks
29
32
  .replace(/`{3}.*\n/g, '');
30
33
  }
34
+ if (options.abbr) {
35
+ // Remove abbreviations
36
+ output = output.replace(/\*\[.*\]:.*\n/, '');
37
+ }
31
38
  output = output
32
- // Remove HTML tags
39
+ // Remove HTML tags
33
40
  .replace(/<[^>]*>/g, '')
41
+
42
+ var htmlReplaceRegex = new RegExp('<[^>]*>', 'g');
43
+ if (options.htmlTagsToSkip.length > 0) {
44
+ // Using negative lookahead. Eg. (?!sup|sub) will not match 'sup' and 'sub' tags.
45
+ var joinedHtmlTagsToSkip = '(?!' + options.htmlTagsToSkip.join("|") + ')';
46
+
47
+ // Adding the lookahead literal with the default regex for html. Eg./<(?!sup|sub)[^>]*>/ig
48
+ htmlReplaceRegex = new RegExp(
49
+ '<' +
50
+ joinedHtmlTagsToSkip +
51
+ '[^>]*>',
52
+ 'ig'
53
+ );
54
+ }
55
+
56
+ output = output
57
+ // Remove HTML tags
58
+ .replace(htmlReplaceRegex, '')
34
59
  // Remove setext-style headers
35
60
  .replace(/^[=\-]{2,}\s*$/g, '')
36
61
  // Remove footnotes?
@@ -39,22 +64,30 @@ module.exports = function(md, options) {
39
64
  // Remove images
40
65
  .replace(/\!\[(.*?)\][\[\(].*?[\]\)]/g, options.useImgAltText ? '$1' : '')
41
66
  // Remove inline links
42
- .replace(/\[(.*?)\][\[\(].*?[\]\)]/g, '$1')
67
+ .replace(/\[([^\]]*?)\][\[\(].*?[\]\)]/g, options.replaceLinksWithURL ? '$2' : '$1')
43
68
  // Remove blockquotes
44
- .replace(/^\s{0,3}>\s?/g, '')
69
+ .replace(/^\s{0,3}>\s?/gm, '')
70
+ // .replace(/(^|\n)\s{0,3}>\s?/g, '\n\n')
45
71
  // Remove reference-style links?
46
72
  .replace(/^\s{1,2}\[(.*?)\]: (\S+)( ".*?")?\s*$/g, '')
47
73
  // Remove atx-style headers
48
- .replace(/^(\n)?\s{0,}#{1,6}\s+| {0,}(\n)?\s{0,}#{0,} {0,}(\n)?\s{0,}$/gm, '$1$2$3')
49
- // Remove emphasis (repeat the line to remove double emphasis)
50
- .replace(/([\*_]{1,3})(\S.*?\S{0,1})\1/g, '$2')
51
- .replace(/([\*_]{1,3})(\S.*?\S{0,1})\1/g, '$2')
74
+ .replace(/^(\n)?\s{0,}#{1,6}\s+| {0,}(\n)?\s{0,}#{0,} #{0,}(\n)?\s{0,}$/gm, '$1$2$3')
75
+ // Remove * emphasis
76
+ .replace(/([\*]+)(\S)(.*?\S)??\1/g, '$2$3')
77
+ // Remove _ emphasis. Unlike *, _ emphasis gets rendered only if
78
+ // 1. Either there is a whitespace character before opening _ and after closing _.
79
+ // 2. Or _ is at the start/end of the string.
80
+ .replace(/(^|\W)([_]+)(\S)(.*?\S)??\2($|\W)/g, '$1$3$4$5')
52
81
  // Remove code blocks
53
82
  .replace(/(`{3,})(.*?)\1/gm, '$2')
54
83
  // Remove inline code
55
84
  .replace(/`(.+?)`/g, '$1')
56
- // Replace two or more newlines with exactly two? Not entirely sure this belongs here...
57
- .replace(/\n{2,}/g, '\n\n');
85
+ // // Replace two or more newlines with exactly two? Not entirely sure this belongs here...
86
+ // .replace(/\n{2,}/g, '\n\n')
87
+ // // Remove newlines in a paragraph
88
+ // .replace(/(\S+)\n\s*(\S+)/g, '$1 $2')
89
+ // Replace strike through
90
+ .replace(/~(.*?)~/g, '$1');
58
91
  } catch(e) {
59
92
  console.error(e);
60
93
  return md;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "remove-markdown",
3
- "version": "0.3.0",
3
+ "version": "0.5.0",
4
4
  "description": "Remove Markdown formatting from text",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -69,6 +69,18 @@ describe('remove Markdown', function () {
69
69
  expect(removeMd(string)).to.equal(expected);
70
70
  });
71
71
 
72
+ it('should remove emphasis only if there is no space between word and emphasis characters.', function () {
73
+ const string = 'There should be no _space_, *before* *closing * _ephasis character _.';
74
+ const expected = 'There should be no space, before *closing * _ephasis character _.';
75
+ expect(removeMd(string)).to.equal(expected);
76
+ });
77
+
78
+ it('should remove "_" emphasis only if there is space before opening and after closing emphasis characters.', function () {
79
+ const string = '._Spaces_ _ before_ and _after _ emphasised character results in no emphasis.';
80
+ const expected = '.Spaces _ before_ and _after _ emphasised character results in no emphasis.';
81
+ expect(removeMd(string)).to.equal(expected);
82
+ });
83
+
72
84
  it('should remove double emphasis', function () {
73
85
  const string = '**this sentence has __double styling__**';
74
86
  const expected = 'this sentence has double styling';
@@ -109,18 +121,31 @@ describe('remove Markdown', function () {
109
121
  expect(removeMd(test.string)).to.equal(test.expected);
110
122
  });
111
123
  });
124
+
125
+ it('should remove blockquotes over multiple lines', function () {
126
+ const string = '> I am a blockquote firstline \n>I am a blockquote secondline';
127
+ const expected = 'I am a blockquote firstline\nI am a blockquote secondline';
128
+ expect(removeMd(string)).to.equal(expected);
129
+ });
130
+
131
+ // it('should remove blockquotes following other content', function () {
132
+ // const string = '## A headline\n\nA paragraph of text\n\n> I am a blockquote';
133
+ // const expected = 'A headline\n\nA paragraph of text\n\nI am a blockquote';
134
+
135
+ // expect(removeMd(string)).to.equal(expected);
136
+ // });
112
137
 
113
138
  it('should not remove greater than signs', function () {
114
139
  var tests = [
115
- { string: '100 > 0', expected: '100 > 0' },
116
- { string: '100 >= 0', expected: '100 >= 0' },
117
- { string: '100>0', expected: '100>0' },
118
- { string: '> 100 > 0', expected: '100 > 0' },
119
- { string: '1 < 100', expected: '1 < 100' },
120
- { string: '1 <= 100', expected: '1 <= 100' },
140
+ { string: '100 > 0', expected: '100 > 0' },
141
+ { string: '100 >= 0', expected: '100 >= 0' },
142
+ { string: '100>0', expected: '100>0' },
143
+ { string: '> 100 > 0', expected: '100 > 0' },
144
+ { string: '1 < 100', expected: '1 < 100' },
145
+ { string: '1 <= 100', expected: '1 <= 100' },
121
146
  ];
122
147
  tests.forEach(function (test) {
123
- expect(removeMd(test.string)).to.equal(test.expected);
148
+ expect(removeMd(test.string)).to.equal(test.expected);
124
149
  });
125
150
  });
126
151
 
@@ -141,5 +166,22 @@ describe('remove Markdown', function () {
141
166
  const expected = '\nThis is a heading\n\nThis is a paragraph with a link.\n\nThis is another heading\n\nIn Getting Started we set up something foo.\n\n Some list\n With items\n Even indented';
142
167
  expect(removeMd(paragraph)).to.equal(expected);
143
168
  });
169
+
170
+ it('should not strip paragraphs without content', function() {
171
+ const paragraph = '\n#This paragraph\n##This paragraph#';
172
+ const expected = paragraph;
173
+ expect(removeMd(paragraph)).to.equal(expected);
174
+ });
175
+
176
+ it('should not trigger ReDoS with atx-headers', function () {
177
+ const start = Date.now();
178
+
179
+ const paragraph = '\n## This is a long "'+' '.repeat(200)+'" heading ##\n';
180
+ const expected = /\nThis is a long " {200}" heading\n/;
181
+ expect(removeMd(paragraph)).to.match(expected);
182
+
183
+ const duration = Date.now()-start;
184
+ expect(duration).to.be.lt(500);
185
+ });
144
186
  });
145
187
  });
package/.npmignore DELETED
@@ -1 +0,0 @@
1
- node_modules/