This repository has been archived by the owner on Sep 7, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathidentify-recipes.js
164 lines (157 loc) · 3.27 KB
/
identify-recipes.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
const path = require("path");
const signatures = [
{
recipePath: recipe("css-at-rule"),
conditions: {
tags: ["CSS", "At-rule"],
},
},
{
recipePath: recipe("css-at-rule-descriptor"),
conditions: {
tags: ["CSS", "At-rule descriptor"],
},
},
{
recipePath: recipe("css-data-type"),
conditions: {
tags: ["CSS", "Data type"],
},
},
{
recipePath: recipe("css-function"),
conditions: {
tags: ["CSS", "Function"],
},
},
{
recipePath: recipe("css-keyword"),
conditions: {
tags: ["CSS", "Keyword"],
},
},
{
recipePath: recipe("css-media-feature"),
conditions: {
tags: ["CSS", "Media feature"],
},
},
{
recipePath: recipe("css-property"),
conditions: {
tags: ["recipe:css-property"],
},
},
{
recipePath: recipe("css-selector"),
conditions: {
tags: ["CSS", "Selector"],
},
},
{
recipePath: recipe("css-shorthand-property"),
conditions: {
tags: ["recipe:css-shorthand-property"],
},
},
{
recipePath: recipe("guide"),
conditions: {
tags: ["Guide"],
},
},
{
recipePath: recipe("javascript-class"),
conditions: {
tags: ["JavaScript", "Class"],
},
},
{
recipePath: recipe("javascript-constructor"),
conditions: {
tags: ["JavaScript", "Constructor"],
},
},
{
recipePath: recipe("javascript-error"),
conditions: {
tags: ["JavaScript", "Error"],
},
},
{
recipePath: recipe("javascript-language-feature"),
conditions: {
tags: ["JavaScript", "Language feature"],
},
},
{
recipePath: recipe("javascript-method"),
conditions: {
tags: ["JavaScript", "Method"],
},
},
{
recipePath: recipe("javascript-namespace"),
conditions: {
tags: ["JavaScript", "Namespace"],
},
},
{
recipePath: recipe("javascript-property"),
conditions: {
tags: ["JavaScript", "Property"],
},
},
{
recipePath: recipe("landing-page"),
conditions: {
tags: ["Landing page"],
},
},
];
/**
* Add a path to a recipe file to `VFile.data.recipePath`.
*
* @returns A unified transformer.
*/
function identifyRecipesPlugin() {
return function transformer(tree, file) {
const tagSet = new Set(file.data.tags.map((tag) => tag.toLowerCase()));
const recipes = [];
for (const { recipePath, conditions } of signatures) {
if (hasAll(tagSet, conditions.tags)) {
recipes.push(recipePath);
}
}
if (recipes.length === 1) {
file.data.recipePath = recipes[0];
} else if (recipes.length > 1) {
file.data.recipePath = recipes;
}
};
}
/**
* Convert a recipe name to a recipe file path.
*
* @param {String} name - the name of a recipe, without an extension
* @returns {String} An absolute path to a recipe file
*/
function recipe(name) {
return path.resolve(__dirname, "../../../recipes", name + ".yaml");
}
/**
* Check if a set of tags contains some other set of tags.
*
* @param {Set} set
* @param {Set} subset
* @returns {Boolean} `true` or `false`
*/
function hasAll(set, subset) {
for (let elem of subset) {
if (!set.has(elem.toLowerCase())) {
return false;
}
}
return true;
}
module.exports = identifyRecipesPlugin;