-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path_tmp_process.js
More file actions
199 lines (169 loc) · 7.37 KB
/
_tmp_process.js
File metadata and controls
199 lines (169 loc) · 7.37 KB
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
const fs = require('fs');
const path = require('path');
const jsyaml = require('/home/runner/workspace/node_modules/js-yaml');
// ===== STEP 1: Create _common.single.yml files =====
const newFiles = {
'marketing-content/pages/_common.single.yml': 'meta:\n robots: "index, follow"\n priority: 0.8\n change_frequency: monthly\nschema:\n include:\n - organization\n - website\n',
'marketing-content/programs/_common.single.yml': 'meta:\n robots: "index, follow"\n priority: 0.9\n change_frequency: weekly\nschema:\n include:\n - organization\n - website\n',
'marketing-content/locations/_common.single.yml': 'visibility: listed\n',
'marketing-content/landings/_common.single.yml': 'meta:\n robots: "index, follow"\n priority: 0.9\n change_frequency: weekly\nschema:\n include:\n - organization\n - website\n',
'marketing-content/downloadable/_common.single.yml': 'meta:\n robots: "index, follow"\n priority: 0.9\n change_frequency: weekly\nschema:\n include:\n - organization\n - website\n',
};
for (const [fp, content] of Object.entries(newFiles)) {
fs.writeFileSync(fp, content);
console.log('Created: ' + fp);
}
// Blog - update existing
const blogPath = 'marketing-content/blog/_common.single.yml';
let blogRaw = fs.readFileSync(blogPath, 'utf8');
const blogData = jsyaml.load(blogRaw);
if (!blogData.meta) {
blogRaw = blogRaw.trimEnd() + '\nmeta:\n robots: "index, follow"\n priority: 0.8\n change_frequency: monthly\n';
}
if (!blogData.schema) {
blogRaw = blogRaw.trimEnd() + '\nschema:\n include:\n - organization\n - website\n';
}
fs.writeFileSync(blogPath, blogRaw);
console.log('Updated: ' + blogPath);
// ===== STEP 2: Process per-slug files =====
function norm(val) {
if (typeof val === 'string') return val.replace(/['"]/g, '').trim();
return val;
}
function processFiles(dir, defaultMeta, defaultSchemaInclude, extraKey) {
const slugDirs = fs.readdirSync(dir).filter(d => {
try { return fs.statSync(path.join(dir, d)).isDirectory() && !d.startsWith('_'); } catch(e) { return false; }
});
for (const slug of slugDirs) {
const fp = path.join(dir, slug, '_common.yml');
if (!fs.existsSync(fp)) continue;
const raw = fs.readFileSync(fp, 'utf8');
const data = jsyaml.load(raw);
if (!data) continue;
let removeEntireMeta = false;
let metaKeysToRemove = [];
let removeEntireSchema = false;
let removeSchemaInclude = false;
let removeExtra = false;
// Meta analysis
if (defaultMeta && data.meta) {
for (const [key, defVal] of Object.entries(defaultMeta)) {
if (key in data.meta && String(norm(data.meta[key])) === String(norm(defVal))) {
metaKeysToRemove.push(key);
}
}
const remaining = Object.keys(data.meta).filter(k => !metaKeysToRemove.includes(k));
if (remaining.length === 0 && metaKeysToRemove.length > 0) removeEntireMeta = true;
}
// Schema analysis
if (defaultSchemaInclude && data.schema && data.schema.include) {
const inc = data.schema.include.map(x => norm(String(x)));
const def = defaultSchemaInclude.map(x => norm(String(x)));
if (inc.length === def.length && inc.every((v,i) => v === def[i])) {
const otherKeys = Object.keys(data.schema).filter(k => k !== 'include');
if (otherKeys.length === 0) removeEntireSchema = true;
else removeSchemaInclude = true;
}
}
// Extra key (visibility for locations)
if (extraKey && data[extraKey.key] !== undefined) {
if (String(norm(data[extraKey.key])) === String(norm(extraKey.value))) {
removeExtra = true;
}
}
if (!removeEntireMeta && metaKeysToRemove.length === 0 && !removeEntireSchema && !removeSchemaInclude && !removeExtra) continue;
// Line-by-line processing
const lines = raw.split('\n');
const newLines = [];
let i = 0;
function skipBlock(startIndent) {
i++;
while (i < lines.length) {
const l = lines[i];
const s = l.trim();
if (s === '') { i++; continue; }
const ind = l.length - l.trimStart().length;
if (ind > startIndent) { i++; continue; }
break;
}
}
while (i < lines.length) {
const line = lines[i];
const stripped = line.trim();
const indent = line.length - line.trimStart().length;
// Remove entire meta block
if (removeEntireMeta && stripped === 'meta:' && indent === 0) {
skipBlock(0);
continue;
}
// Remove specific meta keys (indent=2, under meta:)
if (!removeEntireMeta && metaKeysToRemove.length > 0 && indent === 2) {
let skip = false;
for (const key of metaKeysToRemove) {
if (stripped.startsWith(key + ':')) { skip = true; break; }
}
if (skip) { i++; continue; }
}
// Remove entire schema block
if (removeEntireSchema && stripped === 'schema:' && indent === 0) {
skipBlock(0);
continue;
}
// Remove schema include only
if (removeSchemaInclude && stripped === 'include:' && indent === 2) {
// Skip include: and its list items
i++;
while (i < lines.length) {
const l = lines[i];
const s = l.trim();
const ind = l.length - l.trimStart().length;
if (s === '') { i++; continue; }
if (s.startsWith('- ') && ind >= 4) { i++; continue; }
break;
}
continue;
}
// Remove extra key (visibility)
if (removeExtra && indent === 0 && stripped.startsWith(extraKey.key + ':')) {
i++;
continue;
}
newLines.push(line);
i++;
}
let result = newLines.join('\n');
while (result.includes('\n\n\n')) result = result.replace(/\n\n\n/g, '\n\n');
result = result.trimEnd() + '\n';
// Check if meta block is now empty (only "meta:" with no children)
// This can happen if we removed individual keys and nothing is left
const resultData = jsyaml.load(result);
if (resultData && resultData.meta && typeof resultData.meta === 'object' && Object.keys(resultData.meta).length === 0) {
// Remove the empty meta: line
result = result.split('\n').filter(l => l.trim() !== 'meta:').join('\n');
while (result.includes('\n\n\n')) result = result.replace(/\n\n\n/g, '\n\n');
result = result.trimEnd() + '\n';
}
fs.writeFileSync(fp, result);
console.log(' Updated: ' + fp);
}
}
console.log('\nProcessing pages...');
processFiles('marketing-content/pages',
{ robots: 'index, follow', priority: 0.8, change_frequency: 'monthly' },
['organization', 'website'], null);
console.log('\nProcessing programs...');
processFiles('marketing-content/programs',
{ robots: 'index, follow', priority: 0.9, change_frequency: 'weekly' },
['organization', 'website'], null);
console.log('\nProcessing locations...');
processFiles('marketing-content/locations', null, null,
{ key: 'visibility', value: 'listed' });
console.log('\nProcessing landings...');
processFiles('marketing-content/landings',
{ robots: 'index, follow', priority: 0.9, change_frequency: 'weekly' },
['organization', 'website'], null);
console.log('\nProcessing downloadable...');
processFiles('marketing-content/downloadable',
{ robots: 'index, follow', priority: 0.9, change_frequency: 'weekly' },
['organization', 'website'], null);
console.log('\nDone!');