Path: blob/master/web-gui/buildyourownbotnet/assets/js/codemirror/mode/gfm/gfm.js
1293 views
CodeMirror.defineMode("gfm", function(config, modeConfig) {1var codeDepth = 0;2function blankLine(state) {3state.code = false;4return null;5}6var gfmOverlay = {7startState: function() {8return {9code: false,10codeBlock: false,11ateSpace: false12};13},14copyState: function(s) {15return {16code: s.code,17codeBlock: s.codeBlock,18ateSpace: s.ateSpace19};20},21token: function(stream, state) {22// Hack to prevent formatting override inside code blocks (block and inline)23if (state.codeBlock) {24if (stream.match(/^```/)) {25state.codeBlock = false;26return null;27}28stream.skipToEnd();29return null;30}31if (stream.sol()) {32state.code = false;33}34if (stream.sol() && stream.match(/^```/)) {35stream.skipToEnd();36state.codeBlock = true;37return null;38}39// If this block is changed, it may need to be updated in Markdown mode40if (stream.peek() === '`') {41stream.next();42var before = stream.pos;43stream.eatWhile('`');44var difference = 1 + stream.pos - before;45if (!state.code) {46codeDepth = difference;47state.code = true;48} else {49if (difference === codeDepth) { // Must be exact50state.code = false;51}52}53return null;54} else if (state.code) {55stream.next();56return null;57}58// Check if space. If so, links can be formatted later on59if (stream.eatSpace()) {60state.ateSpace = true;61return null;62}63if (stream.sol() || state.ateSpace) {64state.ateSpace = false;65if(stream.match(/^(?:[a-zA-Z0-9\-_]+\/)?(?:[a-zA-Z0-9\-_]+@)?(?:[a-f0-9]{7,40}\b)/)) {66// User/Project@SHA67// User@SHA68// SHA69return "link";70} else if (stream.match(/^(?:[a-zA-Z0-9\-_]+\/)?(?:[a-zA-Z0-9\-_]+)?#[0-9]+\b/)) {71// User/Project#Num72// User#Num73// #Num74return "link";75}76}77if (stream.match(/^((?:[a-z][\w-]+:(?:\/{1,3}|[a-z0-9%])|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}\/)(?:[^\s()<>]|\([^\s()<>]*\))+(?:\([^\s()<>]*\)|[^\s`!()\[\]{};:'".,<>?«»“”‘’]))/i) &&78stream.string.slice(stream.start - 2, stream.start) != "](") {79// URLs80// Taken from http://daringfireball.net/2010/07/improved_regex_for_matching_urls81// And then (issue #1160) simplified to make it not crash the Chrome Regexp engine82return "link";83}84stream.next();85return null;86},87blankLine: blankLine88};8990var markdownConfig = {91underscoresBreakWords: false,92taskLists: true,93fencedCodeBlocks: true94};95for (var attr in modeConfig) {96markdownConfig[attr] = modeConfig[attr];97}98markdownConfig.name = "markdown";99CodeMirror.defineMIME("gfmBase", markdownConfig);100return CodeMirror.overlayMode(CodeMirror.getMode(config, "gfmBase"), gfmOverlay);101}, "markdown");102103104