Comparing version 1.0.9 to 1.0.10
@@ -15,3 +15,3 @@ 'use strict' | ||
* heading h1-h6的正则 | ||
* | ||
* | ||
*/ | ||
@@ -34,2 +34,8 @@ newline: /^\n+/, | ||
() | ||
block.normal = Object.assign({}, block) | ||
block.dfm = Object.assign({}, block.normal, { | ||
codepen: /^ *@codepen +([^\n]+) *(?:\n+|$)/ | ||
}) | ||
/** | ||
@@ -53,3 +59,2 @@ * 语法解析器 | ||
parse(src) { | ||
debugger; | ||
this.inline = new InlineLexer(src.links, this.options) | ||
@@ -98,2 +103,5 @@ this.tokens = src.reverse() | ||
} | ||
case 'codepen': { | ||
return this.renderer.codepen(this.token.text) | ||
} | ||
case 'text': { | ||
@@ -133,2 +141,3 @@ return this.renderer.text(this.token.text) | ||
link: /^!?\[(inside)\]\(href\)/, | ||
autolink: /^<([^ >]+(@|:\/)[^ >]+)>/, | ||
code: /^(`+)\s*([\s\S]+?[^`])\s*\1(?!`)/, | ||
@@ -159,2 +168,4 @@ text: /^[\s\S]+?(?=[\\<!\[_*`]| {2,}\n|$)/ | ||
, cap | ||
, text | ||
, href | ||
while(src) { | ||
@@ -174,2 +185,20 @@ if(cap = this.rules.link.exec(src)) { | ||
if(cap = this.rules.autolink.exec(src)) { | ||
src = src.substring(cap[0].length) | ||
debugger | ||
if(cap[2] === '@') { | ||
text = cap[1].charAt(6) === ':' | ||
? this.mangle(cap[1].substring(7)) | ||
: this.mangle(cap[1]) | ||
href = this.mangle('mailto:') + text | ||
} else { | ||
text = escape(cap[1]) | ||
href = text | ||
} | ||
out += this.renderer.link(href, null, text) | ||
continue | ||
} | ||
if(cap = this.rules.code.exec(src)) { | ||
@@ -201,2 +230,21 @@ src = src.substring(cap[0].length) | ||
} | ||
mangle(text) { | ||
if(!this.options.mangle) return text | ||
let out = '' | ||
, l = text.length | ||
, i = 0 | ||
, ch | ||
for(; i < l; i++) { | ||
ch = text.charCodeAt(i) | ||
if(Math.random() > 0.5) { | ||
ch = 'x' + ch.toString(16) | ||
} | ||
out += '&#' + ch + ';' | ||
} | ||
return out | ||
} | ||
} | ||
@@ -215,2 +263,36 @@ /** | ||
codepen(text) { | ||
return '<iframe height="' | ||
+ this.options.codepenHeight | ||
+ '" scrolling="no" title="' | ||
+ text | ||
+ '" src="http://codepen.io/' | ||
+ this.options.codepenUsername | ||
+ '/embed/' | ||
+ text | ||
+ '?height=' | ||
+ this.options.codepenHeight | ||
+ '&theme-id=' | ||
+ this.options.themeId | ||
+ '&default-tab=' | ||
+ this.options.defaultTab | ||
+ '&embed-version=' | ||
+ this.options.embedVersion | ||
+ '" frameborder="no" allowtransparency="true" allowfullscreen="true" style="width: 100%;"">See the Pen <a href="http://codepen.io/' | ||
+ this.options.codepenUsername | ||
+ '/pen/' | ||
+ text | ||
+ '/">' | ||
+ text | ||
+ '</a>' | ||
+ ' by ' | ||
+ this.options.codepenUsername | ||
+ ' (<a href="http://codepen.io/' | ||
+ this.options.codepenUsername | ||
+ '">@' | ||
+ this.options.codepenUsername | ||
+ '</a>) on <a href="http://codepen.io">CodePen</a>.' | ||
+ '</iframe>' | ||
} | ||
heading(text, level, id) { | ||
@@ -292,2 +374,6 @@ return '<h' | ||
this.options = options || cmarked.options | ||
if(this.options.dfm) { | ||
this.rules = block.dfm | ||
} | ||
} | ||
@@ -347,2 +433,12 @@ | ||
if(cap = this.rules.codepen.exec(src)) { | ||
src = src.substring(cap[0].length) | ||
this.tokens.push({ | ||
type: 'codepen', | ||
text: cap[1] | ||
}) | ||
continue | ||
} | ||
if(cap = this.rules.heading.exec(src)) { | ||
@@ -381,6 +477,9 @@ src = src.substring(cap[0].length) | ||
debugger; | ||
if(~item.indexOf('\n ')) { | ||
space -= item.length | ||
item = this.options.pedantic | ||
// pedantic 迂腐的 | ||
// 如果关闭了此选项,marked会智能的缩进属于li里面的内容,但是我并不认为这是一个非常好的行为,因为如果我在li里面嵌套的代码块,也许会导致代码会出现不可预料的缩进行为。 | ||
item = !this.options.pedantic | ||
? item.replace(new RegExp('^ {1,' + space + '}', 'gm'), '') | ||
@@ -394,3 +493,3 @@ : item.replace(/ {1,4}/gm, '') | ||
} | ||
// debugger | ||
this.tokens.push({ | ||
@@ -446,2 +545,14 @@ type: loose | ||
/* | ||
* 帮助函数 | ||
*/ | ||
function escape(html, encode) { | ||
return html | ||
.replace(/</g, '<') | ||
.replace(/>/g, '>') | ||
.replace(/"/g, '"') | ||
.replace(/'/g, ''') | ||
} | ||
function replace(regex, opt) { | ||
@@ -462,10 +573,8 @@ regex = regex.source | ||
if(!options) options = cmarked.default | ||
debugger; | ||
// debugger; | ||
return Parser.parse(Lexer.lex(src, options), options) | ||
} | ||
cmarked.options = | ||
cmarked.setOptions = function(opt) { | ||
Object.assign(cmarked.default, opt) | ||
return cmarked | ||
cmarked.default = Object.assign(cmarked.default, opt) | ||
} | ||
@@ -476,5 +585,14 @@ | ||
gap: '-', | ||
smartOrderList: false | ||
smartOrderList: false, | ||
pedantic: true, | ||
codepenUsername: 'aizhizhi', | ||
codepenHeight: '274', | ||
defaultTab: 'html', | ||
themeId: '22001', | ||
embedVersion: '2', | ||
dfm: false | ||
} | ||
cmarked.InlineLexer = InlineLexer | ||
/** | ||
@@ -481,0 +599,0 @@ * 调用cmarked → 将源码传递给词法解析器Lexer.lex → 词法解析器将自己解析的结果交给语法解析器Parser.parse |
{ | ||
"name": "cmarked", | ||
"version": "1.0.9", | ||
"version": "1.0.10", | ||
"description": "更符合汉语的markdown编译器", | ||
@@ -25,4 +25,6 @@ "main": "index.js", | ||
"devDependencies": { | ||
"chai": "^3.5.0" | ||
"chai": "^3.5.0", | ||
"express": "^4.10.2", | ||
"socket.io": "^1.7.2" | ||
} | ||
} |
@@ -70,5 +70,5 @@ # cmarked | ||
console.log(cmarked('3. cmarked\n4. cmarked')) | ||
//输出 | ||
1. cmarked | ||
2. cmarked | ||
//输出效果 | ||
//1. cmarked | ||
//2. cmarked | ||
``` | ||
@@ -81,5 +81,5 @@ | ||
console.log(cmarked('3. cmarked\n4. cmarked')) | ||
//输出 | ||
3. cmarked | ||
4. cmarked | ||
//输出效果 | ||
//3. cmarked | ||
//4. cmarked | ||
``` | ||
@@ -86,0 +86,0 @@ |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
31353
10
887
3
2
1