mirror of
https://github.com/DIYgod/RSSHub.git
synced 2025-12-04 19:16:52 +08:00
docs: 文档sidebar排序 (#2093)
This commit is contained in:
90
docs/format.js
Normal file
90
docs/format.js
Normal file
@@ -0,0 +1,90 @@
|
||||
const fs = require('fs');
|
||||
const pinyin = require('pinyin');
|
||||
const path = require('path');
|
||||
const isASCII = (str) => /^[\x00-\x7F]*$/.test(str);
|
||||
|
||||
const sortByHeading = async (filePath) => {
|
||||
fs.readFile(filePath, 'utf8', (err, data) => {
|
||||
if (err) {
|
||||
return Promise.reject(err);
|
||||
}
|
||||
const content = data.split('\n');
|
||||
|
||||
const blocks = [];
|
||||
const h1 = [];
|
||||
|
||||
let i = 0;
|
||||
while (i < content.length) {
|
||||
const m = /^##\s*(.*)$/.exec(content[i]);
|
||||
if (m) {
|
||||
const b = {
|
||||
title: m[1],
|
||||
content: [],
|
||||
};
|
||||
|
||||
b.content.push(content[i]);
|
||||
i++;
|
||||
while (i < content.length && !/^##.*$/.test(content[i])) {
|
||||
b.content.push(content[i]);
|
||||
i++;
|
||||
}
|
||||
blocks.push(b);
|
||||
} else {
|
||||
h1.push(content[i]);
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
let newContent = blocks
|
||||
.sort((a, b) => {
|
||||
const ia = isASCII(a.title[0]);
|
||||
const ib = isASCII(b.title[0]);
|
||||
if (ia && ib) {
|
||||
return a.title.toLowerCase() < b.title.toLowerCase() ? -1 : 1;
|
||||
} else if (ia || ib) {
|
||||
return ia > ib ? -1 : 1;
|
||||
} else {
|
||||
return pinyin.compare(a.title, b.title);
|
||||
}
|
||||
})
|
||||
.map((x) => x.content.join('\n'))
|
||||
.join('\n');
|
||||
h1.push(newContent);
|
||||
newContent = h1.join('\n');
|
||||
|
||||
fs.writeFile(filePath, newContent, 'utf8', (err) => {
|
||||
if (err) {
|
||||
return Promise.reject(err);
|
||||
} else {
|
||||
return Promise.resolve(newContent);
|
||||
}
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
(async () => {
|
||||
const config = require(`./.vuepress/config`);
|
||||
|
||||
let fileList = Object.keys(config.themeConfig.locales)
|
||||
.map((key) => {
|
||||
const locale = config.themeConfig.locales[key];
|
||||
if (locale.hasOwnProperty('sidebar')) {
|
||||
return locale.sidebar['/'][1].children.map((x) => path.resolve(__dirname, `./${x}.md`));
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
})
|
||||
.filter((x) => !!x);
|
||||
|
||||
fileList = [].concat.apply([], fileList);
|
||||
|
||||
fileList.forEach((filePath) => {
|
||||
sortByHeading(filePath)
|
||||
.then(() => {
|
||||
// console.log(`Processed ${filePath}`);
|
||||
})
|
||||
.catch((err) => {
|
||||
throw err;
|
||||
});
|
||||
});
|
||||
})();
|
||||
Reference in New Issue
Block a user