diff --git a/website/docs/.format/format.js b/website/docs/.format/format.js index d538a7dda..dec724d7b 100644 --- a/website/docs/.format/format.js +++ b/website/docs/.format/format.js @@ -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) => { diff --git a/website/docs/.format/slugId.js b/website/docs/.format/slugId.js index e2eeaca01..482affb5f 100644 --- a/website/docs/.format/slugId.js +++ b/website/docs/.format/slugId.js @@ -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)}}`; } }