comparison .cms/lib/codemirror/mode/yaml-frontmatter/yaml-frontmatter.js @ 0:78edf6b517a0 draft

24.10
author Coffee CMS <info@coffee-cms.ru>
date Fri, 11 Oct 2024 22:40:23 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:78edf6b517a0
1 // CodeMirror, copyright (c) by Marijn Haverbeke and others
2 // Distributed under an MIT license: https://codemirror.net/5/LICENSE
3
4 (function (mod) {
5 if (typeof exports == "object" && typeof module == "object") // CommonJS
6 mod(require("../../lib/codemirror"), require("../yaml/yaml"))
7 else if (typeof define == "function" && define.amd) // AMD
8 define(["../../lib/codemirror", "../yaml/yaml"], mod)
9 else // Plain browser env
10 mod(CodeMirror)
11 })(function (CodeMirror) {
12
13 var START = 0, FRONTMATTER = 1, BODY = 2
14
15 // a mixed mode for Markdown text with an optional YAML front matter
16 CodeMirror.defineMode("yaml-frontmatter", function (config, parserConfig) {
17 var yamlMode = CodeMirror.getMode(config, "yaml")
18 var innerMode = CodeMirror.getMode(config, parserConfig && parserConfig.base || "gfm")
19
20 function localMode(state) {
21 return state.state == FRONTMATTER ? {mode: yamlMode, state: state.yaml} : {mode: innerMode, state: state.inner}
22 }
23
24 return {
25 startState: function () {
26 return {
27 state: START,
28 yaml: null,
29 inner: CodeMirror.startState(innerMode)
30 }
31 },
32 copyState: function (state) {
33 return {
34 state: state.state,
35 yaml: state.yaml && CodeMirror.copyState(yamlMode, state.yaml),
36 inner: CodeMirror.copyState(innerMode, state.inner)
37 }
38 },
39 token: function (stream, state) {
40 if (state.state == START) {
41 if (stream.match('---', false)) {
42 state.state = FRONTMATTER
43 state.yaml = CodeMirror.startState(yamlMode)
44 return yamlMode.token(stream, state.yaml)
45 } else {
46 state.state = BODY
47 return innerMode.token(stream, state.inner)
48 }
49 } else if (state.state == FRONTMATTER) {
50 var end = stream.sol() && stream.match(/(---|\.\.\.)/, false)
51 var style = yamlMode.token(stream, state.yaml)
52 if (end) {
53 state.state = BODY
54 state.yaml = null
55 }
56 return style
57 } else {
58 return innerMode.token(stream, state.inner)
59 }
60 },
61 innerMode: localMode,
62 indent: function(state, a, b) {
63 var m = localMode(state)
64 return m.mode.indent ? m.mode.indent(m.state, a, b) : CodeMirror.Pass
65 },
66 blankLine: function (state) {
67 var m = localMode(state)
68 if (m.mode.blankLine) return m.mode.blankLine(m.state)
69 }
70 }
71 })
72 });