mirror of
https://github.com/pengzhanbo/vuepress-theme-plume.git
synced 2026-04-23 10:58:13 +08:00
52 lines
1.4 KiB
TypeScript
52 lines
1.4 KiB
TypeScript
import { pathJoin } from '../utils/index.js'
|
|
import type { SidebarItem } from '../../shared/index.js'
|
|
|
|
export function resolveLinkBySidebar(
|
|
sidebar: 'auto' | (string | SidebarItem)[],
|
|
_prefix: string,
|
|
) {
|
|
const res: Record<string, string> = {}
|
|
|
|
if (sidebar === 'auto') {
|
|
return res
|
|
}
|
|
|
|
for (const item of sidebar) {
|
|
if (typeof item !== 'string') {
|
|
const { prefix, dir = '', link = '/', items, text = '' } = item
|
|
getSidebarLink(items, link, text, pathJoin(_prefix, prefix || dir), res)
|
|
}
|
|
}
|
|
return res
|
|
}
|
|
|
|
function getSidebarLink(items: 'auto' | (string | SidebarItem)[] | undefined, link: string, text: string, dir = '', res: Record<string, string> = {}) {
|
|
if (items === 'auto')
|
|
return
|
|
|
|
if (!items) {
|
|
res[pathJoin(dir, `${text}.md`)] = link
|
|
return
|
|
}
|
|
|
|
for (const item of items) {
|
|
if (typeof item === 'string') {
|
|
if (!link)
|
|
continue
|
|
if (item) {
|
|
res[pathJoin(dir, `${item}.md`)] = link
|
|
}
|
|
else {
|
|
res[pathJoin(dir, 'README.md')] = link
|
|
res[pathJoin(dir, 'index.md')] = link
|
|
res[pathJoin(dir, 'readme.md')] = link
|
|
}
|
|
res[dir] = link
|
|
}
|
|
else {
|
|
const { prefix, dir: subDir = '', link: subLink = '/', items: subItems, text: subText = '' } = item
|
|
getSidebarLink(subItems, pathJoin(link, subLink), subText, pathJoin(prefix || dir, subDir), res)
|
|
}
|
|
}
|
|
}
|