docs: slugify h4 (#13248)

This commit is contained in:
Tony
2023-09-11 05:51:57 +08:00
committed by GitHub
parent e86ad59f95
commit ac8059d25b
2 changed files with 35 additions and 26 deletions
+20 -20
View File
@@ -24,25 +24,25 @@ const processors = [sortByHeading, slugId];
// type,
// lang,
// }));
const loopNav = (nav, lang) =>
nav.flatMap((e) => {
if (e.items) {
return loopNav(e.items, lang);
}
if (e.link.endsWith('/')) {
return {
path: path.resolve(__dirname, '..', e.link.slice(1), 'README.md'),
type: file.NAV_TYPE,
lang,
};
} else {
return {
path: path.resolve(__dirname, '..', `${e.link.replace(/^\//, '')}.md`),
type: file.NAV_TYPE,
lang,
};
}
});
// const loopNav = (nav, lang) =>
// nav.flatMap((e) => {
// if (e.items) {
// return loopNav(e.items, lang);
// }
// if (e.link.endsWith('/')) {
// return {
// path: path.resolve(__dirname, '..', e.link.slice(1), 'README.md'),
// type: file.NAV_TYPE,
// lang,
// };
// } else {
// return {
// path: path.resolve(__dirname, '..', `${e.link.replace(/^\//, '')}.md`),
// type: file.NAV_TYPE,
// lang,
// };
// }
// });
// const loopType = (sidebar, lang, prefix) => loopSideBar(sidebar[0].children, file.GUIDE_TYPE, lang, prefix).concat(loopSideBar(sidebar[1].children, file.ROUTE_TYPE, lang, prefix));
/**
@@ -59,7 +59,7 @@ const buildFileList = async () => {
const fileList = config.guideSidebar[2].items.map(({ id }) => ({
type: file.ROUTE_TYPE,
path: path.resolve(__dirname, '..', `./${id}.md`),
lang: 'zh-CN',
lang: file.LANG_EN,
}));
// let fileList = [];
// Object.keys(config.themeConfig.locales).forEach((key) => {
+15 -6
View File
@@ -1,24 +1,29 @@
const file = require('./file');
const { pinyin } = require('pinyin-pro');
const slugify = (s) =>
encodeURIComponent(
const slugify = (s) => {
s = s?.replace(/[#&'()+,/:[\]|’“”、「」・().:|]/g, '-');
return encodeURIComponent(
pinyin(s, {
nonZh: 'consecutive',
toneType: 'none',
type: 'array',
v: true,
})
.join(' ')
.trim()
.toLowerCase()
.replace(/\s+/g, '-')
.replace(/-+/g, '-')
.replace(/(^-|-$)/g, '')
);
};
module.exports = {
rules: (list) => list.filter((e) => e.type === file.ROUTE_TYPE && e.lang === file.LANG_CN),
rules: (list) => list.filter((e) => e),
handler: async (data) => {
const content = data.split('\n');
let lastH2 = '';
let lastH3 = '';
for (let i = 0; i < content.length; i++) {
if (content[i].startsWith('## ')) {
@@ -26,9 +31,13 @@ module.exports = {
content[i] = `## ${lastH2} {#${slugify(lastH2)}}`;
} else if (content[i].startsWith('### ')) {
const title = content[i].match(`### (([^{])*)`)?.[1].trim();
lastH3 = content[i].match(`### (([^{])*)`)?.[1].trim();
content[i] = `### ${title} {#${slugify(lastH2)}-${slugify(title)}}`;
content[i] = `### ${lastH3} {#${slugify(lastH2)}-${slugify(lastH3)}}`;
} else if (content[i].startsWith('#### ')) {
const title = content[i].match(`#### (([^{])*)`)?.[1].trim();
content[i] = `#### ${title} {#${slugify(lastH2)}-${slugify(lastH3)}-${slugify(title)}}`;
}
}