remove-markdown 0.5.5 → 0.6.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.
package/README.md CHANGED
@@ -1,3 +1,5 @@
1
+ - ![default workflow](https://github.com/stiang/remove-markdown/actions/workflows/default.yaml/badge.svg)
2
+
1
3
  ## What is it?
2
4
  **remove-markdown** is a node.js module that will remove (strip) Markdown formatting from text.
3
5
  *Markdown formatting* means pretty much anything that doesn’t look like regular text, like square brackets, asterisks etc.
package/index.js CHANGED
@@ -12,7 +12,7 @@ module.exports = function(md, options) {
12
12
  var output = md || '';
13
13
 
14
14
  // Remove horizontal rules (stripListHeaders conflict with this rule, which is why it has been moved to the top)
15
- output = output.replace(/^(-\s*?|\*\s*?|_\s*?){3,}\s*/gm, '');
15
+ output = output.replace(/^ {0,3}((?:-[\t ]*){3,}|(?:_[ \t]*){3,}|(?:\*[ \t]*){3,})(?:\n+|$)/gm, '');
16
16
 
17
17
  try {
18
18
  if (options.stripListLeaders) {
@@ -36,22 +36,15 @@ module.exports = function(md, options) {
36
36
  // Remove abbreviations
37
37
  output = output.replace(/\*\[.*\]:.*\n/, '');
38
38
  }
39
- output = output
40
- // Remove HTML tags
41
- .replace(/<[^>]*>/g, '')
42
-
43
- var htmlReplaceRegex = new RegExp('<[^>]*>', 'g');
44
- if (options.htmlTagsToSkip.length > 0) {
45
- // Using negative lookahead. Eg. (?!sup|sub) will not match 'sup' and 'sub' tags.
46
- var joinedHtmlTagsToSkip = '(?!' + options.htmlTagsToSkip.join("|") + ')';
47
-
48
- // Adding the lookahead literal with the default regex for html. Eg./<(?!sup|sub)[^>]*>/ig
39
+
40
+ let htmlReplaceRegex = /<[^>]*>/g
41
+ if (options.htmlTagsToSkip && options.htmlTagsToSkip.length > 0) {
42
+ // Create a regex that matches tags not in htmlTagsToSkip
43
+ const joinedHtmlTagsToSkip = options.htmlTagsToSkip.join('|')
49
44
  htmlReplaceRegex = new RegExp(
50
- '<' +
51
- joinedHtmlTagsToSkip +
52
- '[^>]*>',
53
- 'ig'
54
- );
45
+ `<(?!\/?(${joinedHtmlTagsToSkip})(?=>|\s[^>]*>))[^>]*>`,
46
+ 'g',
47
+ )
55
48
  }
56
49
 
57
50
  output = output
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "remove-markdown",
3
- "version": "0.5.5",
3
+ "version": "0.6.0",
4
4
  "description": "Remove Markdown formatting from text",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -87,6 +87,12 @@ describe('remove Markdown', function () {
87
87
  expect(removeMd(string)).to.equal(expected);
88
88
  });
89
89
 
90
+ it('should not mistake a horizontal rule when symbols are mixed ', function () {
91
+ const string = 'Some text on a line\n\n--*\n\nA line below';
92
+ const expected = 'Some text on a line\n\n--*\n\nA line below';
93
+ expect(removeMd(string)).to.equal(expected);
94
+ });
95
+
90
96
  it('should remove horizontal rules', function () {
91
97
  const string = 'Some text on a line\n\n---\n\nA line below';
92
98
  const expected = 'Some text on a line\n\nA line below';
@@ -161,6 +167,12 @@ describe('remove Markdown', function () {
161
167
  expect(removeMd(string)).to.equal(expected);
162
168
  });
163
169
 
170
+ it('should strip list items with bold word in the beginning', function () {
171
+ const string = 'Some text on a line\n\n- **A** list Item\n- **Another** list item';
172
+ const expected = 'Some text on a line\n\nA list Item\nAnother list item';
173
+ expect(removeMd(string)).to.equal(expected);
174
+ });
175
+
164
176
  it('should handle paragraphs with markdown', function () {
165
177
  const paragraph = '\n## This is a heading ##\n\nThis is a paragraph with [a link](http://www.disney.com/).\n\n### This is another heading\n\nIn `Getting Started` we set up `something` foo.\n\n * Some list\n * With items\n * Even indented';
166
178
  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';
@@ -195,5 +207,16 @@ describe('remove Markdown', function () {
195
207
  const expected = 'Heading in italic';
196
208
  expect(removeMd(string)).to.equal(expected);
197
209
  });
210
+
211
+ it('should skip specified HTML tags when htmlTagsToSkip option is provided', () => {
212
+ const markdown =
213
+ '<div>HTML content <sub>Superscript</sub> <span>span text</span></div>'
214
+ const result = removeMd(markdown, {htmlTagsToSkip: ['sub']})
215
+ expect(result).to.equal('HTML content <sub>Superscript</sub> span text')
216
+ const result2 = removeMd(markdown, {htmlTagsToSkip: ['sub', 'span']})
217
+ expect(result2).to.equal(
218
+ 'HTML content <sub>Superscript</sub> <span>span text</span>',
219
+ )
220
+ })
198
221
  });
199
222
  });