remove-markdown 0.1.0 → 0.3.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/README.md CHANGED
@@ -1,8 +1,11 @@
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 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.
3
6
 
4
7
  ## When do I need it?
5
- 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.
6
9
 
7
10
  ## Installation
8
11
 
@@ -12,21 +15,30 @@ npm install remove-markdown
12
15
 
13
16
  ## Usage
14
17
  ```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.'
18
+ const removeMd = require('remove-markdown');
19
+ const markdown = '# This is a heading\n\nThis is a paragraph with [a link](http://www.disney.com/) in it.';
20
+ const plainText = removeMd(markdown); // plainText is now 'This is a heading\n\nThis is a paragraph with a link in it.'
18
21
  ```
19
22
 
20
- You can also supply an options object to the function. Currently, the only two options are for stripping list headers and supporting Github Flavored Markdown:
23
+ You can also supply an options object to the function. Currently, the following options are supported:
21
24
 
22
25
  ```js
23
- var plainText = removeMd(markdown, {
24
- stripListLeaders: false,
25
- gfm: false
26
- }); // The default for both is true
26
+ const plainText = removeMd(markdown, {
27
+ stripListLeaders: true , // strip list leaders (default: true)
28
+ listUnicodeChar: '', // char to insert instead of stripped list leaders (default: '')
29
+ gfm: true // support GitHub-Flavored Markdown (default: true)
30
+ useImgAltText: true // replace images with alt-text, if present (default: true)
31
+ });
27
32
  ```
28
33
 
29
- Stripping list headers will retain any list characters (`*, -, +, (digit).`).
34
+ Setting `stripListLeaders` to false will retain any list characters (`*, -, +, (digit).`).
35
+
36
+ ## TODO
37
+ PRs are very much welcome. Here are some ideas for future enhancements:
38
+
39
+ * Allow the RegEx expressions to be customized per rule
40
+ * Make the rules more robust, support more edge cases
41
+ * Add more (comprehensive) tests
30
42
 
31
43
  ## Credits
32
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.
package/index.js CHANGED
@@ -1,17 +1,28 @@
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;
4
5
  options.gfm = options.hasOwnProperty('gfm') ? options.gfm : true;
6
+ options.useImgAltText = options.hasOwnProperty('useImgAltText') ? options.useImgAltText : true;
7
+
8
+ var output = md || '';
9
+
10
+ // 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, '');
5
12
 
