From cf6cc381f8764d7988d2df048743ba0d84fe286c Mon Sep 17 00:00:00 2001 From: Marijn Haverbeke Date: Tue, 20 Jul 2021 15:56:37 +0200 Subject: [PATCH] [yaml-frontmatter mode] Treat the start of the document as being in the base mode For purposes of indentation and such. Issue https://github.com/codemirror/CodeMirror/issues/6737 --- mode/yaml-frontmatter/yaml-frontmatter.js | 30 +++++++++++------------ 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/mode/yaml-frontmatter/yaml-frontmatter.js b/mode/yaml-frontmatter/yaml-frontmatter.js index 88c845e222..5c6175e4ec 100644 --- a/mode/yaml-frontmatter/yaml-frontmatter.js +++ b/mode/yaml-frontmatter/yaml-frontmatter.js @@ -17,55 +17,55 @@ var yamlMode = CodeMirror.getMode(config, "yaml") var innerMode = CodeMirror.getMode(config, parserConfig && parserConfig.base || "gfm") - function curMode(state) { - return state.state == BODY ? innerMode : yamlMode + function localMode(state) { + return state.state == FRONTMATTER ? {mode: yamlMode, state: state.yaml} : {mode: innerMode, state: state.inner} } return { startState: function () { return { state: START, - inner: CodeMirror.startState(yamlMode) + yaml: null, + inner: CodeMirror.startState(innerMode) } }, copyState: function (state) { return { state: state.state, - inner: CodeMirror.copyState(curMode(state), state.inner) + yaml: state.yaml && CodeMirror.copyState(yamlMode, state.yaml), + inner: CodeMirror.copyState(innerMode, state.inner) } }, token: function (stream, state) { if (state.state == START) { if (stream.match('---', false)) { state.state = FRONTMATTER - return yamlMode.token(stream, state.inner) + state.yaml = CodeMirror.startState(yamlMode) + return yamlMode.token(stream, state.yaml) } else { state.state = BODY - state.inner = CodeMirror.startState(innerMode) return innerMode.token(stream, state.inner) } } else if (state.state == FRONTMATTER) { var end = stream.sol() && stream.match(/(---|\.\.\.)/, false) - var style = yamlMode.token(stream, state.inner) + var style = yamlMode.token(stream, state.yaml) if (end) { state.state = BODY - state.inner = CodeMirror.startState(innerMode) + state.yaml = null } return style } else { return innerMode.token(stream, state.inner) } }, - innerMode: function (state) { - return {mode: curMode(state), state: state.inner} - }, + innerMode: localMode, indent: function(state, a, b) { - var mode = curMode(state) - return mode.indent ? mode.indent(state.inner, a, b) : CodeMirror.Pass + var m = localMode(state) + return m.mode.indent ? m.mode.indent(m.state, a, b) : CodeMirror.Pass }, blankLine: function (state) { - var mode = curMode(state) - if (mode.blankLine) return mode.blankLine(state.inner) + var m = localMode(state) + if (m.mode.blankLine) return m.mode.blankLine(m.state) } } })