Skip to content
This repository has been archived by the owner on Dec 4, 2017. It is now read-only.

Commit

Permalink
Add support for onparse, onstringify callbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Mar 27, 2015
1 parent 784088e commit a34166e
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 2 deletions.
4 changes: 3 additions & 1 deletion Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,9 @@ Adds a `yaml` property to [**YAML**](https://github.com/wooorm/mdast/blob/master
* `library` (`Object?`, default: [`nodeca/js-yaml`](https://github.com/nodeca/js-yaml));
* `parse` (`string?`, default [`"safeLoad"`](https://github.com/nodeca/js-yaml#safeload-string---options-));
* `stringify` (`string?`, default [`"safeDump"`](https://github.com/nodeca/js-yaml#safedump-object---options-));
* `prettify` (`boolean?`, default: `true`) — When true, the node’s content will be overwritten by the result of `library[stringify](node.yaml)`.
* `prettify` (`boolean?`, default: `true`) — When true, the node’s content will be overwritten by the result of `library[stringify](node.yaml)`;
* `onparse` (`function(Node)`, default `function () {}`) — Invoked when YAML is parsed, during parsing;
* `onstringify` (`function(Node)`, default `function () {}`) — Invoked when YAML is stringified, during stringification.

## License

Expand Down
11 changes: 11 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ try {
jsYAML = require('js-yaml');
} catch (exception) {}

/**
* No-operation.
*/
function noop() {}

/**
* Remove trailing newline.
*
Expand All @@ -31,6 +36,7 @@ function removeLastLine(value) {
function parse(tokenize, settings) {
var parser = settings.library || jsYAML;
var method = settings.parse || 'safeLoad';
var callback = settings.onparse || noop;

/**
* Parse YAML, if available, using the bound
Expand All @@ -52,6 +58,8 @@ function parse(tokenize, settings) {
});

eat($0)(node);

callback(node, this);
};
}

Expand All @@ -64,6 +72,7 @@ function parse(tokenize, settings) {
function stringify(compile, settings) {
var stringifier = settings.library || jsYAML;
var method = settings.stringify || 'safeDump';
var callback = settings.onstringify || noop;

if (settings.prettify === false) {
return compile;
Expand All @@ -81,6 +90,8 @@ function stringify(compile, settings) {
node.value = removeLastLine(stringifier[method](node.yaml));
}

callback(node, this);

return compile.apply(this, arguments);
};
}
Expand Down
11 changes: 11 additions & 0 deletions mdast-yaml.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ try {
jsYAML = require('js-yaml');
} catch (exception) {}

/**
* No-operation.
*/
function noop() {}

/**
* Remove trailing newline.
*
Expand All @@ -32,6 +37,7 @@ function removeLastLine(value) {
function parse(tokenize, settings) {
var parser = settings.library || jsYAML;
var method = settings.parse || 'safeLoad';
var callback = settings.onparse || noop;

/**
* Parse YAML, if available, using the bound
Expand All @@ -53,6 +59,8 @@ function parse(tokenize, settings) {
});

eat($0)(node);

callback(node, this);
};
}

Expand All @@ -65,6 +73,7 @@ function parse(tokenize, settings) {
function stringify(compile, settings) {
var stringifier = settings.library || jsYAML;
var method = settings.stringify || 'safeDump';
var callback = settings.onstringify || noop;

if (settings.prettify === false) {
return compile;
Expand All @@ -82,6 +91,8 @@ function stringify(compile, settings) {
node.value = removeLastLine(stringifier[method](node.yaml));
}

callback(node, this);

return compile.apply(this, arguments);
};
}
Expand Down
2 changes: 1 addition & 1 deletion mdast-yaml.min.js

Large diffs are not rendered by default.

56 changes: 56 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,4 +143,60 @@ describe('mdast-yaml()', function () {
assert('yaml' in ast);
assert(ast.yaml === 'hello');
});

it('should accept `onparse`', function () {
var isInvoked;

/**
* Assertion.
*/
function onparse(node) {
assert(node.type === 'yaml');
assert(node.value === '"hello": "world"');
assert(node.yaml.hello === 'world');

isInvoked = true;
}

yaml([
'---',
'"hello": "world"',
'---',
'',
'# Foo bar',
''
].join('\n'), {
'onparse': onparse
});

assert(isInvoked === true);
});

it('should accept `onstringify`', function () {
var isInvoked;

/**
* Assertion.
*/
function onstringify(node) {
assert(node.type === 'yaml');
assert(node.value === 'hello: world');
assert(node.yaml.hello === 'world');

isInvoked = true;
}

yaml([
'---',
'"hello": "world"',
'---',
'',
'# Foo bar',
''
].join('\n'), {
'onstringify': onstringify
});

assert(isInvoked === true);
});
});

0 comments on commit a34166e

Please sign in to comment.