summaryrefslogtreecommitdiff
path: root/tools/doc/common.js
blob: 86daae6cfc6d560da62e13908e5de9c5e01dd630 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
'use strict';

const yaml =
  require(`${__dirname}/../node_modules/eslint/node_modules/js-yaml`);

function isYAMLBlock(text) {
  return /^<!-- YAML/.test(text);
}

function arrify(value) {
  return Array.isArray(value) ? value : [value];
}

function extractAndParseYAML(text) {
  text = text.trim()
             .replace(/^<!-- YAML/, '')
             .replace(/-->$/, '');

  // js-yaml.safeLoad() throws on error.
  const meta = yaml.safeLoad(text);

  if (meta.added) {
    // Since semver-minors can trickle down to previous major versions,
    // features may have been added in multiple versions.
    meta.added = arrify(meta.added);
  }

  if (meta.napiVersion) {
    meta.napiVersion = arrify(meta.napiVersion);
  }

  if (meta.deprecated) {
    // Treat deprecated like added for consistency.
    meta.deprecated = arrify(meta.deprecated);
  }

  if (meta.removed) {
    meta.removed = arrify(meta.removed);
  }

  meta.changes = meta.changes || [];

  return meta;
}

module.exports = { arrify, isYAMLBlock, extractAndParseYAML };