This repository has been archived by the owner on Dec 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
99 lines (78 loc) · 2.98 KB
/
index.js
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
const path = require('path');
const visit = require('unist-util-visit');
const { html, link, paragraph, text } = require('mdast-builder');
const defaultConfiguration = {
property: 'parent',
separator: '>',
};
const requirementHasParent = (requirement, configuration) => Object.prototype.hasOwnProperty.call(requirement, configuration.property);
const walkRootline = (current, requirements, configuration) => {
if (requirementHasParent(current, configuration)) {
const parent = requirements.find(aRequirement => aRequirement.id === current.parent);
if (parent) {
return [
current,
...walkRootline(parent, requirements, configuration),
];
}
}
return [ current ];
};
const removeBreadcrumbs = (original) => {
const requirement = { ...original };
visit(requirement.ast, 'html', (node, index, parent) => {
if (node.value === '<div class="tracey tracey-plugin-breadcrumbs">' && parent) {
parent.children.splice(index, 3);
}
});
return requirement;
};
const createBreadcrumbs = (requirement, parents, configuration) => {
const updatedParents = parents.map((parent, index) => {
const relativeLink = path.relative(path.parse(requirement.file).dir, parent.file);
let output = [ link(relativeLink, parent.id, text(parent.id)) ];
if (index < parents.length - 1) {
output.push(text(` ${configuration.separator} `));
}
return output;
}).flat();
return [
html('<div class="tracey tracey-plugin-breadcrumbs">'),
paragraph(updatedParents),
html('</div>'),
];
};
const updateBreadcrumbs = (original, parents, configuration) => {
const requirement = { ...original };
const breadcrumbs = createBreadcrumbs(requirement, parents, configuration);
let shouldAddBreadcrumbsToTop = true;
visit(requirement.ast, 'html', (node, index, parent) => {
if (node.value === '<div class="tracey tracey-plugin-breadcrumbs">' && parent) {
parent.children.splice(index, breadcrumbs.length, ...breadcrumbs);
shouldAddBreadcrumbsToTop = false;
}
});
if (shouldAddBreadcrumbsToTop) {
visit(requirement.ast, 'yaml', (node, index, parent) => {
requirement.ast.children.splice(index + 1, 0, ...breadcrumbs);
});
}
return requirement;
};
const plugin = (configuration = defaultConfiguration) => ({ requirements, annotations }) => {
const updatedRequirements = requirements.map(theRequirement => {
const rootline = walkRootline(theRequirement, requirements, configuration);
if (rootline.length > 1) {
let [ self, ...parents ] = rootline;
return updateBreadcrumbs(theRequirement, parents, configuration);
}
return removeBreadcrumbs(theRequirement);
});
return {
requirements: updatedRequirements,
annotations,
};
};
module.exports = {
plugin,
};