Skip to content

Commit

Permalink
[yaml-frontmatter mode] Repurpose yaml-markdown mode to a general yam…
Browse files Browse the repository at this point in the history
…l-frontmatter mode

Issue codemirror#3722
  • Loading branch information
marijnh committed Dec 20, 2015
1 parent a4e9870 commit d6212b9
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 69 deletions.
1 change: 1 addition & 0 deletions doc/compress.html
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@ <h2>Script compression helper</h2>
<option value="http://codemirror.net/mode/xml/xml.js">xml.js</option>
<option value="http://codemirror.net/mode/xquery/xquery.js">xquery.js</option>
<option value="http://codemirror.net/mode/yaml/yaml.js">yaml.js</option>
<option value="http://codemirror.net/mode/yaml-frontmatter/yaml-frontmatter.js">yaml-frontmatter.js</option>
<option value="http://codemirror.net/mode/z80/z80.js">z80.js</option>
</optgroup>
<optgroup label="Add-ons">
Expand Down
1 change: 1 addition & 0 deletions mode/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ <h2>Language modes</h2>
<li><a href="xml/index.html">XML/HTML</a></li>
<li><a href="xquery/index.html">XQuery</a></li>
<li><a href="yaml/index.html">YAML</a></li>
<li><a href="yaml-frontmatter/index.html">YAML frontmatter</a></li>
<li><a href="z80/index.html">Z80</a></li>
</ul>
</div>
Expand Down
17 changes: 12 additions & 5 deletions mode/yaml-markdown/index.html → mode/yaml-frontmatter/index.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<!doctype html>

<title>CodeMirror: GitHub Flavored Markdown with YAML front matter mode</title>
<title>CodeMirror: YAML front matter mode</title>
<meta charset="utf-8"/>
<link rel=stylesheet href="../../doc/docs.css">

Expand All @@ -10,7 +10,7 @@
<script src="../markdown/markdown.js"></script>
<script src="../gfm/gfm.js"></script>
<script src="../yaml/yaml.js"></script>
<script src="yaml-markdown.js"></script>
<script src="yaml-frontmatter.js"></script>
<style>.CodeMirror { border-top: 1px solid #ddd; border-bottom: 1px solid #ddd; }</style>
<div id=nav>
<a href="http://codemirror.net"><h1>CodeMirror</h1><img id=logo src="../../doc/logo.png"></a>
Expand All @@ -22,12 +22,12 @@
</ul>
<ul>
<li><a href="../index.html">Language modes</a>
<li><a class=active href="#">YAML-Markdown</a>
<li><a class=active href="#">YAML-Frontmatter</a>
</ul>
</div>

<article>
<h2>GitHub Flavored Markdown with YAML front matter mode</h2>
<h2>YAML front matter mode</h2>
<form><textarea id="code" name="code">
---
receipt: Oz-Ware Purchase Invoice
Expand Down Expand Up @@ -107,8 +107,15 @@ <h2>GitHub Flavored Markdown with YAML front matter mode</h2>

See http://github.github.com/github-flavored-markdown/.
</textarea></form>

<p>Defines a mode that parses
a <a href="http://jekyllrb.com/docs/frontmatter/">YAML frontmatter</a>
at the start of a file, switching to a base mode at the end of that.
Takes a mode configuration option <code>base</code> to configure the
base mode, which defaults to <code>"gfm"</code>.</p>

<script>
var editor = CodeMirror.fromTextArea(document.getElementById("code"), {mode: 'yaml-markdown'});
var editor = CodeMirror.fromTextArea(document.getElementById("code"), {mode: "yaml-frontmatter"});
</script>

</article>
68 changes: 68 additions & 0 deletions mode/yaml-frontmatter/yaml-frontmatter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// CodeMirror, copyright (c) by Marijn Haverbeke and others
// Distributed under an MIT license: http://codemirror.net/LICENSE

(function (mod) {
if (typeof exports == "object" && typeof module == "object") // CommonJS
mod(require("../../lib/codemirror"), require("../yaml/yaml"))
else if (typeof define == "function" && define.amd) // AMD
define(["../../lib/codemirror", "../yaml/yaml"], mod)
else // Plain browser env
mod(CodeMirror)
})(function (CodeMirror) {

var START = 0, FRONTMATTER = 1, BODY = 2

// a mixed mode for Markdown text with an optional YAML front matter
CodeMirror.defineMode("yaml-frontmatter", function (config, parserConfig) {
var yamlMode = CodeMirror.getMode(config, "yaml")
var innerMode = CodeMirror.getMode(config, parserConfig && parserConfig.base || "gfm")

function curMode(state) {
return state.state == BODY ? innerMode : yamlMode
}

return {
startState: function () {
return {
state: START,
inner: CodeMirror.startState(yamlMode)
}
},
copyState: function (state) {
return {
state: state.state,
inner: CodeMirror.copyState(curMode(state), state.inner)
}
},
token: function (stream, state) {
if (state.state == START) {
if (stream.match(/---/, false)) {
state.state = FRONTMATTER
return yamlMode.token(stream, state.inner)
} else {
stream.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)
if (end) {
state.state = BODY
state.inner = CodeMirror.startState(innerMode)
}
return style
} else {
return innerMode.token(stream, state.inner)
}
},
innerMode: function (state) {
return {mode: curMode(state), state: state.inner}
},
blankLine: function (state) {
var mode = curMode(state)
if (mode.blankLine) return mode.blankLine(state.inner)
}
}
})
})
64 changes: 0 additions & 64 deletions mode/yaml-markdown/yaml-markdown.js

This file was deleted.

0 comments on commit d6212b9

Please sign in to comment.