Stackedit/public/res/libs/prism-markdown.js

346 lines
7.3 KiB
JavaScript
Raw Normal View History

2014-06-12 20:15:41 +00:00
Prism.languages.md = (function() {
2014-03-19 22:10:59 +00:00
2014-06-12 20:15:41 +00:00
var charInsideUrl = "[-A-Z0-9+&@#/%?=~_|[\\]()!:,.;]",
charEndingUrl = "[-A-Z0-9+&@#/%=~_|[\\])]";
var urlPattern = new RegExp("(https?|ftp)(://" + charInsideUrl + "*" + charEndingUrl + ")(?=$|\\W)", "gi");
var emailPattern = /(?:mailto:)?([-.\w]+\@[-a-z0-9]+(\.[-a-z0-9]+)*\.[a-z]+)/gi;
2014-03-16 02:13:42 +00:00
2014-06-12 20:15:41 +00:00
var latex = Prism.languages.latex;
2014-04-05 00:54:06 +00:00
2014-06-12 20:15:41 +00:00
var lf = /\n/gm;
2014-05-19 18:07:51 +00:00
2014-06-12 20:15:41 +00:00
var md = {};
md['pre gfm'] = {
pattern: /^`{3}.*\n(?:[\s\S]*?)\n`{3} *$/gm,
inside: {
"md md-pre": /`{3}/,
lf: lf
}
};
md['h1 alt'] = {
pattern: /^(.+)[ \t]*\n=+[ \t]*$/gm,
inside: {
}
};
md['h2 alt'] = {
pattern: /^(.+)[ \t]*\n-+[ \t]*$/gm,
inside: {
}
};
for(var i = 6; i >= 1; i--) {
md["h" + i] = {
pattern: new RegExp("^#{" + i + "}.+$", "gm"),
inside: {
"md md-hash": new RegExp("^#{" + i + "}")
}
};
}
md.li = {
pattern: /^[ \t]*([*+\-]|\d+\.)[ \t].+(?:\n|[ \t].*\n)*/gm,
inside: {
"md md-li": /^[ \t]*([*+\-]|\d+\.)[ \t]/m,
'pre gfm': {
pattern: /^((?: {4}|\t)+)`{3}.*\n(?:[\s\S]*?)\n\1`{3} *$/gm,
inside: {
"md md-pre": /`{3}/,
lf: lf
}
},
lf: lf
}
};
md.pre = {
pattern: /(^|(?:^|(?:^|\n)(?![ \t]*([*+\-]|\d+\.)[ \t]).*\n)\s*?\n)(\s*(?: {4}|\t).*(?:\n|$))+/g,
lookbehind: true,
inside: {
2014-08-25 08:13:37 +00:00
lf: lf
2014-06-12 20:15:41 +00:00
}
};
md.table = {
pattern: new RegExp(
[
'^' ,
'[ ]{0,3}' , // Allowed whitespace
'[|]' , // Initial pipe
'(.+)\\n' , // $1: Header Row
2014-05-19 18:07:51 +00:00
2014-06-12 20:15:41 +00:00
'[ ]{0,3}' , // Allowed whitespace
'[|]([ ]*[-:]+[-| :]*)\\n' , // $2: Separator
2014-05-19 18:07:51 +00:00
2014-06-12 20:15:41 +00:00
'(' , // $3: Table Body
'(?:[ ]*[|].*\\n?)*' , // Table rows
')',
'(?:\\n|$)' // Stop at final newline
].join(''),
'gm'
),
inside: {
lf: lf
}
};
md['table alt'] = {
pattern: new RegExp(
[
'^' ,
'[ ]{0,3}' , // Allowed whitespace
'(\\S.*[|].*)\\n' , // $1: Header Row
2014-05-19 18:07:51 +00:00
2014-06-12 20:15:41 +00:00
'[ ]{0,3}' , // Allowed whitespace
'([-:]+[ ]*[|][-| :]*)\\n' , // $2: Separator
2014-05-19 18:07:51 +00:00
2014-06-12 20:15:41 +00:00
'(' , // $3: Table Body
'(?:.*[|].*\\n?)*' , // Table rows
')' ,
'(?:\\n|$)' // Stop at final newline
].join(''),
'gm'
),
inside: {
lf: lf
}
};
2014-03-19 22:10:59 +00:00
2014-06-12 20:15:41 +00:00
md.hr = {
pattern: /^([*\-_] *){3,}$/gm
};
md.blockquote = {
pattern: /^ {0,3}> *[^\n]+$/gm,
inside: {
"md md-gt": /^ {0,3}> */,
"li": md.li
}
};
md['math block'] = {
pattern: /(\$\$|\\\\\[|\\\\\\\\\()[\s\S]*?(\$\$|\\\\\]|\\\\\\\\\))/g,
inside: {
"md md-bracket-start": /^(\$\$|\\\\\[|\\\\\\\\\()/,
"md md-bracket-end": /(\$\$|\\\\\]|\\\\\\\\\))/,
lf: lf,
rest: latex
}
};
md['latex block'] = {
pattern: /\\?\\begin\{[a-z]*\*?\}[\s\S]*?\\?\\end\{[a-z]*\*?\}/g,
inside: {
"keyword": /\\?\\(begin|end)/,
lf: lf,
rest: latex
}
};
md.fndef = {
pattern: /^ {0,3}\[\^.*?\]:[ \t]+.*$/gm,
inside: {
"ref-id": {
pattern: /\[\^.*?\]/,
inside: {
"md md-bracket-start": /\[/,
"md md-bracket-end": /\]/
}
}
}
};
md.linkdef = {
pattern: /^ {0,3}\[.*?\]:[ \t]+.*$/gm,
inside: {
"link-id": {
pattern: /\[.*?\]/,
inside: {
"md md-bracket-start": /\[/,
"md md-bracket-end": /\]/
}
},
url: urlPattern,
linktitle: /['\"\(][^\'\"\)]*['\"\)]/
}
};
md.p = {
pattern: /.+/g,
inside: {
'md md-toc': /^\s*\[(toc|TOC)\]\s*$/g
}
};
md.lf = /^\n$/gm;
md.img = {
pattern: /!\[[^\]]*\]\([^\)]+\)/g,
inside: {
"md md-bang": /^!/,
"md md-bracket-start": /\[/,
"md md-alt": /[^\[]+(?=\])/,
"md md-bracket-end": /\](?=\()/,
"md img-parens": {
pattern: /\([^\)]+\)/,
inside: {
"md md-paren-start": /^\(/,
"md md-title": /(['][^']*[']|["“][^"”]*["”])(?=\)$)/,
"md md-src": /[^\('" \t]+(?=[\)'" \t])/,
"md md-paren-end": /\)$/
}
}
}
};
md.link = {
pattern: /\[(?:(\\.)|[^\[\]])*\]\([^\(\)\s]+(\(\S*?\))??[^\(\)\s]*?(\s(['][^']*[']|["“][^"”]*["”]))?\)/gm,
inside: {
"md md-bracket-start": {
pattern: /(^|[^\\])\[/,
lookbehind: true
},
"md md-underlined-text": {
pattern: /(?:(\\.)|[^\[\]])+(?=\])/
},
"md md-bracket-end": /\]\s?\(/,
"md md-paren-end": /\)$/,
"md md-href": /.*/
}
};
md.fn = {
pattern: /\[\^(.*?)\]/g,
inside: {
"ref": {
pattern: /^\[[^\[\]]+\] ?/,
inside: {
"md md-bracket-start": /\[/,
"md md-ref": /^[^\[\]]+/,
"md md-bracket-end": /\]/
}
}
}
};
md.imgref = {
pattern: /!\[(.*?)\] ?\[(.*?)\]/g,
inside: {
"md md-bang": /^!/,
"ref-end": {
pattern: /\[[^\[\]]+\]$/,
inside: {
"md md-bracket-start": /\[/,
"md md-href": /[^\[\]]+(?=]$)/,
"md md-bracket-end": /\]/
}
},
"ref-start": {
pattern: /^\[[^\[\]]+\] ?/,
inside: {
"md md-bracket-start": /\[/,
"md md-alt": /^[^\[\]]+/,
"md md-bracket-end": /\]/
}
}
}
};
md.linkref = {
pattern: /\[(.*?)\] ?\[(.*?)\]/g,
inside: {
"ref-end": {
pattern: /\[[^\[\]]+\]$/,
inside: {
"md md-bracket-start": /\[/,
"md md-href": /[^\[\]]+(?=]$)/,
"md md-bracket-end": /\]/
}
},
"ref-start": {
pattern: /^\[[^\[\]]+\] ?/,
inside: {
"md md-bracket-start": /\[/,
"md md-underlined-text": /^[^\[\]]+/,
"md md-bracket-end": /\]/
}
}
}
};
md.code = {
pattern: /(^|[^\\])(`+)([^\r]*?[^`])\2(?!`)/g,
lookbehind: true,
inside: {
"md md-code": /`/
}
};
md.math = {
pattern: /\$.*?\$/g,
inside: {
"md md-bracket-start": /^\$/,
"md md-bracket-end": /\$$/,
rest: latex
}
};
md.strong = {
pattern: /([_\*])\1((?!\1{2}).)*\1{2}/g,
inside: {
"md md-strong": /([_\*])\1/g
}
};
md.em = {
pattern: /(^|[^\\])(\*|_)(\S[^\2]*?)??[^\s\\]+?\2/g,
lookbehind: true,
inside: {
"md md-em md-start": /^(\*|_)/,
"md md-em md-close": /(\*|_)$/
}
};
md.strike = {
pattern: /(^|\n|\W)(~~)(?=\S)([^\r]*?\S)\2/gm,
lookbehind: true,
inside: {
"md md-s": /(~~)/,
"md-strike-text": /[^~]+/
}
};
var rest = {
code: md.code,
math: md.math,
fn: md.fn,
img: md.img,
link: md.link,
imgref: md.imgref,
linkref: md.linkref,
2014-08-10 23:47:04 +00:00
url: urlPattern,
email: emailPattern,
2014-06-12 20:15:41 +00:00
strong: md.strong,
em: md.em,
strike: md.strike,
conflict: //g,
comment: Prism.languages.markup.comment,
tag: Prism.languages.markup.tag,
2014-08-10 23:47:04 +00:00
entity: Prism.languages.markup.entity
2014-06-12 20:15:41 +00:00
};
2014-06-09 11:59:19 +00:00
2014-06-12 20:15:41 +00:00
for(var c = 6; c >= 1; c--) {
md["h" + c].inside.rest = rest;
}
md["h1 alt"].inside.rest = rest;
md["h2 alt"].inside.rest = rest;
md.table.inside.rest = rest;
md["table alt"].inside.rest = rest;
md.p.inside.rest = rest;
md.blockquote.inside.rest = rest;
md.li.inside.rest = rest;
md.fndef.inside.rest = rest;
2014-03-19 22:10:59 +00:00
2014-06-12 20:15:41 +00:00
rest = {
code: md.code,
fn: md.fn,
link: md.link,
linkref: md.linkref,
conflict: //g,
};
md.strong.inside.rest = rest;
md.em.inside.rest = rest;
md.strike.inside.rest = rest;
2014-03-19 22:10:59 +00:00
2014-06-12 20:15:41 +00:00
var inside = {
code: md.code,
strong: md.strong,
em: md.em,
strike: md.strike,
conflict: //g,
comment: Prism.languages.markup.comment,
tag: Prism.languages.markup.tag,
entity: Prism.languages.markup.entity
};
md.link.inside["md md-underlined-text"].inside = inside;
md.linkref.inside["ref-start"].inside["md md-underlined-text"].inside = inside;
2014-05-27 13:33:06 +00:00
2014-06-12 20:15:41 +00:00
return md;
2014-03-19 22:10:59 +00:00
})();