remove-markdown 0.3.0 → 0.5.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.
@@ -0,0 +1,27 @@
1
+ name: Run tests
2
+
3
+ on:
4
+ push:
5
+ branches: [ main ]
6
+ pull_request:
7
+ branches: [ main ]
8
+
9
+ jobs:
10
+ build:
11
+ runs-on: ubuntu-latest
12
+
13
+ strategy:
14
+ matrix:
15
+ node-version: [14.x, 16.x, 18.x]
16
+ # See supported Node.js release schedule at https://nodejs.org/en/about/releases/
17
+
18
+ steps:
19
+ - uses: actions/checkout@v3
20
+ - name: Use Node.js ${{ matrix.node-version }}
21
+ uses: actions/setup-node@v3
22
+ with:
23
+ node-version: ${{ matrix.node-version }}
24
+ cache: 'npm'
25
+ - run: npm ci
26
+ - run: npm run build --if-present
27
+ - run: npm test
package/README.md CHANGED
@@ -1,4 +1,4 @@
1
- [![CircleCI](https://circleci.com/gh/stiang/remove-markdown.svg?style=svg&circle-token=cac2feef7dc90e6b8578aec361be369412be1c6a)](https://circleci.com/gh/stiang/remove-markdown)
1
+ ![default workflow](https://github.com/stiang/remove-markdown/actions/workflows/default.yaml/badge.svg)
2
2
 
3
3
  ## What is it?
4
4
  **remove-markdown** is a node.js module that will remove (strip) Markdown formatting from text.
@@ -43,5 +43,6 @@ PRs are very much welcome. Here are some ideas for future enhancements:
43
43
  ## Credits
44
44
  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.
45
45
 
46
- ## Author
47
- Stian Grytøyr
46
+ ## Authors
47
+ Stian Grytøyr (original creator)
48
+ [zuchka](https://github.com/zuchka) (maintainer since 2023)
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(/^(\n)?\s{0,3}>\s?/gm, '$1')
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*( (.+))? +#+$|^(\n)?\s{0,}#{1,6}\s*( (.+))?$/gm, '$1$3$4$6')
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.2",
4
4
  "description": "Remove Markdown formatting from text",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -8,7 +8,7 @@
8
8
  },
9
9
  "repository": {
10
10
  "type": "git",
11
- "url": "https://github.com/stiang/remove-markdown.git"
11
+ "url": "https://github.com/zuchka/remove-markdown.git"
12
12
  },
13
13
  "keywords": [
14
14
  "markdown"
@@ -16,12 +16,12 @@
16
16
  "author": "Stian Grytøyr",
17
17
  "license": "MIT",
18
18
  "bugs": {
19
- "url": "https://github.com/stiang/remove-markdown/issues"
19
+ "url": "https://github.com/zuchka/remove-markdown/issues"
20
20
  },
21
- "homepage": "https://github.com/stiang/remove-markdown",
21
+ "homepage": "https://github.com/zuchka/remove-markdown",
22
22
  "devDependencies": {
23
23
  "chai": "^4.0.2",
24
- "mocha": "^2.1.0",
24
+ "mocha": "^10.2.0",
25
25
  "should": "^5.0.0"
26
26
  }
27
27
  }
@@ -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,34 @@ 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(1000);
185
+ });
186
+
187
+ it('should work fast even with lots of whitespace', function () {
188
+ const string = 'Some text with lots of whitespace';
189
+ const expected = 'Some text with lots of whitespace';
190
+ expect(removeMd(string)).to.equal(expected);
191
+ });
192
+
193
+ it('should still remove escaped markdown syntax', function () {
194
+ const string = '\# Heading in _italic_';
195
+ const expected = 'Heading in italic';
196
+ expect(removeMd(string)).to.equal(expected);
197
+ });
144
198
  });
145
199
  });
@@ -1,84 +0,0 @@
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/.npmignore DELETED
@@ -1 +0,0 @@
1
- node_modules/