Skip to content

Commit 7997597

Browse files
committed
add options: pedantic, gfm, sanitize.
1 parent b202cee commit 7997597

File tree

4 files changed

+83
-9
lines changed

4 files changed

+83
-9
lines changed

README.md

+22
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,31 @@ Along with implementing every markdown feature, marked also implements
6363

6464
## Usage
6565

66+
``` js
67+
var html = marked(markdown, options);
68+
```
69+
70+
### Options
71+
72+
- __pedantic__: Conform to obscure parts of `markdown.pl` as much as possible.
73+
Don't fix any of the original markdown bugs or poor behavior.
74+
- __gfm__: Enabled github flavored markdown (default for backward compatibility).
75+
- __sanitize__: Sanitize the output. Ignore an HTML that has been input.
76+
77+
None of the above are mutually exclusive/inclusive.
78+
79+
## Example Usage
80+
6681
``` js
6782
var marked = require('marked');
6883
console.log(marked('i am using __markdown__.'));
84+
85+
// gfm
86+
console.log(marked('```\ni am using GFM.\n```', {
87+
gfm: true,
88+
pedantic: false,
89+
sanitize: true
90+
}));
6991
```
7092

7193
You also have direct access to the lexer and parser if you so desire.

bin/marked

+11-1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ var help = function() {
3434

3535
var main = function(argv) {
3636
var files = []
37+
, options = {}
3738
, data = ''
3839
, input
3940
, output
@@ -64,6 +65,15 @@ var main = function(argv) {
6465
case '--tokens':
6566
tokens = true;
6667
break;
68+
case '--gfm':
69+
options.gfm = true;
70+
break;
71+
case '--sanitize':
72+
options.sanitize = true;
73+
break;
74+
case '--pedantic':
75+
options.pedantic = true;
76+
break;
6777
case '-h':
6878
case '--help':
6979
return help();
@@ -97,7 +107,7 @@ var main = function(argv) {
97107
function write() {
98108
data = tokens
99109
? JSON.stringify(marked.lexer(data), null, 2)
100-
: marked(data);
110+
: marked(data, options);
101111

102112
if (!output) {
103113
process.stdout.write(data + '\n');

lib/marked.js

+40-8
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@
55

66
;(function() {
77

8+
/**
9+
* Options
10+
*/
11+
12+
var options;
13+
814
/**
915
* Block-Level Grammar
1016
*/
@@ -109,13 +115,15 @@ block.token = function(src, tokens, top) {
109115
cap = cap[0].replace(/^ {4}/gm, '');
110116
tokens.push({
111117
type: 'code',
112-
text: cap.replace(/\n+$/, '')
118+
text: !options.pedantic
119+
? cap.replace(/\n+$/, '')
120+
: cap
113121
});
114122
continue;
115123
}
116124

117125
// gfm_code
118-
if (cap = block.gfm_code.exec(src)) {
126+
if (options.gfm && (cap = block.gfm_code.exec(src))) {
119127
src = src.substring(cap[0].length);
120128
tokens.push({
121129
type: 'code',
@@ -159,6 +167,7 @@ block.token = function(src, tokens, top) {
159167
// blockquote
160168
if (cap = block.blockquote.exec(src)) {
161169
src = src.substring(cap[0].length);
170+
162171
tokens.push({
163172
type: 'blockquote_start'
164173
});
@@ -173,6 +182,7 @@ block.token = function(src, tokens, top) {
173182
tokens.push({
174183
type: 'blockquote_end'
175184
});
185+
176186
continue;
177187
}
178188

@@ -206,7 +216,9 @@ block.token = function(src, tokens, top) {
206216
// list item contains. Hacky.
207217
if (~item.indexOf('\n ')) {
208218
space -= item.length;
209-
item = item.replace(new RegExp('^ {1,' + space + '}', 'gm'), '');
219+
item = !options.pedantic
220+
? item.replace(new RegExp('^ {1,' + space + '}', 'gm'), '')
221+
: item.replace(/^ {1,4}/gm, '');
210222
}
211223

212224
// Determine whether item is loose or not.
@@ -299,6 +311,11 @@ var inline = {
299311
nolink: /^!?\[((?:\[[^\]]*\]|[^\[\]])*)\]/,
300312
strong: /^__([^\0]+?)__(?!_)|^\*\*([^\0]+?)\*\*(?!\*)/,
301313
em: /^\b_((?:__|[^\0])+?)_\b|^\*((?:\*\*|[^\0])+?)\*(?!\*)/,
314+
315+
// pedantic strong/em
316+
p_strong: /^__(?=\S)([^\0]*?\S)__(?!_)|^\*\*(?=\S)([^\0]*?\S)\*\*(?!\*)/,
317+
p_em: /^\b_(?=\S)([^\0]*?\S)_\b|^\*(?=\S)([^\0]*?\S)\*(?!\*)/,
318+
302319
code: /^(`+)([^\0]*?[^`])\1(?!`)/,
303320
br: /^ {2,}\n(?!\s*$)/,
304321
text: /^[^\0]+?(?=[\\<!\[_*`]|\w+:\/\/| {2,}\n|$)/
@@ -345,7 +362,7 @@ inline.lexer = function(src) {
345362
}
346363

347364
// gfm_autolink
348-
if (cap = inline.gfm_autolink.exec(src)) {
365+
if (options.gfm && (cap = inline.gfm_autolink.exec(src))) {
349366
src = src.substring(cap[0].length);
350367
text = escape(cap[1]);
351368
href = text;
@@ -360,7 +377,9 @@ inline.lexer = function(src) {
360377
// tag
361378
if (cap = inline.tag.exec(src)) {
362379
src = src.substring(cap[0].length);
363-
out += cap[0];
380+
out += options.sanitize
381+
? escape(cap[0])
382+
: cap[0];
364383
continue;
365384
}
366385

@@ -561,7 +580,7 @@ var tok = function() {
561580
+ '</li>\n';
562581
}
563582
case 'html': {
564-
return !token.pre
583+
return !token.pre && !options.pedantic
565584
? inline.lexer(token.text)
566585
: token.text;
567586
}
@@ -644,13 +663,26 @@ function tag() {
644663
}
645664

646665
/**
647-
* Expose
666+
* Marked
648667
*/
649668

650-
var marked = function(src) {
669+
var marked = function(src, opt) {
670+
options = opt || marked.defaults;
651671
return parse(block.lexer(src));
652672
};
653673

674+
marked.defaults = {
675+
gfm: true,
676+
pedantic: false,
677+
sanitize: false
678+
};
679+
680+
options = marked.defaults;
681+
682+
/**
683+
* Expose
684+
*/
685+
654686
marked.parser = parse;
655687
marked.lexer = block.lexer;
656688

man/marked.1

+10
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,16 @@ is specified, read from stdin.
2222
.BI \-t,\ \-\-tokens
2323
Output a token stream instead of html.
2424
.TP
25+
.BI \-\-pedantic
26+
Conform to obscure parts of markdown.pl as much as possible. Don't fix original
27+
markdown bugs.
28+
.TP
29+
.BI \-\-gfm
30+
Enabled github flavored markdown.
31+
.TP
32+
.BI \-\-sanitize
33+
Sanitize output. Ignore any HTML input.
34+
.TP
2535
.BI \-h,\ \-\-help
2636
Display help information.
2737
.SH EXAMPLES

0 commit comments

Comments
 (0)