Skip to content

Commit

Permalink
Add escape method
Browse files Browse the repository at this point in the history
  • Loading branch information
tommy351 committed Jul 3, 2014
1 parent d6be44a commit fdc08d6
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 2 deletions.
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,24 @@

Front-matter parser.

## API

### parse(str)

Parses YAML front-matter.

### stringify(obj)

Converts an object to YAML front-matter string.

### split(str)

Splits a YAML front-matter string.

### escape(str)

Converts hard tabs to soft tabs.

## License

MIT
14 changes: 13 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,18 @@ var split = exports.split = function(str){
return {data: data, content: content};
};

var escapeYaml = exports.escape = function(str){
return str.replace(/\n(\t+)/g, function(match, tabs){
var result = '\n';

for (var i = 0, len = tabs.length; i < len; i++){
result += ' ';
}

return result;
});
};

var parse = exports.parse = function(str){
var splitData = split(str),
raw = splitData.data,
Expand All @@ -23,7 +35,7 @@ var parse = exports.parse = function(str){
if (!raw) return {_content: str};

try {
var data = yaml.parse(raw);
var data = yaml.parse(escapeYaml(raw));

if (typeof data === 'object'){
data._content = content;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "hexo-front-matter",
"version": "0.0.1",
"version": "0.0.2",
"description": "Front-matter parser.",
"main": "index",
"scripts": {
Expand Down
31 changes: 31 additions & 0 deletions test/yfm.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,37 @@ var yaml = require('yamljs'),
describe('yfm', function(){
var yfm = require('../index');

describe('split', function(){
it('with data', function(){
yfm.split([
'foo',
'---',
'bar'
].join('\n')).should.eql({data: 'foo', content: 'bar'});
});

it('without data', function(){
yfm.split([
'foo',
'bar'
].join('\n')).should.eql({content: 'foo\nbar'});
});
});

describe('escape', function(){
it('escape', function(){
yfm.escape([
'foo',
'\tbar',
'\t\tbaz'
].join('\n')).should.eql([
'foo',
' bar',
' baz'
].join('\n'));
});
});

describe('parse', function(){
it('only content', function(){
var str = [
Expand Down

0 comments on commit fdc08d6

Please sign in to comment.