Fixed url highlighting in editor

This commit is contained in:
benweet 2014-06-12 21:15:41 +01:00
parent a5719f45de
commit bef54c64b8

View File

@ -1,341 +1,345 @@
// Credit to https://editorially.com/ // Credit to https://editorially.com/
Prism.languages.md = (function () { Prism.languages.md = (function() {
var charInsideUrl = "[-A-Z0-9+&@#/%?=~_|[\\]()!:,.;]", var charInsideUrl = "[-A-Z0-9+&@#/%?=~_|[\\]()!:,.;]",
charEndingUrl = "[-A-Z0-9+&@#/%=~_|[\\])]"; charEndingUrl = "[-A-Z0-9+&@#/%=~_|[\\])]";
var urlPattern = new RegExp("(=\"|<)?\\b(https?|ftp)(://" + charInsideUrl + "*" + charEndingUrl + ")(?=$|\\W)", "gi"); var urlPattern = new RegExp("(https?|ftp)(://" + charInsideUrl + "*" + charEndingUrl + ")(?=$|\\W)", "gi");
var emailPattern = /(?:mailto:)?([-.\w]+\@[-a-z0-9]+(\.[-a-z0-9]+)*\.[a-z]+)/gi; var emailPattern = /(?:mailto:)?([-.\w]+\@[-a-z0-9]+(\.[-a-z0-9]+)*\.[a-z]+)/gi;
var latex = Prism.languages.latex; var latex = Prism.languages.latex;
var lf = /\n/gm; var lf = /\n/gm;
var md = {}; var md = {};
md['pre gfm'] = { md['pre gfm'] = {
pattern: /^`{3}.*\n(?:[\s\S]*?)\n`{3} *$/gm, pattern: /^`{3}.*\n(?:[\s\S]*?)\n`{3} *$/gm,
inside: { inside: {
"md md-pre": /`{3}/, "md md-pre": /`{3}/,
lf: lf lf: lf
} }
}; };
md['h1 alt'] = { md['h1 alt'] = {
pattern: /^(.+)[ \t]*\n=+[ \t]*$/gm, pattern: /^(.+)[ \t]*\n=+[ \t]*$/gm,
inside: { inside: {
} }
}; };
md['h2 alt'] = { md['h2 alt'] = {
pattern: /^(.+)[ \t]*\n-+[ \t]*$/gm, pattern: /^(.+)[ \t]*\n-+[ \t]*$/gm,
inside: { inside: {
} }
}; };
for (var i = 6; i >= 1; i--) { for(var i = 6; i >= 1; i--) {
md["h" + i] = { md["h" + i] = {
pattern: new RegExp("^#{" + i + "}.+$", "gm"), pattern: new RegExp("^#{" + i + "}.+$", "gm"),
inside: { inside: {
"md md-hash": new RegExp("^#{" + i + "}") "md md-hash": new RegExp("^#{" + i + "}")
} }
}; };
} }
md.li = { md.li = {
pattern: /^[ \t]*([*+\-]|\d+\.)[ \t].+(?:\n|[ \t].*\n)*/gm, pattern: /^[ \t]*([*+\-]|\d+\.)[ \t].+(?:\n|[ \t].*\n)*/gm,
inside: { inside: {
"md md-li": /^[ \t]*([*+\-]|\d+\.)[ \t]/m, "md md-li": /^[ \t]*([*+\-]|\d+\.)[ \t]/m,
'pre gfm': /^((?: {4}|\t)+)`{3}.*\n(?:[\s\S]*?)\n\1`{3} *$/gm, 'pre gfm': {
lf: lf pattern: /^((?: {4}|\t)+)`{3}.*\n(?:[\s\S]*?)\n\1`{3} *$/gm,
} inside: {
}; "md md-pre": /`{3}/,
md.pre = { lf: lf
pattern: /(^|(?:^|(?:^|\n)(?![ \t]*([*+\-]|\d+\.)[ \t]).*\n)\s*?\n)(\s*(?: {4}|\t).*(?:\n|$))+/g, }
lookbehind: true, },
inside: { lf: lf
} }
}; };
md.table = { md.pre = {
pattern: new RegExp( pattern: /(^|(?:^|(?:^|\n)(?![ \t]*([*+\-]|\d+\.)[ \t]).*\n)\s*?\n)(\s*(?: {4}|\t).*(?:\n|$))+/g,
[ lookbehind: true,
'^' , inside: {
'[ ]{0,3}' , // Allowed whitespace }
'[|]' , // Initial pipe };
'(.+)\\n' , // $1: Header Row md.table = {
pattern: new RegExp(
[
'^' ,
'[ ]{0,3}' , // Allowed whitespace
'[|]' , // Initial pipe
'(.+)\\n' , // $1: Header Row
'[ ]{0,3}' , // Allowed whitespace '[ ]{0,3}' , // Allowed whitespace
'[|]([ ]*[-:]+[-| :]*)\\n' , // $2: Separator '[|]([ ]*[-:]+[-| :]*)\\n' , // $2: Separator
'(' , // $3: Table Body '(' , // $3: Table Body
'(?:[ ]*[|].*\\n?)*' , // Table rows '(?:[ ]*[|].*\\n?)*' , // Table rows
')', ')',
'(?:\\n|$)' // Stop at final newline '(?:\\n|$)' // Stop at final newline
].join(''), ].join(''),
'gm' 'gm'
), ),
inside: { inside: {
lf: lf lf: lf
} }
}; };
md['table alt'] = { md['table alt'] = {
pattern: new RegExp( pattern: new RegExp(
[ [
'^' , '^' ,
'[ ]{0,3}' , // Allowed whitespace '[ ]{0,3}' , // Allowed whitespace
'(\\S.*[|].*)\\n' , // $1: Header Row '(\\S.*[|].*)\\n' , // $1: Header Row
'[ ]{0,3}' , // Allowed whitespace '[ ]{0,3}' , // Allowed whitespace
'([-:]+[ ]*[|][-| :]*)\\n' , // $2: Separator '([-:]+[ ]*[|][-| :]*)\\n' , // $2: Separator
'(' , // $3: Table Body '(' , // $3: Table Body
'(?:.*[|].*\\n?)*' , // Table rows '(?:.*[|].*\\n?)*' , // Table rows
')' , ')' ,
'(?:\\n|$)' // Stop at final newline '(?:\\n|$)' // Stop at final newline
].join(''), ].join(''),
'gm' 'gm'
), ),
inside: { inside: {
lf: lf lf: lf
} }
}; };
md.hr = { md.hr = {
pattern: /^([*\-_] *){3,}$/gm pattern: /^([*\-_] *){3,}$/gm
}; };
md.blockquote = { md.blockquote = {
pattern: /^ {0,3}> *[^\n]+$/gm, pattern: /^ {0,3}> *[^\n]+$/gm,
inside: { inside: {
"md md-gt": /^ {0,3}> */, "md md-gt": /^ {0,3}> */,
"li": md.li "li": md.li
} }
}; };
md['math block'] = { md['math block'] = {
pattern: /(\$\$|\\\\\[|\\\\\\\\\()[\s\S]*?(\$\$|\\\\\]|\\\\\\\\\))/g, pattern: /(\$\$|\\\\\[|\\\\\\\\\()[\s\S]*?(\$\$|\\\\\]|\\\\\\\\\))/g,
inside: { inside: {
"md md-bracket-start": /^(\$\$|\\\\\[|\\\\\\\\\()/, "md md-bracket-start": /^(\$\$|\\\\\[|\\\\\\\\\()/,
"md md-bracket-end": /(\$\$|\\\\\]|\\\\\\\\\))/, "md md-bracket-end": /(\$\$|\\\\\]|\\\\\\\\\))/,
lf: lf, lf: lf,
rest: latex rest: latex
} }
}; };
md['latex block'] = { md['latex block'] = {
pattern: /\\?\\begin\{[a-z]*\*?\}[\s\S]*?\\?\\end\{[a-z]*\*?\}/g, pattern: /\\?\\begin\{[a-z]*\*?\}[\s\S]*?\\?\\end\{[a-z]*\*?\}/g,
inside: { inside: {
"keyword": /\\?\\(begin|end)/, "keyword": /\\?\\(begin|end)/,
lf: lf, lf: lf,
rest: latex rest: latex
} }
}; };
md.fndef = { md.fndef = {
pattern: /^ {0,3}\[\^.*?\]:[ \t]+.*$/gm, pattern: /^ {0,3}\[\^.*?\]:[ \t]+.*$/gm,
inside: { inside: {
"ref-id": { "ref-id": {
pattern: /\[\^.*?\]/, pattern: /\[\^.*?\]/,
inside: { inside: {
"md md-bracket-start": /\[/, "md md-bracket-start": /\[/,
"md md-bracket-end": /\]/ "md md-bracket-end": /\]/
} }
} }
} }
}; };
md.linkdef = { md.linkdef = {
pattern: /^ {0,3}\[.*?\]:[ \t]+.*$/gm, pattern: /^ {0,3}\[.*?\]:[ \t]+.*$/gm,
inside: { inside: {
"link-id": { "link-id": {
pattern: /\[.*?\]/, pattern: /\[.*?\]/,
inside: { inside: {
"md md-bracket-start": /\[/, "md md-bracket-start": /\[/,
"md md-bracket-end": /\]/ "md md-bracket-end": /\]/
} }
}, },
url: urlPattern, url: urlPattern,
linktitle: /['\"\(][^\'\"\)]*['\"\)]/ linktitle: /['\"\(][^\'\"\)]*['\"\)]/
} }
}; };
md.p = { md.p = {
pattern: /.+/g, pattern: /.+/g,
inside: { inside: {
'md md-toc': /^\s*\[(toc|TOC)\]\s*$/g 'md md-toc': /^\s*\[(toc|TOC)\]\s*$/g
} }
}; };
md.lf = /^\n$/gm; md.lf = /^\n$/gm;
md.img = { md.img = {
pattern: /!\[[^\]]*\]\([^\)]+\)/g, pattern: /!\[[^\]]*\]\([^\)]+\)/g,
inside: { inside: {
"md md-bang": /^!/, "md md-bang": /^!/,
"md md-bracket-start": /\[/, "md md-bracket-start": /\[/,
"md md-alt": /[^\[]+(?=\])/, "md md-alt": /[^\[]+(?=\])/,
"md md-bracket-end": /\](?=\()/, "md md-bracket-end": /\](?=\()/,
"md img-parens": { "md img-parens": {
pattern: /\([^\)]+\)/, pattern: /\([^\)]+\)/,
inside: { inside: {
"md md-paren-start": /^\(/, "md md-paren-start": /^\(/,
"md md-title": /(['][^']*[']|["“][^"”]*["”])(?=\)$)/, "md md-title": /(['][^']*[']|["“][^"”]*["”])(?=\)$)/,
"md md-src": /[^\('" \t]+(?=[\)'" \t])/, "md md-src": /[^\('" \t]+(?=[\)'" \t])/,
"md md-paren-end": /\)$/ "md md-paren-end": /\)$/
} }
} }
} }
}; };
md.link = { md.link = {
pattern: /\[(?:(\\.)|[^\[\]])*\]\([^\(\)\s]+(\(\S*?\))??[^\(\)\s]*?(\s(['][^']*[']|["“][^"”]*["”]))?\)/gm, pattern: /\[(?:(\\.)|[^\[\]])*\]\([^\(\)\s]+(\(\S*?\))??[^\(\)\s]*?(\s(['][^']*[']|["“][^"”]*["”]))?\)/gm,
inside: { inside: {
"md md-bracket-start": { "md md-bracket-start": {
pattern: /(^|[^\\])\[/, pattern: /(^|[^\\])\[/,
lookbehind: true lookbehind: true
}, },
"md md-underlined-text": { "md md-underlined-text": {
pattern: /(?:(\\.)|[^\[\]])+(?=\])/ pattern: /(?:(\\.)|[^\[\]])+(?=\])/
}, },
"md md-bracket-end": /\]\s?\(/, "md md-bracket-end": /\]\s?\(/,
"md md-paren-end": /\)$/, "md md-paren-end": /\)$/,
"md md-href": /.*/ "md md-href": /.*/
} }
}; };
md.fn = { md.fn = {
pattern: /\[\^(.*?)\]/g, pattern: /\[\^(.*?)\]/g,
inside: { inside: {
"ref": { "ref": {
pattern: /^\[[^\[\]]+\] ?/, pattern: /^\[[^\[\]]+\] ?/,
inside: { inside: {
"md md-bracket-start": /\[/, "md md-bracket-start": /\[/,
"md md-ref": /^[^\[\]]+/, "md md-ref": /^[^\[\]]+/,
"md md-bracket-end": /\]/ "md md-bracket-end": /\]/
} }
} }
} }
}; };
md.imgref = { md.imgref = {
pattern: /!\[(.*?)\] ?\[(.*?)\]/g, pattern: /!\[(.*?)\] ?\[(.*?)\]/g,
inside: { inside: {
"md md-bang": /^!/, "md md-bang": /^!/,
"ref-end": { "ref-end": {
pattern: /\[[^\[\]]+\]$/, pattern: /\[[^\[\]]+\]$/,
inside: { inside: {
"md md-bracket-start": /\[/, "md md-bracket-start": /\[/,
"md md-href": /[^\[\]]+(?=]$)/, "md md-href": /[^\[\]]+(?=]$)/,
"md md-bracket-end": /\]/ "md md-bracket-end": /\]/
} }
}, },
"ref-start": { "ref-start": {
pattern: /^\[[^\[\]]+\] ?/, pattern: /^\[[^\[\]]+\] ?/,
inside: { inside: {
"md md-bracket-start": /\[/, "md md-bracket-start": /\[/,
"md md-alt": /^[^\[\]]+/, "md md-alt": /^[^\[\]]+/,
"md md-bracket-end": /\]/ "md md-bracket-end": /\]/
} }
} }
} }
}; };
md.linkref = { md.linkref = {
pattern: /\[(.*?)\] ?\[(.*?)\]/g, pattern: /\[(.*?)\] ?\[(.*?)\]/g,
inside: { inside: {
"ref-end": { "ref-end": {
pattern: /\[[^\[\]]+\]$/, pattern: /\[[^\[\]]+\]$/,
inside: { inside: {
"md md-bracket-start": /\[/, "md md-bracket-start": /\[/,
"md md-href": /[^\[\]]+(?=]$)/, "md md-href": /[^\[\]]+(?=]$)/,
"md md-bracket-end": /\]/ "md md-bracket-end": /\]/
} }
}, },
"ref-start": { "ref-start": {
pattern: /^\[[^\[\]]+\] ?/, pattern: /^\[[^\[\]]+\] ?/,
inside: { inside: {
"md md-bracket-start": /\[/, "md md-bracket-start": /\[/,
"md md-underlined-text": /^[^\[\]]+/, "md md-underlined-text": /^[^\[\]]+/,
"md md-bracket-end": /\]/ "md md-bracket-end": /\]/
} }
} }
} }
}; };
md.code = { md.code = {
pattern: /(^|[^\\])(`+)([^\r]*?[^`])\2(?!`)/g, pattern: /(^|[^\\])(`+)([^\r]*?[^`])\2(?!`)/g,
lookbehind: true, lookbehind: true,
inside: { inside: {
"md md-code": /`/ "md md-code": /`/
} }
}; };
md.math = { md.math = {
pattern: /\$.*?\$/g, pattern: /\$.*?\$/g,
inside: { inside: {
"md md-bracket-start": /^\$/, "md md-bracket-start": /^\$/,
"md md-bracket-end": /\$$/, "md md-bracket-end": /\$$/,
rest: latex rest: latex
} }
}; };
md.strong = { md.strong = {
pattern: /([_\*])\1((?!\1{2}).)*\1{2}/g, pattern: /([_\*])\1((?!\1{2}).)*\1{2}/g,
inside: { inside: {
"md md-strong": /([_\*])\1/g "md md-strong": /([_\*])\1/g
} }
}; };
md.em = { md.em = {
pattern: /(^|[^\\])(\*|_)(\S[^\2]*?)??[^\s\\]+?\2/g, pattern: /(^|[^\\])(\*|_)(\S[^\2]*?)??[^\s\\]+?\2/g,
lookbehind: true, lookbehind: true,
inside: { inside: {
"md md-em md-start": /^(\*|_)/, "md md-em md-start": /^(\*|_)/,
"md md-em md-close": /(\*|_)$/ "md md-em md-close": /(\*|_)$/
} }
}; };
md.strike = { md.strike = {
pattern: /(^|\n|\W)(~~)(?=\S)([^\r]*?\S)\2/gm, pattern: /(^|\n|\W)(~~)(?=\S)([^\r]*?\S)\2/gm,
lookbehind: true, lookbehind: true,
inside: { inside: {
"md md-s": /(~~)/, "md md-s": /(~~)/,
"md-strike-text": /[^~]+/ "md-strike-text": /[^~]+/
} }
}; };
var rest = { var rest = {
code: md.code, code: md.code,
math: md.math, math: md.math,
fn: md.fn, fn: md.fn,
img: md.img, img: md.img,
link: md.link, link: md.link,
imgref: md.imgref, imgref: md.imgref,
linkref: md.linkref, linkref: md.linkref,
url: md.url, strong: md.strong,
email: md.email, em: md.em,
strong: md.strong, strike: md.strike,
em: md.em, conflict: //g,
strike: md.strike, comment: Prism.languages.markup.comment,
conflict: //g, tag: Prism.languages.markup.tag,
comment: Prism.languages.markup.comment, entity: Prism.languages.markup.entity,
tag: Prism.languages.markup.tag, url: urlPattern,
entity: Prism.languages.markup.entity, email: emailPattern
url: urlPattern, };
email: emailPattern
};
for (var c = 6; c >= 1; c--) { for(var c = 6; c >= 1; c--) {
md["h" + c].inside.rest = rest; md["h" + c].inside.rest = rest;
} }
md["h1 alt"].inside.rest = rest; md["h1 alt"].inside.rest = rest;
md["h2 alt"].inside.rest = rest; md["h2 alt"].inside.rest = rest;
md.table.inside.rest = rest; md.table.inside.rest = rest;
md["table alt"].inside.rest = rest; md["table alt"].inside.rest = rest;
md.p.inside.rest = rest; md.p.inside.rest = rest;
md.blockquote.inside.rest = rest; md.blockquote.inside.rest = rest;
md.li.inside.rest = rest; md.li.inside.rest = rest;
md.fndef.inside.rest = rest; md.fndef.inside.rest = rest;
rest = { rest = {
code: md.code, code: md.code,
fn: md.fn, fn: md.fn,
link: md.link, link: md.link,
linkref: md.linkref, linkref: md.linkref,
conflict: //g, conflict: //g,
}; };
md.strong.inside.rest = rest; md.strong.inside.rest = rest;
md.em.inside.rest = rest; md.em.inside.rest = rest;
md.strike.inside.rest = rest; md.strike.inside.rest = rest;
var inside = { var inside = {
code: md.code, code: md.code,
strong: md.strong, strong: md.strong,
em: md.em, em: md.em,
strike: md.strike, strike: md.strike,
conflict: //g, conflict: //g,
comment: Prism.languages.markup.comment, comment: Prism.languages.markup.comment,
tag: Prism.languages.markup.tag, tag: Prism.languages.markup.tag,
entity: Prism.languages.markup.entity entity: Prism.languages.markup.entity
}; };
md.link.inside["md md-underlined-text"].inside = inside; md.link.inside["md md-underlined-text"].inside = inside;
md.linkref.inside["ref-start"].inside["md md-underlined-text"].inside = inside; md.linkref.inside["ref-start"].inside["md md-underlined-text"].inside = inside;
return md; return md;
})(); })();