remove-markdown 0.0.6 → 0.2.2

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,5 +1,7 @@
1
+ [![CircleCI](https://circleci.com/gh/stiang/remove-markdown.svg?style=svg&circle-token=cac2feef7dc90e6b8578aec361be369412be1c6a)](https://circleci.com/gh/stiang/remove-markdown)
2
+
1
3
  ## What is it?
2
- **remove-markdown** is a node.js/io.js module that will remove (strip) Markdown formatting from a text. "Markdown formatting" means pretty much anything that doesn’t look like regular text, like square brackets, asterisks etc.
4
+ **remove-markdown** is a node.js module that will remove (strip) Markdown formatting from a text. "Markdown formatting" means pretty much anything that doesn’t look like regular text, like square brackets, asterisks etc.
3
5
 
4
6
  ## When do I need it?
5
7
  The typical use case is to display an excerpt of a Markdown text, without the actual Markdown (or rendered HTML, for that matter), for example in a list of posts.
@@ -12,18 +14,28 @@ npm install remove-markdown
12
14
 
13
15
  ## Usage
14
16
  ```js
15
- var removeMd = require('remove-markdown');
16
- var markdown = '# This is a heading\n\nThis is a paragraph with [a link](http://www.disney.com/) in it.';
17
- var plainText = removeMd(markdown); // plainText is now 'This is a heading\n\nThis is a paragraph with a link in it.'
17
+ const removeMd = require('remove-markdown');
18
+ const markdown = '# This is a heading\n\nThis is a paragraph with [a link](http://www.disney.com/) in it.';
19
+ const plainText = removeMd(markdown); // plainText is now 'This is a heading\n\nThis is a paragraph with a link in it.'
18
20
  ```
19
21
 
20
- You can also supply an options object to the function. Currently, the only option you can set is whether to strip list leaders:
22
+ You can also supply an options object to the function. Currently, the following options are supported:
21
23
 
22
24
  ```js
23
- var plainText = removeMd(markdown, {stripListLeaders: false}); // The default is true
25
+ var plainText = removeMd(markdown, {
26
+ stripListLeaders: true , // strip list leaders (default: true)
27
+ listUnicodeChar: '', // char to insert instead of stripped list leaders (default: '')
28
+ gfm: true // support GitHub-Flavored Markdown (default: true)
29
+ });
24
30
  ```
25
31
 
26
- This will retain any list characters (`*, -, +, (digit).`).
32
+ Setting `stripListLeaders` to false will retain any list characters (`*, -, +, (digit).`).
33
+
34
+ ## TODO
35
+ PRs are very much welcome.
36
+ * Allow the RegEx expressions to be customized per rule
37
+ * Make the rules more robust, support more edge cases
38
+ * Add more (comprehensive) tests
27
39
 
28
40
  ## Credits
29
41
  The code is based on [Markdown Service Tools - Strip Markdown](http://brettterpstra.com/2013/10/18/a-markdown-service-to-strip-markdown/) by Brett Terpstra.
package/index.js CHANGED
@@ -1,15 +1,33 @@
1
1
  module.exports = function(md, options) {
2
2
  options = options || {};
3
+ options.listUnicodeChar = options.hasOwnProperty('listUnicodeChar') ? options.listUnicodeChar : false;
3
4
  options.stripListLeaders = options.hasOwnProperty('stripListLeaders') ? options.stripListLeaders : true;
5
+ options.gfm = options.hasOwnProperty('gfm') ? options.gfm : true;
6
+
7
+ var output = md || '';
8
+
9
+ // Remove horizontal rules (stripListHeaders conflict with this rule, which is why it has been moved to the top)
10
+ output = output.replace(/^(-\s*?|\*\s*?|_\s*?){3,}\s*$/gm, '');
4
11
 
5
- var output = md;
6
12
  try {
7
13
  if (options.stripListLeaders) {
8
- output = output.replace(/^([\s\t]*)([\*\-\+]|\d\.)\s+/gm, '$1');
14
+ if (options.listUnicodeChar)
15
+ output = output.replace(/^([\s\t]*)([\*\-\+]|\d+\.)\s+/gm, options.listUnicodeChar + ' $1');
16
+ else
17
+ output = output.replace(/^([\s\t]*)([\*\-\+]|\d+\.)\s+/gm, '$1');
18
+ }
19
+ if (options.gfm) {
20
+ output = output
21
+ // Header
22
+ .replace(/\n={2,}/g, '\n')
23
+ // Strikethrough
24
+ .replace(/~~/g, '')
25
+ // Fenced codeblocks
26
+ .replace(/`{3}.*\n/g, '');
9
27
  }
10
28
  output = output
11
29
  // Remove HTML tags
12
- .replace(/<(.*?)>/g, '$1')
30
+ .replace(/<[^>]*>/g, '')
13
31
  // Remove setext-style headers
14
32
  .replace(/^[=\-]{2,}\s*$/g, '')
15
33
  // Remove footnotes?
@@ -19,18 +37,24 @@ module.exports = function(md, options) {
19
37
  .replace(/\!\[.*?\][\[\(].*?[\]\)]/g, '')
20
38
  // Remove inline links
21
39
  .replace(/\[(.*?)\][\[\(].*?[\]\)]/g, '$1')
40
+ // Remove blockquotes
41
+ .replace(/^\s{0,3}>\s?/g, '')
22
42
  // Remove reference-style links?
23
43
  .replace(/^\s{1,2}\[(.*?)\]: (\S+)( ".*?")?\s*$/g, '')
24
44
  // Remove atx-style headers
25
- .replace(/^\#{1,6}\s*([^#]*)\s*(\#{1,6})?/gm, '$1')
26
- .replace(/([\*_]{1,2})(\S.*?\S)\1/g, '$2')
45
+ .replace(/^(\n)?\s{0,}#{1,6}\s+| {0,}(\n)?\s{0,}#{0,} {0,}(\n)?\s{0,}$/gm, '$1$2$3')
46
+ // Remove emphasis (repeat the line to remove double emphasis)
47
+ .replace(/([\*_]{1,3})(\S.*?\S{0,1})\1/g, '$2')
48
+ .replace(/([\*_]{1,3})(\S.*?\S{0,1})\1/g, '$2')
49
+ // Remove code blocks
27
50
  .replace(/(`{3,})(.*?)\1/gm, '$2')
28
- .replace(/^-{3,}\s*$/g, '')
51
+ // Remove inline code
29
52
  .replace(/`(.+?)`/g, '$1')
53
+ // Replace two or more newlines with exactly two? Not entirely sure this belongs here...
30
54
  .replace(/\n{2,}/g, '\n\n');
31
55
  } catch(e) {
32
56
  console.error(e);
33
- return md;
57
+ return md;
34
58
  }
35
59
  return output;
36
- }
60
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "remove-markdown",
3
- "version": "0.0.6",
3
+ "version": "0.2.2",
4
4
  "description": "Remove Markdown formatting from text",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -20,6 +20,7 @@
20
20
  },
21
21
  "homepage": "https://github.com/stiang/remove-markdown",
22
22
  "devDependencies": {
23
+ "chai": "^4.0.2",
23
24
  "mocha": "^2.1.0",
24
25
  "should": "^5.0.0"
25
26
  }
@@ -1,14 +1,139 @@
1
1
  'use strict';
2
+ const expect = require('chai').expect;
3
+ const removeMd = require('../');
2
4
 
3
- var fs = require('fs'),
4
- path = require('path'),
5
- should = require('should'),
6
- removeMd = require('../'),
7
- markdown = fs.readFileSync(path.resolve(__dirname, 'markdown.md')).toString(),
8
- result = fs.readFileSync(path.resolve(__dirname, 'result.txt')).toString();
5
+ describe('remove Markdown', function () {
6
+ describe('removeMd', function () {
7
+ it('should leave a string alone without markdown', function () {
8
+ const string = 'Javascript Developers are the best.';
9
+ expect(removeMd(string)).to.equal(string);
10
+ });
9
11
 
10
- describe('remove-markdown', function () {
11
- it('should remove markdown', function () {
12
- removeMd(markdown).should.eql(result);
12
+ it('should strip out remaining markdown', function () {
13
+ const string = '*Javascript* developers are the _best_.';
14
+ const expected = 'Javascript developers are the best.';
15
+ expect(removeMd(string)).to.equal(expected);
16
+ });
17
+
18
+ it('should leave non-matching markdown markdown', function () {
19
+ const string = '*Javascript* developers* are the _best_.';
20
+ const expected = 'Javascript developers* are the best.';
21
+ expect(removeMd(string)).to.equal(expected);
22
+ });
23
+
24
+ it('should leave non-matching markdown, but strip empty anchors', function () {
25
+ const string = '*Javascript* [developers]()* are the _best_.';
26
+ const expected = 'Javascript developers* are the best.';
27
+ expect(removeMd(string)).to.equal(expected);
28
+ });
29
+
30
+ it('should strip HTML', function () {
31
+ const string = '<p>Hello World</p>';
32
+ const expected = 'Hello World';
33
+ expect(removeMd(string)).to.equal(expected);
34
+ });
35
+
36
+ it('should strip anchors', function () {
37
+ const string = '*Javascript* [developers](https://engineering.condenast.io/)* are the _best_.';
38
+ const expected = 'Javascript developers* are the best.';
39
+ expect(removeMd(string)).to.equal(expected);
40
+ });
41
+
42
+ it('should strip img tags', function () {
43
+ const string = '![bear](https://placebear.com/640/480)*Javascript* developers are the _best_.';
44
+ const expected = 'Javascript developers are the best.';
45
+ expect(removeMd(string)).to.equal(expected);
46
+ });
47
+
48
+ it('should strip code tags', function () {
49
+ const string = 'In `Getting Started` we set up `something` foo.';
50
+ const expected = 'In Getting Started we set up something foo.';
51
+ expect(removeMd(string)).to.equal(expected);
52
+ });
53
+
54
+ it('should leave hashtags in headings', function () {
55
+ const string = '## This #heading contains #hashtags';
56
+ const expected = 'This #heading contains #hashtags';
57
+ expect(removeMd(string)).to.equal(expected);
58
+ });
59
+
60
+ it('should remove emphasis', function () {
61
+ const string = 'I italicized an *I* and it _made_ me *sad*.';
62
+ const expected = 'I italicized an I and it made me sad.';
63
+ expect(removeMd(string)).to.equal(expected);
64
+ });
65
+
66
+ it('should remove double emphasis', function () {
67
+ const string = '**this sentence has __double styling__**';
68
+ const expected = 'this sentence has double styling';
69
+ expect(removeMd(string)).to.equal(expected);
70
+ });
71
+
72
+ it('should remove horizontal rules', function () {
73
+ const string = 'Some text on a line\n\n---\n\nA line below';
74
+ const expected = 'Some text on a line\n\nA line below';
75
+ expect(removeMd(string)).to.equal(expected);
76
+ });
77
+
78
+ it('should remove horizontal rules with space-separated asterisks', function () {
79
+ const string = 'Some text on a line\n\n* * *\n\nA line below';
80
+ const expected = 'Some text on a line\n\nA line below';
81
+ expect(removeMd(string)).to.equal(expected);
82
+ });
83
+
84
+ it('should remove blockquotes', function () {
85
+ const string = '>I am a blockquote';
86
+ const expected = 'I am a blockquote';
87
+ expect(removeMd(string)).to.equal(expected);
88
+ });
89
+
90
+ it('should remove blockquotes with spaces', function () {
91
+ const string = '> I am a blockquote';
92
+ const expected = 'I am a blockquote';
93
+ expect(removeMd(string)).to.equal(expected);
94
+ });
95
+
96
+ it('should remove indented blockquotes', function () {
97
+ var tests = [
98
+ { string: ' > I am a blockquote', expected: 'I am a blockquote' },
99
+ { string: ' > I am a blockquote', expected: 'I am a blockquote' },
100
+ { string: ' > I am a blockquote', expected: 'I am a blockquote' },
101
+ ];
102
+ tests.forEach(function (test) {
103
+ expect(removeMd(test.string)).to.equal(test.expected);
104
+ });
105
+ });
106
+
107
+ it('should not remove greater than signs', function () {
108
+ var tests = [
109
+ { string: '100 > 0', expected: '100 > 0' },
110
+ { string: '100 >= 0', expected: '100 >= 0' },
111
+ { string: '100>0', expected: '100>0' },
112
+ { string: '> 100 > 0', expected: '100 > 0' },
113
+ { string: '1 < 100', expected: '1 < 100' },
114
+ { string: '1 <= 100', expected: '1 <= 100' },
115
+ ];
116
+ tests.forEach(function (test) {
117
+ expect(removeMd(test.string)).to.equal(test.expected);
118
+ });
119
+ });
120
+
121
+ it('should strip unordered list leaders', function () {
122
+ const string = 'Some text on a line\n\n* A list Item\n* Another list item';
123
+ const expected = 'Some text on a line\n\nA list Item\nAnother list item';
124
+ expect(removeMd(string)).to.equal(expected);
125
+ });
126
+
127
+ it('should strip ordered list leaders', function () {
128
+ const string = 'Some text on a line\n\n9. A list Item\n10. Another list item';
129
+ const expected = 'Some text on a line\n\nA list Item\nAnother list item';
130
+ expect(removeMd(string)).to.equal(expected);
131
+ });
132
+
133
+ it('should handle paragraphs with markdown', function () {
134
+ 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';
135
+ 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';
136
+ expect(removeMd(paragraph)).to.equal(expected);
137
+ });
13
138
  });
14
- });
139
+ });
package/test/markdown.md DELETED
@@ -1,11 +0,0 @@
1
- ## This is a heading ##
2
-
3
- This is a paragraph with [a link](http://www.disney.com/).
4
-
5
- ### This is another heading
6
-
7
- In `Getting Started` we set up `something` foo.
8
-
9
- * Some list
10
- * With items
11
- * Even indented
package/test/result.txt DELETED
@@ -1,11 +0,0 @@
1
- This is a heading
2
-
3
- This is a paragraph with a link.
4
-
5
- This is another heading
6
-
7
- In Getting Started we set up something foo.
8
-
9
- Some list
10
- With items
11
- Even indented