1
0
Fork 0
mirror of https://github.com/documize/community.git synced 2025-07-28 17:49:41 +02:00

upgraded libs and improved Markdown editing experience

This commit is contained in:
Harvey Kandola 2017-03-13 15:40:28 +00:00
parent 2e146cf767
commit ea1a8000ee
165 changed files with 15930 additions and 19013 deletions

View file

@ -13,8 +13,8 @@
CodeMirror.defineMode("markdown", function(cmCfg, modeCfg) {
var htmlFound = CodeMirror.modes.hasOwnProperty("xml");
var htmlMode = CodeMirror.getMode(cmCfg, htmlFound ? {name: "xml", htmlMode: true} : "text/plain");
var htmlMode = CodeMirror.getMode(cmCfg, "text/html");
var htmlModeMissing = htmlMode.name == "null"
function getMode(name) {
if (CodeMirror.findModeByName) {
@ -55,8 +55,6 @@ CodeMirror.defineMode("markdown", function(cmCfg, modeCfg) {
if (modeCfg.tokenTypeOverrides === undefined)
modeCfg.tokenTypeOverrides = {};
var codeDepth = 0;
var tokenTypes = {
header: "header",
code: "comment",
@ -65,7 +63,9 @@ CodeMirror.defineMode("markdown", function(cmCfg, modeCfg) {
list2: "variable-3",
list3: "keyword",
hr: "hr",
image: "tag",
image: "image",
imageAltText: "image-alt-text",
imageMarker: "image-marker",
formatting: "formatting",
linkInline: "link",
linkEmail: "link",
@ -83,14 +83,13 @@ CodeMirror.defineMode("markdown", function(cmCfg, modeCfg) {
}
var hrRE = /^([*\-_])(?:\s*\1){2,}\s*$/
, ulRE = /^[*\-+]\s+/
, olRE = /^[0-9]+([.)])\s+/
, taskListRE = /^\[(x| )\](?=\s)/ // Must follow ulRE or olRE
, listRE = /^(?:[*\-+]|^[0-9]+([.)]))\s+/
, taskListRE = /^\[(x| )\](?=\s)/ // Must follow listRE
, atxHeaderRE = modeCfg.allowAtxHeaderWithoutSpace ? /^(#+)/ : /^(#+)(?: |$)/
, setextHeaderRE = /^ *(?:\={1,}|-{1,})\s*$/
, textRE = /^[^#!\[\]*_\\<>` "'(~]+/
, fencedCodeRE = new RegExp("^(" + (modeCfg.fencedCodeBlocks === true ? "~~~+|```+" : modeCfg.fencedCodeBlocks) +
")[ \\t]*([\\w+#]*)");
")[ \\t]*([\\w+#\-]*)");
function switchInline(stream, state, f) {
state.f = state.inline = f;
@ -121,7 +120,7 @@ CodeMirror.defineMode("markdown", function(cmCfg, modeCfg) {
state.quote = 0;
// Reset state.indentedCode
state.indentedCode = false;
if (!htmlFound && state.f == htmlBlock) {
if (htmlModeMissing && state.f == htmlBlock) {
state.f = inlineNormal;
state.block = blockNormal;
}
@ -151,10 +150,8 @@ CodeMirror.defineMode("markdown", function(cmCfg, modeCfg) {
state.list = null;
} else if (state.indentation > 0) {
state.list = null;
state.listDepth = Math.floor(state.indentation / 4);
} else { // No longer a list
state.list = false;
state.listDepth = 0;
}
}
@ -191,17 +188,21 @@ CodeMirror.defineMode("markdown", function(cmCfg, modeCfg) {
} else if (stream.match(hrRE, true)) {
state.hr = true;
return tokenTypes.hr;
} else if ((lineIsEmpty(state.prevLine) || prevLineIsList) && (stream.match(ulRE, false) || stream.match(olRE, false))) {
var listType = null;
if (stream.match(ulRE, true)) {
listType = 'ul';
} else {
stream.match(olRE, true);
listType = 'ol';
}
} else if (match = stream.match(listRE)) {
var listType = match[1] ? "ol" : "ul";
state.indentation = stream.column() + stream.current().length;
state.list = true;
state.listDepth++;
// While this list item's marker's indentation
// is less than the deepest list item's content's indentation,
// pop the deepest list item indentation off the stack.
while (state.listStack && stream.column() < state.listStack[state.listStack.length - 1]) {
state.listStack.pop();
}
// Add this list item's content's indentation to the stack
state.listStack.push(state.indentation);
if (modeCfg.taskLists && stream.match(taskListRE, false)) {
state.taskList = true;
}
@ -212,10 +213,10 @@ CodeMirror.defineMode("markdown", function(cmCfg, modeCfg) {
state.fencedChars = match[1]
// try switching mode
state.localMode = getMode(match[2]);
if (state.localMode) state.localState = state.localMode.startState();
if (state.localMode) state.localState = CodeMirror.startState(state.localMode);
state.f = state.block = local;
if (modeCfg.highlightFormatting) state.formatting = "code-block";
state.code = true;
state.code = -1
return getType(state);
}
@ -224,21 +225,27 @@ CodeMirror.defineMode("markdown", function(cmCfg, modeCfg) {
function htmlBlock(stream, state) {
var style = htmlMode.token(stream, state.htmlState);
if ((htmlFound && state.htmlState.tagStart === null &&
(!state.htmlState.context && state.htmlState.tokenize.isInText)) ||
(state.md_inside && stream.current().indexOf(">") > -1)) {
state.f = inlineNormal;
state.block = blockNormal;
state.htmlState = null;
if (!htmlModeMissing) {
var inner = CodeMirror.innerMode(htmlMode, state.htmlState)
if ((inner.mode.name == "xml" && inner.state.tagStart === null &&
(!inner.state.context && inner.state.tokenize.isInText)) ||
(state.md_inside && stream.current().indexOf(">") > -1)) {
state.f = inlineNormal;
state.block = blockNormal;
state.htmlState = null;
}
}
return style;
}
function local(stream, state) {
if (stream.sol() && state.fencedChars && stream.match(state.fencedChars, false)) {
if (state.fencedChars && stream.match(state.fencedChars)) {
if (modeCfg.highlightFormatting) state.formatting = "code-block";
state.localMode = state.localState = null;
state.f = state.block = leavingLocal;
return null;
return getType(state)
} else if (state.fencedChars && stream.skipTo(state.fencedChars)) {
return "comment"
} else if (state.localMode) {
return state.localMode.token(stream, state.localState);
} else {
@ -253,9 +260,9 @@ CodeMirror.defineMode("markdown", function(cmCfg, modeCfg) {
state.f = inlineNormal;
state.fencedChars = null;
if (modeCfg.highlightFormatting) state.formatting = "code-block";
state.code = true;
state.code = 1
var returnType = getType(state);
state.code = false;
state.code = 0
return returnType;
}
@ -304,6 +311,9 @@ CodeMirror.defineMode("markdown", function(cmCfg, modeCfg) {
if (state.strikethrough) { styles.push(tokenTypes.strikethrough); }
if (state.linkText) { styles.push(tokenTypes.linkText); }
if (state.code) { styles.push(tokenTypes.code); }
if (state.image) { styles.push(tokenTypes.image); }
if (state.imageAltText) { styles.push(tokenTypes.imageAltText, "link"); }
if (state.imageMarker) { styles.push(tokenTypes.imageMarker); }
}
if (state.header) { styles.push(tokenTypes.header, tokenTypes.header + "-" + state.header); }
@ -320,7 +330,7 @@ CodeMirror.defineMode("markdown", function(cmCfg, modeCfg) {
}
if (state.list !== false) {
var listMod = (state.listDepth - 1) % 3;
var listMod = (state.listStack.length - 1) % 3;
if (!listMod) {
styles.push(tokenTypes.list1);
} else if (listMod === 1) {
@ -378,15 +388,6 @@ CodeMirror.defineMode("markdown", function(cmCfg, modeCfg) {
var ch = stream.next();
if (ch === '\\') {
stream.next();
if (modeCfg.highlightFormatting) {
var type = getType(state);
var formattingEscape = tokenTypes.formatting + "-escape";
return type ? type + " " + formattingEscape : formattingEscape;
}
}
// Matches link titles present on next line
if (state.linkTitle) {
state.linkTitle = false;
@ -405,39 +406,62 @@ CodeMirror.defineMode("markdown", function(cmCfg, modeCfg) {
if (ch === '`') {
var previousFormatting = state.formatting;
if (modeCfg.highlightFormatting) state.formatting = "code";
var t = getType(state);
var before = stream.pos;
stream.eatWhile('`');
var difference = 1 + stream.pos - before;
if (!state.code) {
codeDepth = difference;
state.code = true;
return getType(state);
var count = stream.current().length
if (state.code == 0) {
state.code = count
return getType(state)
} else if (count == state.code) { // Must be exact
var t = getType(state)
state.code = 0
return t
} else {
if (difference === codeDepth) { // Must be exact
state.code = false;
return t;
}
state.formatting = previousFormatting;
return getType(state);
state.formatting = previousFormatting
return getType(state)
}
} else if (state.code) {
return getType(state);
}
if (ch === '!' && stream.match(/\[[^\]]*\] ?(?:\(|\[)/, false)) {
stream.match(/\[[^\]]*\]/);
state.inline = state.f = linkHref;
return tokenTypes.image;
if (ch === '\\') {
stream.next();
if (modeCfg.highlightFormatting) {
var type = getType(state);
var formattingEscape = tokenTypes.formatting + "-escape";
return type ? type + " " + formattingEscape : formattingEscape;
}
}
if (ch === '[' && stream.match(/.*\](\(.*\)| ?\[.*\])/, false)) {
if (ch === '!' && stream.match(/\[[^\]]*\] ?(?:\(|\[)/, false)) {
state.imageMarker = true;
state.image = true;
if (modeCfg.highlightFormatting) state.formatting = "image";
return getType(state);
}
if (ch === '[' && state.imageMarker && stream.match(/[^\]]*\](\(.*?\)| ?\[.*?\])/, false)) {
state.imageMarker = false;
state.imageAltText = true
if (modeCfg.highlightFormatting) state.formatting = "image";
return getType(state);
}
if (ch === ']' && state.imageAltText) {
if (modeCfg.highlightFormatting) state.formatting = "image";
var type = getType(state);
state.imageAltText = false;
state.image = false;
state.inline = state.f = linkHref;
return type;
}
if (ch === '[' && stream.match(/[^\]]*\](\(.*\)| ?\[.*?\])/, false) && !state.image) {
state.linkText = true;
if (modeCfg.highlightFormatting) state.formatting = "link";
return getType(state);
}
if (ch === ']' && state.linkText && stream.match(/\(.*\)| ?\[.*\]/, false)) {
if (ch === ']' && state.linkText && stream.match(/\(.*?\)| ?\[.*?\]/, false)) {
if (modeCfg.highlightFormatting) state.formatting = "link";
var type = getType(state);
state.linkText = false;
@ -469,7 +493,7 @@ CodeMirror.defineMode("markdown", function(cmCfg, modeCfg) {
return type + tokenTypes.linkEmail;
}
if (ch === '<' && stream.match(/^(!--|\w)/, false)) {
if (ch === '<' && stream.match(/^(!--|[a-z]+(?:\s+[a-z_:.\-]+(?:\s*=\s*[^ >]+)?)*\s*>)/i, false)) {
var end = stream.string.indexOf(">", stream.pos);
if (end != -1) {
var atts = stream.string.substring(stream.start, end);
@ -590,7 +614,7 @@ CodeMirror.defineMode("markdown", function(cmCfg, modeCfg) {
}
var ch = stream.next();
if (ch === '(' || ch === '[') {
state.f = state.inline = getLinkHrefInside(ch === "(" ? ")" : "]");
state.f = state.inline = getLinkHrefInside(ch === "(" ? ")" : "]", 0);
if (modeCfg.highlightFormatting) state.formatting = "link-string";
state.linkHref = true;
return getType(state);
@ -598,6 +622,11 @@ CodeMirror.defineMode("markdown", function(cmCfg, modeCfg) {
return 'error';
}
var linkRE = {
")": /^(?:[^\\\(\)]|\\.|\((?:[^\\\(\)]|\\.)*\))*?(?=\))/,
"]": /^(?:[^\\\[\]]|\\.|\[(?:[^\\\[\\]]|\\.)*\])*?(?=\])/
}
function getLinkHrefInside(endChar) {
return function(stream, state) {
var ch = stream.next();
@ -610,10 +639,7 @@ CodeMirror.defineMode("markdown", function(cmCfg, modeCfg) {
return returnState;
}
if (stream.match(inlineRE(endChar), true)) {
stream.backUp(1);
}
stream.match(linkRE[endChar])
state.linkHref = true;
return getType(state);
};
@ -661,18 +687,6 @@ CodeMirror.defineMode("markdown", function(cmCfg, modeCfg) {
return tokenTypes.linkHref + " url";
}
var savedInlineRE = [];
function inlineRE(endChar) {
if (!savedInlineRE[endChar]) {
// Escape endChar for RegExp (taken from http://stackoverflow.com/a/494122/526741)
endChar = (endChar+'').replace(/([.?*+^$[\]\\(){}|-])/g, "\\$1");
// Match any non-endChar, escaped character, as well as the closing
// endChar.
savedInlineRE[endChar] = new RegExp('^(?:[^\\\\]|\\\\.)*?(' + endChar + ')');
}
return savedInlineRE[endChar];
}
var mode = {
startState: function() {
return {
@ -692,13 +706,14 @@ CodeMirror.defineMode("markdown", function(cmCfg, modeCfg) {
linkText: false,
linkHref: false,
linkTitle: false,
code: 0,
em: false,
strong: false,
header: 0,
hr: false,
taskList: false,
list: false,
listDepth: 0,
listStack: [],
quote: 0,
trailingSpace: 0,
trailingSpaceNewLine: false,
@ -733,7 +748,7 @@ CodeMirror.defineMode("markdown", function(cmCfg, modeCfg) {
hr: s.hr,
taskList: s.taskList,
list: s.list,
listDepth: s.listDepth,
listStack: s.listStack.slice(0),
quote: s.quote,
indentedCode: s.indentedCode,
trailingSpace: s.trailingSpace,
@ -773,11 +788,8 @@ CodeMirror.defineMode("markdown", function(cmCfg, modeCfg) {
state.f = state.block;
var indentation = stream.match(/^\s*/, true)[0].replace(/\t/g, ' ').length;
var difference = Math.floor((indentation - state.indentation) / 4) * 4;
if (difference > 4) difference = 4;
var adjustedIndentation = state.indentation + difference;
state.indentationDiff = adjustedIndentation - state.indentation;
state.indentation = adjustedIndentation;
state.indentationDiff = Math.min(indentation - state.indentation, 4);
state.indentation = state.indentation + state.indentationDiff;
if (indentation > 0) return null;
}
return state.f(stream, state);
@ -793,6 +805,7 @@ CodeMirror.defineMode("markdown", function(cmCfg, modeCfg) {
getType: getType,
closeBrackets: "()[]{}''\"\"``",
fold: "markdown"
};
return mode;