6
- var output = md;
7
13
  try {
8
14
  if (options.stripListLeaders) {
9
- output = output.replace(/^([\s\t]*)([\*\-\+]|\d\.)\s+/gm, '$1');
15
+ if (options.listUnicodeChar)
16
+ output = output.replace(/^([\s\t]*)([\*\-\+]|\d+\.)\s+/gm, options.listUnicodeChar + ' $1');
17
+ else
18
+ output = output.replace(/^([\s\t]*)([\*\-\+]|\d+\.)\s+/gm, '$1');
10
19
  }
11
- if (options.gfm){
20
+ if (options.gfm) {
12
21
  output = output
13
22
  // Header
14
23
  .replace(/\n={2,}/g, '\n')
24
+ // Fenced codeblocks
25
+ .replace(/~{3}.*\n/g, '')
15
26
  // Strikethrough
16
27
  .replace(/~~/g, '')
17
28
  // Fenced codeblocks
@@ -19,26 +30,30 @@ module.exports = function(md, options) {
19
30
  }
20
31
  output = output
21
32
  // Remove HTML tags
22
- .replace(/<(.*?)>/g, '$1')
33
+ .replace(/<[^>]*>/g, '')
23
34
  // Remove setext-style headers
24
35
  .replace(/^[=\-]{2,}\s*$/g, '')
25
36
  // Remove footnotes?
26
37
  .replace(/\[\^.+?\](\: .*?$)?/g, '')
27
38
  .replace(/\s{0,2}\[.*?\]: .*?$/g, '')
28
39
  // Remove images
29
- .replace(/\!\[.*?\][\[\(].*?[\]\)]/g, '')
40
+ .replace(/\!\[(.*?)\][\[\(].*?[\]\)]/g, options.useImgAltText ? '$1' : '')
30
41
  // Remove inline links
31
42
  .replace(/\[(.*?)\][\[\(].*?[\]\)]/g, '$1')
32
- // Remove Blockquotes
33
- .replace(/>/g, '')
43
+ // Remove blockquotes
44
+ .replace(/^\s{0,3}>\s?/g, '')
34
45
  // Remove reference-style links?
35
46
  .replace(/^\s{1,2}\[(.*?)\]: (\S+)( ".*?")?\s*$/g, '')
36
47
  // Remove atx-style headers
37
- .replace(/^\#{1,6}\s*([^#]*)\s*(\#{1,6})?/gm, '$1')
38
- .replace(/([\*_]{1,3})(\S.*?\S)\1/g, '$2')
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')
52
+ // Remove code blocks
39
53
  .replace(/(`{3,})(.*?)\1/gm, '$2')
40
- .replace(/^-{3,}\s*$/g, '')
54
+ // Remove inline code
41
55
  .replace(/`(.+?)`/g, '$1')
56
+ // Replace two or more newlines with exactly two? Not entirely sure this belongs here...
42
57
  .replace(/\n{2,}/g, '\n\n');
43
58
  } catch(e) {
44
59
  console.error(e);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "remove-markdown",
3
- "version": "0.1.0",
3
+ "version": "0.3.0",
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,145 @@
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 = '![](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 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
+
54
+ it('should strip code tags', function () {
55
+ const string = 'In `Getting Started` we set up `something` foo.';
56
+ const expected = 'In Getting Started we set up something foo.';
57
+ expect(removeMd(string)).to.equal(expected);
58
+ });
59
+
60
+ it('should leave hashtags in headings', function () {
61
+ const string = '## This #heading contains #hashtags';
62
+ const expected = 'This #heading contains #hashtags';
63
+ expect(removeMd(string)).to.equal(expected);
64
+ });
65
+
66
+ it('should remove emphasis', function () {
67
+ const string = 'I italicized an *I* and it _made_ me *sad*.';
68
+ const expected = 'I italicized an I and it made me sad.';
69
+ expect(removeMd(string)).to.equal(expected);
70
+ });
71
+
72
+ it('should remove double emphasis', function () {
73
+ const string = '**this sentence has __double styling__**';
74
+ const expected = 'this sentence has double styling';
75
+ expect(removeMd(string)).to.equal(expected);
76
+ });
77
+
78
+ it('should remove horizontal rules', 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 horizontal rules with space-separated asterisks', function () {
85
+ const string = 'Some text on a line\n\n* * *\n\nA line below';
86
+ const expected = 'Some text on a line\n\nA line below';
87
+ expect(removeMd(string)).to.equal(expected);
88
+ });
89
+
90
+ it('should remove blockquotes', 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 blockquotes with spaces', function () {
97
+ const string = '> I am a blockquote';
98
+ const expected = 'I am a blockquote';
99
+ expect(removeMd(string)).to.equal(expected);
100
+ });
101
+
102
+ it('should remove indented blockquotes', function () {
103
+ var tests = [
104
+ { string: ' > I am a blockquote', expected: 'I am a blockquote' },
105
+ { string: ' > I am a blockquote', expected: 'I am a blockquote' },
106
+ { string: ' > I am a blockquote', expected: 'I am a blockquote' },
107
+ ];
108
+ tests.forEach(function (test) {
109
+ expect(removeMd(test.string)).to.equal(test.expected);
110
+ });
111
+ });
112
+
113
+ it('should not remove greater than signs', function () {
114
+ 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' },
121
+ ];
122
+ tests.forEach(function (test) {
123
+ expect(removeMd(test.string)).to.equal(test.expected);
124
+ });
125
+ });
126
+
127
+ it('should strip unordered list leaders', function () {
128
+ const string = 'Some text on a line\n\n* A list Item\n* 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 strip ordered list leaders', function () {
134
+ const string = 'Some text on a line\n\n9. A list Item\n10. Another list item';
135
+ const expected = 'Some text on a line\n\nA list Item\nAnother list item';
136
+ expect(removeMd(string)).to.equal(expected);
137
+ });
138
+
139
+ it('should handle paragraphs with markdown', function () {
140
+ 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';
141
+ 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
+ expect(removeMd(paragraph)).to.equal(expected);
143
+ });
13
144
  });
14
- });
145
+ });
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