Created
September 22, 2012 14:00
-
-
Save cowboy/3766245 to your computer and use it in GitHub Desktop.
JavaScript, YAML: wrapper around js-yaml that allows literal/folded styles on documents, eg. --- | or --- > without all that "crazy" indenting.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* yaml-indent-documents | |
* | |
* Copyright (c) 2012 "Cowboy" Ben Alman | |
* Licensed under the MIT license. | |
* http://benalman.com/about/license/ | |
*/ | |
'use strict'; | |
var fs = require('fs'); | |
var yaml = module.exports = require('js-yaml'); | |
// Add one space of indentation to all documents starting with --- | or --- > | |
// because when writing TONS of content, indenting every line isn't practical. | |
// | |
// http://www.yaml.org/spec/1.2/spec.html#id2793652 | |
// https://github.com/nodeca/js-yaml/issues/53 | |
yaml.addIndents = function(src) { | |
return String(src).split(/^(\-{3})/gm).map(function(s) { | |
var m = s.match(/^(\s+[\|\^].*?\n)([\s\S]*?)\n$/); | |
return m ? m[1] + m[2].replace(/^/gm, ' ') + '\n' : s; | |
}).join(''); | |
}; | |
// This seems to do what I want it to. | |
yaml.parseDocs = function(src) { | |
src = yaml.addIndents(src); | |
var docs = []; | |
yaml.loadAll(src, function(doc) { | |
docs.push(doc); | |
}); | |
return docs; | |
}; | |
// So does this. | |
yaml.parseDocsFile = function(filepath) { | |
return yaml.parseDocs(fs.readFileSync(filepath)); | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- | |
a: | |
When parsed, these lines | |
SHOULD NOT retain linebreaks. | |
b: | | |
When parsed, these lines | |
SHOULD retain linebreaks | |
and trailing newline. | |
c: |+ | |
When parsed, these lines | |
SHOULD retain linebreaks | |
and all trailing whitespace. | |
d: |- | |
When parsed, these lines | |
SHOULD retain linebreaks | |
with no trailing whitespace. | |
# When writing TONS of content, indenting every line isn't practical. | |
--- | |
When parsed, these lines | |
SHOULD NOT retain linebreaks. | |
--- | | |
When parsed, these lines | |
SHOULD retain linebreaks | |
and trailing newline. | |
--- |+ | |
When parsed, these lines | |
SHOULD retain linebreaks | |
and all trailing whitespace. | |
--- |- | |
When parsed, these lines | |
SHOULD retain linebreaks | |
with no trailing whitespace. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[ | |
{ | |
"a": "When parsed, these lines SHOULD NOT retain linebreaks.", | |
"b": "When parsed, these lines\nSHOULD retain linebreaks\nand trailing newline.\n", | |
"c": "When parsed, these lines\nSHOULD retain linebreaks\nand all trailing whitespace.\n\n", | |
"d": "When parsed, these lines\nSHOULD retain linebreaks\nwith no trailing whitespace." | |
}, | |
"When parsed, these lines SHOULD NOT retain linebreaks.", | |
"When parsed, these lines\nSHOULD retain linebreaks\nand trailing newline.\n", | |
"When parsed, these lines\nSHOULD retain linebreaks\nand all trailing whitespace.\n\n", | |
"When parsed, these lines\nSHOULD retain linebreaks\nwith no trailing whitespace." | |
] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
As I just wrote in the issue, I believe it's a typo in the source documents, you can check my fork.