pengzhanbo 61d1e2e37f refactor(theme): 移动端支持&修复frontmatter监听问题
1.响应式布局,支持移动端;
2.修复监听markdown文件生成frontmatter时错误问题
2022-04-30 23:09:28 +08:00

45 lines
1.2 KiB
TypeScript

import { sidebarIndex as sidebarIndexRaw } from '@internal/sidebarIndex.js'
import { ref } from 'vue'
import type { Ref } from 'vue'
import type { SidebarOptions } from '../../shared'
import { useThemeLocaleData } from './themeData'
export type SidebarIndexRef = Ref<Record<string, SidebarOptions>>
export type SidebarRef = Ref<SidebarOptions>
export const sidebarIndex: SidebarIndexRef = ref(sidebarIndexRaw)
interface UseSidebarIndex {
sidebarList: SidebarRef
initSidebarList: (path: string) => void
}
export const useSidebarIndex = (): UseSidebarIndex => {
const sidebarList: SidebarRef = ref([])
const themeLocale = useThemeLocaleData()
const notes = themeLocale.value.notes
function initSidebarList(path = ''): void {
if (!notes) return
const prefix = notes.link?.replace(/^\/|\/$/g, '')
if (path.startsWith(`/${prefix}`)) {
Object.keys(sidebarIndex.value).forEach((key) => {
if (path.startsWith(key)) {
sidebarList.value = sidebarIndex.value[key]
}
})
} else {
sidebarList.value = []
}
}
return { sidebarList, initSidebarList }
}
if (import.meta.hot) {
__VUE_HMR_RUNTIME__.updateSidebarIndex = (
data: Record<string, SidebarOptions>
) => {
sidebarIndex.value = data
}
}