node/tools/doc/common.js
Tobias Nießen 5b9f8e1d77
tools: make metadata parsing less permissive
PR-URL: https://github.com/nodejs/node/pull/19512
Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
2018-03-25 03:35:23 +02:00

46 lines
1.0 KiB
JavaScript

'use strict';
const yaml = require('js-yaml');
function isYAMLBlock(text) {
return !!text.match(/^<!-- YAML/);
}
exports.isYAMLBlock = isYAMLBlock;
function arrify(value) {
return Array.isArray(value) ? value : [value];
}
function extractAndParseYAML(text) {
text = text.trim();
text = text.replace(/^<!-- YAML/, '')
.replace(/-->$/, '');
// js-yaml.safeLoad() throws on error
const meta = yaml.safeLoad(text);
const added = meta.added;
if (added) {
// Since semver-minors can trickle down to previous major versions,
// features may have been added in multiple versions.
meta.added = arrify(added);
}
const deprecated = meta.deprecated;
if (deprecated) {
// Treat deprecated like added for consistency.
meta.deprecated = arrify(deprecated);
}
meta.changes = meta.changes || [];
meta.changes.forEach((entry) => {
entry.description = entry.description.replace(/^\^\s*/, '');
});
return meta;
}
exports.extractAndParseYAML = extractAndParseYAML;