提交 cbc83acf 编写于 作者: D DCloud_LXH

feat: 改进读取 _sidebar 逻辑

上级 91ef2a5e
...@@ -4,7 +4,7 @@ const highlight = require('@vuepress/markdown/lib/highlight') ...@@ -4,7 +4,7 @@ const highlight = require('@vuepress/markdown/lib/highlight')
const translatePlugin = require('./markdown/translate') const translatePlugin = require('./markdown/translate')
const headerPlugin = require('./markdown/header') const headerPlugin = require('./markdown/header')
const createSidebar = require('./markdown/createSidebar') const createSidebar = require('./markdown/createSidebar')
const { simplifySlugText, tabs } = require('./utils') const { simplifySlugText } = require('./utils')
const copyOptions = require('./config/copy'); const copyOptions = require('./config/copy');
const base = '/uniCloud/' const base = '/uniCloud/'
...@@ -39,7 +39,7 @@ const config = { ...@@ -39,7 +39,7 @@ const config = {
themeConfig: { themeConfig: {
titleLogo: 'https://qiniu-web-assets.dcloud.net.cn/unidoc/zh/unicloudlogo.png', titleLogo: 'https://qiniu-web-assets.dcloud.net.cn/unidoc/zh/unicloudlogo.png',
logo: 'https://qiniu-web-assets.dcloud.net.cn/unidoc/zh/logo.png', logo: 'https://qiniu-web-assets.dcloud.net.cn/unidoc/zh/logo.png',
sidebar: createSidebar(tabs), sidebar: createSidebar(),
sidebarDepth: 0, sidebarDepth: 0,
nextLinks: false, nextLinks: false,
prevLinks: false, prevLinks: false,
......
const fs = require('fs') const fs = require('fs')
const path = require('path') const path = require('path')
const MarkdownIt = require('markdown-it'); const MarkdownIt = require('markdown-it')
const glob = require('glob')
const createMarkdownArray = require('./createMarkdownArray') const createMarkdownArray = require('./createMarkdownArray')
const { isExternal } = require('../utils') const { isExternal } = require('../utils')
const createSiteMap = require('./createSiteMap'); const createSiteMap = require('./createSiteMap')
const links = [] const links = []
...@@ -12,9 +13,7 @@ function parseBar(tab, file, options) { ...@@ -12,9 +13,7 @@ function parseBar(tab, file, options) {
const linkName = options.link || 'link' const linkName = options.link || 'link'
const contents = [] const contents = []
new MarkdownIt() new MarkdownIt().parse(fs.readFileSync(file, { encoding: 'utf-8' }).replace(/<!--([\s\S]*?)-->/g, '')).forEach(token => {
.parse(fs.readFileSync(file, { encoding: 'utf-8' }).replace(/<!--([\s\S]*?)-->/g, ""))
.forEach(token => {
if (token.type === 'inline') { if (token.type === 'inline') {
let text let text
let link let link
...@@ -23,18 +22,18 @@ function parseBar(tab, file, options) { ...@@ -23,18 +22,18 @@ function parseBar(tab, file, options) {
switch (child.type) { switch (child.type) {
case 'text': case 'text':
text = text || child.content text = text || child.content
break; break
case 'link_open': case 'link_open':
link = link || new Map(child.attrs).get('href') link = link || new Map(child.attrs).get('href')
break; break
case 'code_inline': case 'code_inline':
try { try {
config = JSON.parse(child.content) config = JSON.parse(child.content)
} catch (error) { } } catch (error) {}
break; break
default: default:
break; break
} }
}) })
...@@ -46,10 +45,15 @@ function parseBar(tab, file, options) { ...@@ -46,10 +45,15 @@ function parseBar(tab, file, options) {
} }
} }
link = path.join('/', link.replace(/\.md\b/, '') link = path
.join(
'/',
link
.replace(/\.md\b/, '')
.replace(/\bREADME\b/, '') .replace(/\bREADME\b/, '')
.replace(/\/index/, '/') .replace(/\/index/, '/')
.replace(/\?id=/, '#')) .replace(/\?id=/, '#')
)
.replace(/\\/g, '/') .replace(/\\/g, '/')
links.push(link) links.push(link)
...@@ -59,7 +63,7 @@ function parseBar(tab, file, options) { ...@@ -59,7 +63,7 @@ function parseBar(tab, file, options) {
level: token.level, level: token.level,
[textName]: text, [textName]: text,
[linkName]: link, [linkName]: link,
...config ...config,
}) })
} }
}) })
...@@ -67,17 +71,26 @@ function parseBar(tab, file, options) { ...@@ -67,17 +71,26 @@ function parseBar(tab, file, options) {
return createMarkdownArray(contents, options.children) return createMarkdownArray(contents, options.children)
} }
module.exports = function (tabs = []) { module.exports = function () {
const sidebar = {} const sidebar = {}
tabs.forEach(tab => { // 需要反向,vuepress 在匹配路由的时候,会按照数组的顺序匹配,所以需要将最长的路径放在最前面,否则 / 路径会第一个被匹配,导致右侧栏不跟随路由变化
const tabs = glob
.GlobSync(path.resolve(__dirname, '../../**/_sidebar.md'))
.found.map(file => {
const tab = file.match(/\/docs([\s\S]+)_sidebar.md/)[1]
return tab
})
.reverse()
;(process.env.DOCS_LITE ? [] : tabs).forEach(tab => {
sidebar[tab] = parseBar(tab, path.join(__dirname, '../../', tab, '_sidebar.md'), { sidebar[tab] = parseBar(tab, path.join(__dirname, '../../', tab, '_sidebar.md'), {
text: 'title', text: 'title',
link: 'path' link: 'path',
}) })
}) })
createSiteMap(links, () => links.length = 0) createSiteMap(links, () => (links.length = 0))
return tabs.length ? sidebar : false return tabs.length ? sidebar : false
} }
...@@ -14,29 +14,7 @@ function simplifySlugText(text) { ...@@ -14,29 +14,7 @@ function simplifySlugText(text) {
return text; return text;
} }
function getFormattedDate() {
const now = new Date();
const year = now.getFullYear();
const month = (now.getMonth() + 1).toString().padStart(2, '0');
const day = now.getDate().toString().padStart(2, '0');
const hours = now.getHours().toString().padStart(2, '0');
const minutes = now.getMinutes().toString().padStart(2, '0');
const seconds = now.getSeconds().toString().padStart(2, '0');
const formattedDate = `${year}-${month}-${day}_${hours}-${minutes}-${seconds}`;
return formattedDate;
}
// 顺序有要求,会影响 for 循环匹配侧边栏
const tabs = [
'/'
]
module.exports = { module.exports = {
isExternal, isExternal,
simplifySlugText, simplifySlugText
getFormattedDate,
tabs: process.env.DOCS_LITE ? [] : tabs
} }
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册