UNPKG

1.24 kBJavaScriptView Raw
1/**
2 * @author Titus Wormer
3 * @copyright 2015 Titus Wormer
4 * @license MIT
5 * @module remark:yaml-config
6 * @fileoverview Configure remark with YAML front-matter.
7 */
8
9'use strict';
10
11/* eslint-env commonjs */
12
13/*
14 * Dependencies.
15 */
16
17var yaml = require('remark-yaml');
18
19/**
20 * No-operation.
21 */
22function noop() {}
23
24/**
25 * Wrapper factory.
26 *
27 * @param {Function} method - Type to wrap.
28 * @return {Function} - Wrapper.
29 */
30function factory(method) {
31 var callback = method || noop;
32
33 return function (node, instance) {
34 var config = node.yaml && node.yaml.remark;
35
36 if (config) {
37 try {
38 instance.setOptions(config);
39 } catch (exception) {
40 instance.file.fail(exception.message, node);
41 }
42 }
43
44 callback.apply(this, arguments);
45 };
46}
47
48/**
49 * Modify remark to parse/stringify YAML.
50 *
51 * @param {Remark} remark - Instance.
52 * @param {Object?} [options] - Plug-in configuration.
53 */
54function attacher(remark, options) {
55 var settings = options || {};
56
57 settings.onparse = factory(settings.onparse);
58 settings.onstringify = factory(settings.onstringify);
59
60 remark.use(yaml, settings);
61}
62
63/*
64 * Expose.
65 */
66
67module.exports = attacher;