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

344 lines
10 KiB
JavaScript
Raw Normal View History

2014-04-06 23:39:24 +00:00
// Credit to https://editorially.com/
2014-03-16 02:13:42 +00:00
Prism.languages.md = (function() {
2014-03-19 22:10:59 +00:00
2014-03-16 02:13:42 +00:00
var urlPattern = /\b((?:[a-z][\w-]+:(?:\/{1,3}|[a-z0-9%])|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}\/)(?:[^\s()<>\[\]'"]+|\([^\s()<>\[\]'"]*\))+(?:\([^\s()<>\[\]'"]*\)|[^\s`!()\[\]{}:'".,<>?«»“”‘’]))/gi;
var emailPattern = /[a-z0-9!#$%&'*+\/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+\/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+(?:[A-Z]{2}|com|org|net|edu|gov|mil|biz|info|mobi|name|aero|asia|jobs|museum)\b/gi;
2014-04-05 00:54:06 +00:00
var latex = Prism.languages.latex;
2014-03-16 02:13:42 +00:00
var md = {};
md.pre = {
2014-03-19 22:10:59 +00:00
pattern: /(^|(?:^|(?:^|\n)(?![ \t]*([*+\-]|\d+\.)[ \t]).*\n)\s*?\n)(\s*(?: {4}|\t).*(?:\n|$))+/g,
2014-03-16 02:13:42 +00:00
lookbehind: true,
inside: {
}
};
md['pre gfm'] = {
pattern: /^ {0,3}`{3}.*?\n(.*?\n)*? {0,3}`{3} *$/gm,
inside: {
2014-04-24 23:43:57 +00:00
"md md-pre": /`{3}/,
lf: /\n/gm,
2014-03-16 02:13:42 +00:00
}
};
md['h1 alt'] = {
pattern: /^(.+)[ \t]*\n=+[ \t]*$/gm,
inside: {
}
};
md['h2 alt'] = {
pattern: /^(.+)[ \t]*\n-+[ \t]*$/gm,
inside: {
}
};
2014-05-19 18:07:51 +00:00
md.table = {
pattern: new RegExp(
['^' ,
'[ ]{0,3}' , // Allowed whitespace
'[|]' , // Initial pipe
'(.+)\\n' , // $1: Header Row
'[ ]{0,3}' , // Allowed whitespace
'[|]([ ]*[-:]+[-| :]*)\\n' , // $2: Separator
'(' , // $3: Table Body
'(?:[ ]*[|].*\\n?)*' , // Table rows
')',
'(?:\\n|$)' // Stop at final newline
].join(''),
'gm'
),
inside: {
lf: /\n/gm
}
};
md['table alt'] = {
pattern: new RegExp(
['^' ,
'[ ]{0,3}' , // Allowed whitespace
'(\\S.*[|].*)\\n' , // $1: Header Row
'[ ]{0,3}' , // Allowed whitespace
'([-:]+[ ]*[|][-| :]*)\\n' , // $2: Separator
'(' , // $3: Table Body
'(?:.*[|].*\\n?)*' , // Table rows
')' ,
'(?:\\n|$)' // Stop at final newline
].join(''),
'gm'
),
inside: {
lf: /\n/gm
}
};
md.hr = {
2014-03-16 02:13:42 +00:00
pattern: /^([*\-_] *){3,}$/gm,
};
md.li = {
pattern: /^[ \t]*([*+\-]|\d+\.)[ \t].+$/gm,
inside: {
2014-03-19 22:10:59 +00:00
"md md-li": /^ ?([*+\-]|\d+\.)[ \t]/m,
"md md-li2": /^ ?(?: {2}|\t{1})([*+\-]|\d+\.)[ \t]/m,
"md md-li3": /^ ?(?: {4}|\t{2})([*+\-]|\d+\.)[ \t]/m,
"md md-li4": /^ ?(?: {6}|\t{3})([*+\-]|\d+\.)[ \t]/m,
"md md-li5": /^ ?(?: {8}|\t{4})([*+\-]|\d+\.)[ \t]/m,
"md md-li6": /^ ?(?: {10}|\t{5})([*+\-]|\d+\.)[ \t]/m,
"md md-li7": /^ ?(?: {12}|\t{6})([*+\-]|\d+\.)[ \t]/m,
"md md-li8": /^ ?(?: {14}|\t{7})([*+\-]|\d+\.)[ \t]/m,
"md md-li9": /^ ?(?: {16}|\t{8})([*+\-]|\d+\.)[ \t]/m,
"md md-li10": /^ ?(?: {18}|\t{9})([*+\-]|\d+\.)[ \t]/m,
"md md-li11": /^ ?(?: {20}|\t{10})([*+\-]|\d+\.)[ \t]/m
2014-03-16 02:13:42 +00:00
}
};
for (var i = 6; i >= 1; i--) {
md["h" + i] = {
2014-03-27 00:20:08 +00:00
pattern: new RegExp("^#{" + i + "}.+$", "gm"),
2014-03-16 02:13:42 +00:00
inside: {
"md md-hash": new RegExp("^#{" + i + "} ")
}
};
}
md.blockquote = {
2014-03-19 22:10:59 +00:00
pattern: /^ {0,3}> *[^\n]+$/gm,
2014-03-16 02:13:42 +00:00
inside: {
2014-03-21 00:36:28 +00:00
"md md-gt": /^ {0,3}> */,
"li": md.li
2014-03-16 02:13:42 +00:00
}
};
md['math block'] = {
pattern: /(\$\$|\\\\\[|\\\\\\\\\()[\s\S]*?(\$\$|\\\\\]|\\\\\\\\\))/g,
inside: {
"md md-bracket-start": /^(\$\$|\\\\\[|\\\\\\\\\()/,
"md md-bracket-end": /(\$\$|\\\\\]|\\\\\\\\\))/,
2014-04-05 00:54:06 +00:00
lf: /\n/gm,
rest: latex
2014-03-16 02:13:42 +00:00
}
};
md['latex block'] = {
pattern: /\\?\\begin\{[a-z]*\*?\}[\s\S]*?\\?\\end\{[a-z]*\*?\}/g,
inside: {
"keyword": /\\?\\(begin|end)/,
2014-04-05 00:54:06 +00:00
lf: /\n/gm,
rest: latex
2014-03-16 02:13:42 +00:00
}
};
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 = {
2014-03-17 02:01:46 +00:00
pattern: /.+/g,
2014-03-16 02:13:42 +00:00
inside: {
2014-03-17 02:01:46 +00:00
'md md-toc': /^\s*\[(toc|TOC)\]\s*$/g
2014-03-16 02:13:42 +00:00
}
};
2014-03-19 22:10:59 +00:00
md.lf = /^\n$/gm;
2014-03-16 02:13:42 +00:00
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.email = {
pattern: emailPattern
};
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": /\$$/,
2014-04-05 00:54:06 +00:00
rest: latex
2014-03-16 02:13:42 +00:00
}
};
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,
url: md.url,
email: md.email,
strong: md.strong,
em: md.em,
strike: md.strike,
2014-04-19 01:09:25 +00:00
conflict: //g,
2014-03-16 02:13:42 +00:00
comment: Prism.languages.markup.comment,
tag: Prism.languages.markup.tag,
entity: Prism.languages.markup.entity
};
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;
2014-05-19 18:07:51 +00:00
md.table.inside.rest = rest;
md["table alt"].inside.rest = rest;
2014-03-16 02:13:42 +00:00
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-03-16 02:13:42 +00:00
rest = {
code: md.code,
fn: md.fn,
link: md.link,
linkref: md.linkref,
2014-04-19 01:09:25 +00:00
conflict: //g,
2014-03-16 02:13:42 +00:00
};
md.strong.inside.rest = rest;
md.em.inside.rest = rest;
md.strike.inside.rest = rest;
2014-03-19 22:10:59 +00:00
2014-03-16 02:13:42 +00:00
var inside = {
code: md.code,
strong: md.strong,
em: md.em,
strike: md.strike,
2014-04-19 01:09:25 +00:00
conflict: //g,
2014-03-16 02:13:42 +00:00
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-03-19 22:10:59 +00:00
2014-03-16 02:13:42 +00:00
return md;
2014-03-19 22:10:59 +00:00
})();