-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathpostinstall-scripts.js
147 lines (125 loc) · 4.2 KB
/
postinstall-scripts.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
const fs = require("fs");
const path = require("path");
function extractMetaDescription(mdxContent) {
// Split content into lines
const lines = mdxContent.split('\n');
let firstParagraph = '';
// Find first valid paragraph
for (let i = 0; i < lines.length; i++) {
const line = lines[i].trim();
// Skip empty lines, imports, components, headers and warnings
if (!line ||
line.startsWith('import') ||
line.startsWith('<') ||
line.startsWith('#') ||
line.toLowerCase().includes('warning')) {
continue;
}
firstParagraph = line;
break;
}
return firstParagraph;
}
function addOrUpdateMetaTags(filePath) {
// Original meta description for non-docs pages
const metaDescriptionContent =
"Extension.js makes it very easy to create, develop, and distribute cross-browser extensions with no build configuration.";
const canonicalURL = "https://extension.js.org/blog/";
// Check if this is a docs page
const isDocsPage = filePath.includes('/docs/');
// If it's a docs page, find corresponding MDX file
let metaDescription = metaDescriptionContent;
if (isDocsPage) {
// Convert HTML path to MDX path
const mdxPath = filePath
.replace('doc_build', 'docs/en')
.replace('.html', '.mdx');
try {
const mdxContent = fs.readFileSync(mdxPath, 'utf8');
const extractedDesc = extractMetaDescription(mdxContent);
if (extractedDesc) {
metaDescription = extractedDesc;
}
} catch (err) {
console.warn(`Could not find MDX file for ${filePath}`);
}
}
// Read the HTML file
fs.readFile(filePath, "utf8", (err, data) => {
if (err) {
console.error("Error reading the file:", err);
return;
}
// Find the <title> tag
const titleTagRegex = /<title\b[^>]*>(.*?)<\/title>/;
const match = data.match(titleTagRegex);
if (!match) {
console.error("No <title> tag found in the file.");
return;
}
const titleTag = match[0];
const afterTitleIndex = data.indexOf(titleTag) + titleTag.length;
// Check if meta description already exists
const metaDescriptionRegex = /<meta\s+name=["']description["'].*?>/;
if (metaDescriptionRegex.test(data)) {
// Update existing meta description
data = data.replace(
metaDescriptionRegex,
`<meta name="description" content="${metaDescription}">`
);
} else {
// Insert new meta description
const newMetaTag = `<meta name="description" content="${metaDescription}">\n`;
data =
data.slice(0, afterTitleIndex) +
newMetaTag +
data.slice(afterTitleIndex);
}
// Handle canonical URL for blog pages
if (filePath.includes("blog/index")) {
const canonicalRegex = /<link\s+rel=["']canonical["'].*?>/;
if (canonicalRegex.test(data)) {
data = data.replace(
canonicalRegex,
`<link rel="canonical" href="${canonicalURL}">`
);
} else {
const newCanonicalTag = `<link rel="canonical" href="${canonicalURL}">\n`;
data =
data.slice(0, afterTitleIndex) +
newCanonicalTag +
data.slice(afterTitleIndex);
}
}
// Write updated HTML back to file
fs.writeFile(filePath, data, "utf8", (err) => {
if (err) {
console.error("Error writing to the file:", err);
return;
}
console.log(`Meta tags updated successfully for ${filePath}`);
});
});
}
// Process all files in doc_build directory
function processDocBuild(dir) {
const files = fs.readdirSync(dir);
files.forEach(file => {
const fullPath = path.join(dir, file);
const stat = fs.statSync(fullPath);
if (stat.isDirectory()) {
processDocBuild(fullPath);
} else if (file.endsWith('.html')) {
addOrUpdateMetaTags(fullPath);
}
});
}
// Paths to process
const docBuildDir = path.join(__dirname, "doc_build");
const indexFilePath = path.join(docBuildDir, "index.html");
const blogIndexPath = path.join(docBuildDir, "blog/index.html");
// Process main index and blog index
addOrUpdateMetaTags(indexFilePath);
addOrUpdateMetaTags(blogIndexPath);
// Process all docs pages
processDocBuild(path.join(docBuildDir, "docs"));