Skip to content

Commit dfa8eb2

Browse files
unidevelwanglinsong
authored andcommitted
Auto update sphinx theme css files
1 parent f72b3e1 commit dfa8eb2

File tree

3 files changed

+77
-3
lines changed

3 files changed

+77
-3
lines changed

website/.husky/pre-commit

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
(node website/updateSiteConfig.js && git add website/siteConfig.js) || true

website/package.json

+8-3
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,18 @@
22
"scripts": {
33
"examples": "docusaurus-examples",
44
"start": "docusaurus-start",
5-
"build": "docusaurus-build",
5+
"fix-css": "sed -ibak -e 's/@supports selector(.*11\\.5rem)}}//' ./static/docs/current/_static/sphinx_immaterial_theme.*.css",
6+
"build-site": "docusaurus-build",
7+
"build": "yarn fix-css; yarn build-site",
68
"publish-gh-pages": "docusaurus-publish",
79
"write-translations": "docusaurus-write-translations",
810
"version": "docusaurus-version",
9-
"rename-version": "docusaurus-rename-version"
11+
"rename-version": "docusaurus-rename-version",
12+
"update-site-config": "updateSiteConfig",
13+
"prepare": "cd .. && husky website/.husky"
1014
},
1115
"devDependencies": {
12-
"docusaurus": "^1.7.1"
16+
"docusaurus": "^1.7.1",
17+
"husky": "^9.1.7"
1318
}
1419
}

website/updateSiteConfig.js

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
const fs = require('fs');
2+
const path = require('path');
3+
4+
async function updateSphinxThemeFiles() {
5+
const siteConfigPath = path.join(__dirname, './siteConfig.js');
6+
const staticDocsPath = path.join(__dirname, 'static/docs');
7+
8+
let content = fs.readFileSync(siteConfigPath, 'utf8');
9+
let lines = content.split('\n');
10+
11+
const pattern = /\Wstatic\/sphinx_immaterial_theme\.[a-f0-9]+\.min\.css\W/;
12+
const insertLineIndex = lines.findIndex(line => pattern.test(line));
13+
14+
if (insertLineIndex === -1) {
15+
console.error('Could not find sphinx theme file references in siteConfig.js');
16+
process.exit(1);
17+
}
18+
19+
matchSet = new Set();
20+
lines = lines.filter(line => {
21+
if ( !pattern.test(line) ){
22+
return true;
23+
} else {
24+
matchSet.add(line.trim());
25+
return false;
26+
}
27+
});
28+
29+
const versionFolders = fs.readdirSync(staticDocsPath)
30+
.filter(folder => /^\d+\.\d+(\.\d+)?$/.test(folder));
31+
32+
if (versionFolders.length === 0) {
33+
console.error('No version folders found');
34+
process.exit(1);
35+
}
36+
37+
const allCssFiles = [];
38+
const fileSet = new Set();
39+
for (const version of versionFolders) {
40+
const staticPath = path.join(staticDocsPath, version, '_static');
41+
if (fs.existsSync(staticPath)) {
42+
const files = fs.readdirSync(staticPath)
43+
.sort()
44+
.filter(file => file.startsWith('sphinx_immaterial_theme.') && file.endsWith('.min.css'))
45+
.filter(file => {
46+
if ( fileSet.has(file) ) return false;
47+
fileSet.add(file);
48+
return true;
49+
})
50+
.map(file => ` "static/${file}",`);
51+
allCssFiles.push(...files);
52+
}
53+
}
54+
55+
if (matchSet.size === allCssFiles.length &&
56+
allCssFiles.every(file => matchSet.has(file.trim()))) {
57+
process.exit(1);
58+
}
59+
60+
console.info('Found CSS files:', allCssFiles);
61+
lines.splice(insertLineIndex, 0, ...allCssFiles);
62+
63+
fs.writeFileSync(siteConfigPath, lines.join('\n'));
64+
console.log('Successfully updated sphinx theme file references');
65+
process.exit(0);
66+
}
67+
68+
updateSphinxThemeFiles().catch(console.error);

0 commit comments

Comments
 (0)