|
1 | 1 | (function(mod) {
|
2 | 2 | if (typeof exports == "object" && typeof module == "object") // CommonJS
|
3 |
| - mod(require("../codemirror/lib/codemirror"), require("../codemirror/mode/gfm/gfm")) |
| 3 | + mod(require("../codemirror/lib/codemirror"), require("../codemirror/mode/gfm/gfm"), require("../codemirror/mode/yaml-frontmatter/yaml-frontmatter")) |
4 | 4 | else if (typeof define == "function" && define.amd) // AMD
|
5 |
| - define(["../codemirror/lib/codemirror", "../codemirror/mode/gfm/gfm"], mod) |
| 5 | + define(["../codemirror/lib/codemirror", "../codemirror/mode/gfm/gfm", "../codemirror/mode/yaml-frontmatter/yaml-frontmatter"], mod) |
6 | 6 | else // Plain browser env
|
7 | 7 | mod(CodeMirror)
|
8 | 8 | })(function(CodeMirror) {
|
9 | 9 | 'use strict'
|
10 | 10 |
|
11 |
| - CodeMirror.defineMode('bfm', function(config, gfmConfig) { |
12 |
| - const bfmOverlay = { |
13 |
| - startState() { |
| 11 | + const fencedCodeRE = /^(~~~+|```+)[ \t]*([\w+#-]+)?(?:\(((?:\s*\w[-\w]*(?:=(?:'(?:.*?[^\\])?'|"(?:.*?[^\\])?"|(?:[^'"][^\s]*)))?)*)\))?(?::([^:]*)(?::(\d+))?)?\s*$/ |
| 12 | + |
| 13 | + function getMode(name, params, config, cm) { |
| 14 | + if (!name) { |
| 15 | + return null |
| 16 | + } |
| 17 | + |
| 18 | + const parameters = {} |
| 19 | + if (params) { |
| 20 | + const regex = /(\w[-\w]*)(?:=(?:'(.*?[^\\])?'|"(.*?[^\\])?"|([^'"][^\s]*)))?/g |
| 21 | + |
| 22 | + let match |
| 23 | + while ((match = regex.exec(params))) { |
| 24 | + parameters[match[1]] = match[2] || match[3] || match[4] || null |
| 25 | + } |
| 26 | + } |
| 27 | + |
| 28 | + if (name === 'chart') { |
| 29 | + name = parameters.hasOwnProperty('yaml') ? 'yaml' : 'json' |
| 30 | + } |
| 31 | + |
| 32 | + const found = CodeMirror.findModeByName(name) |
| 33 | + if (!found) { |
| 34 | + return null |
| 35 | + } |
| 36 | + |
| 37 | + if (CodeMirror.modes.hasOwnProperty(found.mode)) { |
| 38 | + const mode = CodeMirror.getMode(config, found.mode) |
| 39 | + |
| 40 | + return mode.name === 'null' ? null : mode |
| 41 | + } else { |
| 42 | + CodeMirror.requireMode(found.mode, () => { |
| 43 | + cm.setOption('mode', cm.getOption('mode')) |
| 44 | + }) |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + CodeMirror.defineMode('bfm', function (config, baseConfig) { |
| 49 | + baseConfig.name = 'yaml-frontmatter' |
| 50 | + const baseMode = CodeMirror.getMode(config, baseConfig) |
| 51 | + |
| 52 | + return { |
| 53 | + startState: function() { |
14 | 54 | return {
|
| 55 | + baseState: CodeMirror.startState(baseMode), |
| 56 | + |
| 57 | + basePos: 0, |
| 58 | + baseCur: null, |
| 59 | + overlayPos: 0, |
| 60 | + overlayCur: null, |
| 61 | + streamSeen: null, |
| 62 | + |
| 63 | + fencedEndRE: null, |
| 64 | + |
15 | 65 | inTable: false,
|
16 | 66 | rowIndex: 0
|
17 | 67 | }
|
18 | 68 | },
|
19 |
| - copyState(s) { |
| 69 | + copyState: function(s) { |
20 | 70 | return {
|
| 71 | + baseState: CodeMirror.copyState(baseMode, s.baseState), |
| 72 | + |
| 73 | + basePos: s.basePos, |
| 74 | + baseCur: null, |
| 75 | + overlayPos: s.overlayPos, |
| 76 | + overlayCur: null, |
| 77 | + |
| 78 | + fencedMode: s.fencedMode, |
| 79 | + fencedState: s.fencedMode ? CodeMirror.copyState(s.fencedMode, s.fencedState) : null, |
| 80 | + |
| 81 | + fencedEndRE: s.fencedEndRE, |
| 82 | + |
21 | 83 | inTable: s.inTable,
|
22 | 84 | rowIndex: s.rowIndex
|
23 | 85 | }
|
24 | 86 | },
|
25 |
| - token(stream, state) { |
| 87 | + token: function(stream, state) { |
| 88 | + const initialPos = stream.pos |
| 89 | + |
| 90 | + if (state.fencedEndRE && stream.match(state.fencedEndRE)) { |
| 91 | + state.fencedEndRE = null |
| 92 | + state.fencedMode = null |
| 93 | + state.fencedState = null |
| 94 | + |
| 95 | + stream.pos = initialPos |
| 96 | + } |
| 97 | + else { |
| 98 | + if (state.fencedMode) { |
| 99 | + return state.fencedMode.token(stream, state.fencedState) |
| 100 | + } |
| 101 | + |
| 102 | + const match = stream.match(fencedCodeRE, true) |
| 103 | + if (match) { |
| 104 | + state.fencedEndRE = new RegExp(match[1] + '+ *$') |
| 105 | + |
| 106 | + state.fencedMode = getMode(match[2], match[3], config, stream.lineOracle.doc.cm) |
| 107 | + if (state.fencedMode) { |
| 108 | + state.fencedState = CodeMirror.startState(state.fencedMode) |
| 109 | + } |
| 110 | + |
| 111 | + stream.pos = initialPos |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + if (stream != state.streamSeen || Math.min(state.basePos, state.overlayPos) < stream.start) { |
| 116 | + state.streamSeen = stream |
| 117 | + state.basePos = state.overlayPos = stream.start |
| 118 | + } |
| 119 | + |
| 120 | + if (stream.start == state.basePos) { |
| 121 | + state.baseCur = baseMode.token(stream, state.baseState) |
| 122 | + state.basePos = stream.pos |
| 123 | + } |
| 124 | + if (stream.start == state.overlayPos) { |
| 125 | + stream.pos = stream.start |
| 126 | + state.overlayCur = this.overlayToken(stream, state) |
| 127 | + state.overlayPos = stream.pos |
| 128 | + } |
| 129 | + stream.pos = Math.min(state.basePos, state.overlayPos) |
| 130 | + |
| 131 | + if (state.overlayCur == null) { |
| 132 | + return state.baseCur |
| 133 | + } |
| 134 | + else if (state.baseCur != null && state.combineTokens) { |
| 135 | + return state.baseCur + ' ' + state.overlayCur |
| 136 | + } |
| 137 | + else { |
| 138 | + return state.overlayCur |
| 139 | + } |
| 140 | + }, |
| 141 | + overlayToken: function(stream, state) { |
| 142 | + state.combineTokens = false |
| 143 | + |
| 144 | + if (state.fencedEndRE && stream.match(state.fencedEndRE)) { |
| 145 | + state.fencedEndRE = null |
| 146 | + state.localMode = null |
| 147 | + state.localState = null |
| 148 | + |
| 149 | + return null |
| 150 | + } |
| 151 | + |
| 152 | + if (state.localMode) { |
| 153 | + return state.localMode.token(stream, state.localState) || '' |
| 154 | + } |
| 155 | + |
| 156 | + const match = stream.match(fencedCodeRE, true) |
| 157 | + if (match) { |
| 158 | + state.fencedEndRE = new RegExp(match[1] + '+ *$') |
| 159 | + |
| 160 | + state.localMode = getMode(match[2], match[3], config, stream.lineOracle.doc.cm) |
| 161 | + if (state.localMode) { |
| 162 | + state.localState = CodeMirror.startState(state.localMode) |
| 163 | + } |
| 164 | + |
| 165 | + return null |
| 166 | + } |
| 167 | + |
26 | 168 | state.combineTokens = true
|
27 | 169 |
|
28 | 170 | if (state.inTable) {
|
|
55 | 197 | stream.skipToEnd()
|
56 | 198 | return null
|
57 | 199 | },
|
58 |
| - blankLine(state) { |
| 200 | + electricChars: baseMode.electricChars, |
| 201 | + innerMode: function(state) { |
| 202 | + if (state.fencedMode) { |
| 203 | + return { |
| 204 | + mode: state.fencedMode, |
| 205 | + state: state.fencedState |
| 206 | + } |
| 207 | + } else { |
| 208 | + return { |
| 209 | + mode: baseMode, |
| 210 | + state: state.baseState |
| 211 | + } |
| 212 | + } |
| 213 | + }, |
| 214 | + blankLine: function(state) { |
59 | 215 | state.inTable = false
|
| 216 | + |
| 217 | + if (state.fencedMode) { |
| 218 | + return state.fencedMode.blankLine && state.fencedMode.blankLine(state.fencedState) |
| 219 | + } else { |
| 220 | + return baseMode.blankLine(state.baseState) |
| 221 | + } |
60 | 222 | }
|
61 | 223 | }
|
62 |
| - |
63 |
| - gfmConfig.name = 'gfm' |
64 |
| - return CodeMirror.overlayMode(CodeMirror.getMode(config, gfmConfig), bfmOverlay) |
65 |
| - }) |
| 224 | + }, 'yaml-frontmatter') |
66 | 225 |
|
67 | 226 | CodeMirror.defineMIME('text/x-bfm', 'bfm')
|
68 | 227 |
|
|
0 commit comments