remove-markdown 0.5.5 → 0.6.1
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 +4 -0
- package/index.js +12 -19
- package/package.json +1 -1
- package/test/remove-markdown.js +41 -0
package/README.md
CHANGED
|
@@ -1,3 +1,7 @@
|
|
|
1
|
+
[](https://www.npmjs.com/package/remove-markdown)
|
|
2
|
+
[](https://npm-compare.com/remove-markdown/#timeRange=THREE_YEARS)
|
|
3
|
+
[](https://github.com/zuchka/remove-markdown/actions/workflows/default.yaml)
|
|
4
|
+
|
|
1
5
|
## What is it?
|
|
2
6
|
**remove-markdown** is a node.js module that will remove (strip) Markdown formatting from text.
|
|
3
7
|
*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(/^(
|
|
15
|
+
output = output.replace(/^ {0,3}((?:-[\t ]*){3,}|(?:_[ \t]*){3,}|(?:\*[ \t]*){3,})(?:\n+|$)/gm, '');
|
|
16
16
|
|
|
17
17
|
try {
|
|
18
18
|
if (options.stripListLeaders) {
|
|
@@ -29,29 +29,22 @@ module.exports = function(md, options) {
|
|
|
29
29
|
.replace(/~{3}.*\n/g, '')
|
|
30
30
|
// Strikethrough
|
|
31
31
|
.replace(/~~/g, '')
|
|
32
|
-
// Fenced codeblocks
|
|
33
|
-
.replace(
|
|
32
|
+
// Fenced codeblocks with backticks
|
|
33
|
+
.replace(/```(?:.*)\n([\s\S]*?)```/g, (_, code) => code.trim());
|
|
34
34
|
}
|
|
35
35
|
if (options.abbr) {
|
|
36
36
|
// Remove abbreviations
|
|
37
37
|
output = output.replace(/\*\[.*\]:.*\n/, '');
|
|
38
38
|
}
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
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
|
-
|
|
52
|
-
|
|
53
|
-
'ig'
|
|
54
|
-
);
|
|
45
|
+
`<(?!\/?(${joinedHtmlTagsToSkip})(?=>|\s[^>]*>))[^>]*>`,
|
|
46
|
+
'g',
|
|
47
|
+
)
|
|
55
48
|
}
|
|
56
49
|
|
|
57
50
|
output = output
|
|
@@ -79,7 +72,7 @@ module.exports = function(md, options) {
|
|
|
79
72
|
// 1. Either there is a whitespace character before opening _ and after closing _.
|
|
80
73
|
// 2. Or _ is at the start/end of the string.
|
|
81
74
|
.replace(/(^|\W)([_]+)(\S)(.*?\S)??\2($|\W)/g, '$1$3$4$5')
|
|
82
|
-
// Remove code blocks
|
|
75
|
+
// Remove single-line code blocks (already handled multiline above in gfm section)
|
|
83
76
|
.replace(/(`{3,})(.*?)\1/gm, '$2')
|
|
84
77
|
// Remove inline code
|
|
85
78
|
.replace(/`(.+?)`/g, '$1')
|
package/package.json
CHANGED
package/test/remove-markdown.js
CHANGED
|
@@ -57,6 +57,24 @@ describe('remove Markdown', function () {
|
|
|
57
57
|
expect(removeMd(string)).to.equal(expected);
|
|
58
58
|
});
|
|
59
59
|
|
|
60
|
+
it('should strip simple multiline code tags', function () {
|
|
61
|
+
const string = '```\ncode\n```';
|
|
62
|
+
const expected = 'code';
|
|
63
|
+
expect(removeMd(string)).to.equal(expected);
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
it('should strip complex multiline code blocks with language specified', function () {
|
|
67
|
+
const string = '```javascript\nconst x = 1;\nconst y = 2;\nconsole.log(x + y);\n```';
|
|
68
|
+
const expected = 'const x = 1;\nconst y = 2;\nconsole.log(x + y);';
|
|
69
|
+
expect(removeMd(string)).to.equal(expected);
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
it('should strip multiline code blocks with multiple paragraphs', function () {
|
|
73
|
+
const string = 'Text before\n\n```\ncode line 1\n\ncode line 2\n```\n\nText after';
|
|
74
|
+
const expected = 'Text before\n\ncode line 1\n\ncode line 2\n\nText after';
|
|
75
|
+
expect(removeMd(string)).to.equal(expected);
|
|
76
|
+
});
|
|
77
|
+
|
|
60
78
|
it('should leave hashtags in headings', function () {
|
|
61
79
|
const string = '## This #heading contains #hashtags';
|
|
62
80
|
const expected = 'This #heading contains #hashtags';
|
|
@@ -87,6 +105,12 @@ describe('remove Markdown', function () {
|
|
|
87
105
|
expect(removeMd(string)).to.equal(expected);
|
|
88
106
|
});
|
|
89
107
|
|
|
108
|
+
it('should not mistake a horizontal rule when symbols are mixed ', function () {
|
|
109
|
+
const string = 'Some text on a line\n\n--*\n\nA line below';
|
|
110
|
+
const expected = 'Some text on a line\n\n--*\n\nA line below';
|
|
111
|
+
expect(removeMd(string)).to.equal(expected);
|
|
112
|
+
});
|
|
113
|
+
|
|
90
114
|
it('should remove horizontal rules', function () {
|
|
91
115
|
const string = 'Some text on a line\n\n---\n\nA line below';
|
|
92
116
|
const expected = 'Some text on a line\n\nA line below';
|
|
@@ -161,6 +185,12 @@ describe('remove Markdown', function () {
|
|
|
161
185
|
expect(removeMd(string)).to.equal(expected);
|
|
162
186
|
});
|
|
163
187
|
|
|
188
|
+
it('should strip list items with bold word in the beginning', function () {
|
|
189
|
+
const string = 'Some text on a line\n\n- **A** list Item\n- **Another** list item';
|
|
190
|
+
const expected = 'Some text on a line\n\nA list Item\nAnother list item';
|
|
191
|
+
expect(removeMd(string)).to.equal(expected);
|
|
192
|
+
});
|
|
193
|
+
|
|
164
194
|
it('should handle paragraphs with markdown', function () {
|
|
165
195
|
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
196
|
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 +225,16 @@ describe('remove Markdown', function () {
|
|
|
195
225
|
const expected = 'Heading in italic';
|
|
196
226
|
expect(removeMd(string)).to.equal(expected);
|
|
197
227
|
});
|
|
228
|
+
|
|
229
|
+
it('should skip specified HTML tags when htmlTagsToSkip option is provided', () => {
|
|
230
|
+
const markdown =
|
|
231
|
+
'<div>HTML content <sub>Superscript</sub> <span>span text</span></div>'
|
|
232
|
+
const result = removeMd(markdown, {htmlTagsToSkip: ['sub']})
|
|
233
|
+
expect(result).to.equal('HTML content <sub>Superscript</sub> span text')
|
|
234
|
+
const result2 = removeMd(markdown, {htmlTagsToSkip: ['sub', 'span']})
|
|
235
|
+
expect(result2).to.equal(
|
|
236
|
+
'HTML content <sub>Superscript</sub> <span>span text</span>',
|
|
237
|
+
)
|
|
238
|
+
})
|
|
198
239
|
});
|
|
199
240
|
});
|