remove-markdown 0.2.1 → 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.
@@ -0,0 +1,84 @@
1
+ # This configuration was automatically generated from a CircleCI 1.0 config.
2
+ # It should include any build commands you had along with commands that CircleCI
3
+ # inferred from your project structure. We strongly recommend you read all the
4
+ # comments in this file to understand the structure of CircleCI 2.0, as the idiom
5
+ # for configuration has changed substantially in 2.0 to allow arbitrary jobs rather
6
+ # than the prescribed lifecycle of 1.0. In general, we recommend using this generated
7
+ # configuration as a reference rather than using it in production, though in most
8
+ # cases it should duplicate the execution of your original 1.0 config.
9
+ version: 2
10
+ jobs:
11
+ build:
12
+ working_directory: ~/stiang/remove-markdown
13
+ parallelism: 1
14
+ shell: /bin/bash --login
15
+ # CircleCI 2.0 does not support environment variables that refer to each other the same way as 1.0 did.
16
+ # If any of these refer to each other, rewrite them so that they don't or see https://circleci.com/docs/2.0/env-vars/#interpolating-environment-variables-to-set-other-environment-variables .
17
+ environment:
18
+ CIRCLE_ARTIFACTS: /tmp/circleci-artifacts
19
+ CIRCLE_TEST_REPORTS: /tmp/circleci-test-results
20
+ # In CircleCI 1.0 we used a pre-configured image with a large number of languages and other packages.
21
+ # In CircleCI 2.0 you can now specify your own image, or use one of our pre-configured images.
22
+ # The following configuration line tells CircleCI to use the specified docker image as the runtime environment for you job.
23
+ # We have selected a pre-built image that mirrors the build environment we use on
24
+ # the 1.0 platform, but we recommend you choose an image more tailored to the needs
25
+ # of each job. For more information on choosing an image (or alternatively using a
26
+ # VM instead of a container) see https://circleci.com/docs/2.0/executor-types/
27
+ # To see the list of pre-built images that CircleCI provides for most common languages see
28
+ # https://circleci.com/docs/2.0/circleci-images/
29
+ docker:
30
+ - image: circleci/build-image:ubuntu-14.04-XXL-upstart-1189-5614f37
31
+ command: /sbin/init
32
+ steps:
33
+ # Machine Setup
34
+ # If you break your build into multiple jobs with workflows, you will probably want to do the parts of this that are relevant in each
35
+ # The following `checkout` command checks out your code to your working directory. In 1.0 we did this implicitly. In 2.0 you can choose where in the course of a job your code should be checked out.
36
+ - checkout
37
+ # Prepare for artifact and test results collection equivalent to how it was done on 1.0.
38
+ # In many cases you can simplify this from what is generated here.
39
+ # 'See docs on artifact collection here https://circleci.com/docs/2.0/artifacts/'
40
+ - run: mkdir -p $CIRCLE_ARTIFACTS $CIRCLE_TEST_REPORTS
41
+ # Dependencies
42
+ # This would typically go in either a build or a build-and-test job when using workflows
43
+ # Restore the dependency cache
44
+ - restore_cache:
45
+ keys:
46
+ # This branch if available
47
+ - v1-dep-{{ .Branch }}-
48
+ # Default branch if not
49
+ - v1-dep-master-
50
+ # Any branch if there are none on the default branch - this should be unnecessary if you have your default branch configured correctly
51
+ - v1-dep-
52
+ # The following line was run implicitly in your 1.0 builds based on what CircleCI inferred about the structure of your project. In 2.0 you need to be explicit about which commands should be run. In some cases you can discard inferred commands if they are not relevant to your project.
53
+ - run: if [ -z "${NODE_ENV:-}" ]; then export NODE_ENV=test; fi
54
+ - run: export PATH="~/stiang/remove-markdown/node_modules/.bin:$PATH"
55
+ - run: npm install
56
+ # Save dependency cache
57
+ - save_cache:
58
+ key: v1-dep-{{ .Branch }}-{{ epoch }}
59
+ paths:
60
+ # This is a broad list of cache paths to include many possible development environments
61
+ # You can probably delete some of these entries
62
+ - vendor/bundle
63
+ - ~/virtualenvs
64
+ - ~/.m2
65
+ - ~/.ivy2
66
+ - ~/.bundle
67
+ - ~/.go_workspace
68
+ - ~/.gradle
69
+ - ~/.cache/bower
70
+ - ./node_modules
71
+ # Test
72
+ # This would typically be a build job when using workflows, possibly combined with build
73
+ # The following line was run implicitly in your 1.0 builds based on what CircleCI inferred about the structure of your project. In 2.0 you need to be explicit about which commands should be run. In some cases you can discard inferred commands if they are not relevant to your project.
74
+ - run: npm test
75
+ # Teardown
76
+ # If you break your build into multiple jobs with workflows, you will probably want to do the parts of this that are relevant in each
77
+ # Save test results
78
+ - store_test_results:
79
+ path: /tmp/circleci-test-results
80
+ # Save artifacts
81
+ - store_artifacts:
82
+ path: /tmp/circleci-artifacts
83
+ - store_artifacts:
84
+ path: /tmp/circleci-test-results
package/LICENSE CHANGED
File without changes
package/README.md CHANGED
@@ -1,10 +1,11 @@
1
1
  [![CircleCI](https://circleci.com/gh/stiang/remove-markdown.svg?style=svg&circle-token=cac2feef7dc90e6b8578aec361be369412be1c6a)](https://circleci.com/gh/stiang/remove-markdown)
2
2
 
3
3
  ## What is it?
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.
4
+ **remove-markdown** is a node.js module that will remove (strip) Markdown formatting from text.
5
+ *Markdown formatting* means pretty much anything that doesn’t look like regular text, like square brackets, asterisks etc.
5
6
 
6
7
  ## When do I need it?
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.
8
+ The typical use case is to display an excerpt from some Markdown text, without any of the actual Markdown syntax - for example in a list of posts.
8
9
 
9
10
  ## Installation
10
11
 
@@ -22,17 +23,19 @@ const plainText = removeMd(markdown); // plainText is now 'This is a heading\n\n
22
23
  You can also supply an options object to the function. Currently, the following options are supported:
23
24
 
24
25
  ```js
25
- var plainText = removeMd(markdown, {
26
+ const plainText = removeMd(markdown, {
26
27
  stripListLeaders: true , // strip list leaders (default: true)
27
28
  listUnicodeChar: '', // char to insert instead of stripped list leaders (default: '')
28
29
  gfm: true // support GitHub-Flavored Markdown (default: true)
30
+ useImgAltText: true // replace images with alt-text, if present (default: true)
29
31
  });
30
32
  ```
31
33
 
32
34
  Setting `stripListLeaders` to false will retain any list characters (`*, -, +, (digit).`).
33
35
 
34
36
  ## TODO
35
- PRs are very much welcome.
37
+ PRs are very much welcome. Here are some ideas for future enhancements:
38
+
36
39
  * Allow the RegEx expressions to be customized per rule
37
40
  * Make the rules more robust, support more edge cases
38
41
  * Add more (comprehensive) tests
package/index.js CHANGED
@@ -3,11 +3,15 @@ module.exports = function(md, options) {
3
3
  options.listUnicodeChar = options.hasOwnProperty('listUnicodeChar') ? options.listUnicodeChar : false;
4
4
  options.stripListLeaders = options.hasOwnProperty('stripListLeaders') ? options.stripListLeaders : true;
5
5
  options.gfm = options.hasOwnProperty('gfm') ? options.gfm : true;
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 : [];
6
10
 
7
11
  var output = md || '';
8
12
 
9
13
  // 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, '');
14
+ output = output.replace(/^(-\s*?|\*\s*?|_\s*?){3,}\s*/gm, '');
11
15
 
12
16
  try {
13
17
  if (options.stripListLeaders) {
@@ -18,40 +22,72 @@ module.exports = function(md, options) {
18
22
  }
19
23
  if (options.gfm) {
20
24
  output = output
21
- // Header
25
+ // Header
22
26
  .replace(/\n={2,}/g, '\n')
27
+ // Fenced codeblocks
28
+ .replace(/~{3}.*\n/g, '')
23
29
  // Strikethrough
24
30
  .replace(/~~/g, '')
25
31
  // Fenced codeblocks
26
32
  .replace(/`{3}.*\n/g, '');
27
33
  }
34
+ if (options.abbr) {
35
+ // Remove abbreviations
36
+ output = output.replace(/\*\[.*\]:.*\n/, '');
37
+ }
28
38
  output = output
29
- // Remove HTML tags
39
+ // Remove HTML tags
30
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, '')
31
59
  // Remove setext-style headers
32
60
  .replace(/^[=\-]{2,}\s*$/g, '')
33
61
  // Remove footnotes?
34
62
  .replace(/\[\^.+?\](\: .*?$)?/g, '')
35
63
  .replace(/\s{0,2}\[.*?\]: .*?$/g, '')
36
64
  // Remove images
37
- .replace(/\!\[.*?\][\[\(].*?[\]\)]/g, '')
65
+ .replace(/\!\[(.*?)\][\[\(].*?[\]\)]/g, options.useImgAltText ? '$1' : '')
38
66
  // Remove inline links
39
- .replace(/\[(.*?)\][\[\(].*?[\]\)]/g, '$1')
40
- // Remove Blockquotes
41
- .replace(/>/g, '')
67
+ .replace(/\[([^\]]*?)\][\[\(].*?[\]\)]/g, options.replaceLinksWithURL ? '$2' : '$1')
68
+ // Remove blockquotes
69
+ .replace(/^\s{0,3}>\s?/gm, '')
70
+ // .replace(/(^|\n)\s{0,3}>\s?/g, '\n\n')
42
71
  // Remove reference-style links?
43
72
  .replace(/^\s{1,2}\[(.*?)\]: (\S+)( ".*?")?\s*$/g, '')
44
73
  // Remove atx-style headers
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')
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')
49
81
  // Remove code blocks
50
82
  .replace(/(`{3,})(.*?)\1/gm, '$2')
51
83
  // Remove inline code
52
84
  .replace(/`(.+?)`/g, '$1')
53
- // Replace two or more newlines with exactly two? Not entirely sure this belongs here...
54
- .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');
55
91
  } catch(e) {
56
92
  console.error(e);
57
93
  return md;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "remove-markdown",
3
- "version": "0.2.1",
3
+ "version": "0.5.0",
4
4
  "description": "Remove Markdown formatting from text",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -40,11 +40,17 @@ describe('remove Markdown', function () {
40
40
  });
41
41
 
42
42
  it('should strip img tags', function () {
43
- const string = '![bear](https://placebear.com/640/480)*Javascript* developers are the _best_.';
43
+ const string = '![](https://placebear.com/640/480)*Javascript* developers are the _best_.';
44
44
  const expected = 'Javascript developers are the best.';
45
45
  expect(removeMd(string)).to.equal(expected);
46
46
  });
47
47
 
48
+ it('should use the alt-text of an image, if it is provided', function () {
49
+ const string = '![This is the alt-text](https://www.example.com/images/logo.png)';
50
+ const expected = 'This is the alt-text';
51
+ expect(removeMd(string)).to.equal(expected);
52
+ });
53
+
48
54
  it('should strip code tags', function () {
49
55
  const string = 'In `Getting Started` we set up `something` foo.';
50
56
  const expected = 'In Getting Started we set up something foo.';
@@ -63,11 +69,23 @@ describe('remove Markdown', function () {
63
69
  expect(removeMd(string)).to.equal(expected);
64
70
  });
65
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
+
66
84
  it('should remove double emphasis', function () {
67
85
  const string = '**this sentence has __double styling__**';
68
86
  const expected = 'this sentence has double styling';
69
87
  expect(removeMd(string)).to.equal(expected);
70
- });
88
+ });
71
89
 
72
90
  it('should remove horizontal rules', function () {
73
91
  const string = 'Some text on a line\n\n---\n\nA line below';
@@ -81,6 +99,56 @@ describe('remove Markdown', function () {
81
99
  expect(removeMd(string)).to.equal(expected);
82
100
  });
83
101
 
102
+ it('should remove blockquotes', function () {
103
+ const string = '>I am a blockquote';
104
+ const expected = 'I am a blockquote';
105
+ expect(removeMd(string)).to.equal(expected);
106
+ });
107
+
108
+ it('should remove blockquotes with spaces', function () {
109
+ const string = '> I am a blockquote';
110
+ const expected = 'I am a blockquote';
111
+ expect(removeMd(string)).to.equal(expected);
112
+ });
113
+
114
+ it('should remove indented blockquotes', function () {
115
+ var tests = [
116
+ { string: ' > I am a blockquote', expected: 'I am a blockquote' },
117
+ { string: ' > I am a blockquote', expected: 'I am a blockquote' },
118
+ { string: ' > I am a blockquote', expected: 'I am a blockquote' },
119
+ ];
120
+ tests.forEach(function (test) {
121
+ expect(removeMd(test.string)).to.equal(test.expected);
122
+ });
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
+ // });
137
+
138
+ it('should not remove greater than signs', function () {
139
+ var tests = [
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' },
146
+ ];
147
+ tests.forEach(function (test) {
148
+ expect(removeMd(test.string)).to.equal(test.expected);
149
+ });
150
+ });
151
+
84
152
  it('should strip unordered list leaders', function () {
85
153
  const string = 'Some text on a line\n\n* A list Item\n* Another list item';
86
154
  const expected = 'Some text on a line\n\nA list Item\nAnother list item';
@@ -98,5 +166,22 @@ describe('remove Markdown', function () {
98
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';
99
167
  expect(removeMd(paragraph)).to.equal(expected);
100
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
+ });
101
186
  });
102
187
  });
package/.npmignore DELETED
@@ -1 +0,0 @@
1
- node_modules/