diff --git a/packages/devui-vue/docs/.vitepress/config.mts b/packages/devui-vue/docs/.vitepress/config.mts index 3ba9db4f89..89bddc7500 100644 --- a/packages/devui-vue/docs/.vitepress/config.mts +++ b/packages/devui-vue/docs/.vitepress/config.mts @@ -11,6 +11,7 @@ export default defineConfig({ head, markdown: { + headers: true, config: (md) => { md.use(demoblockPlugin as any); }, diff --git a/packages/devui-vue/docs/.vitepress/devui-theme/composables/activeBar.ts b/packages/devui-vue/docs/.vitepress/devui-theme/composables/activeBar.ts index 95ad9fec90..6831bf4e0c 100644 --- a/packages/devui-vue/docs/.vitepress/devui-theme/composables/activeBar.ts +++ b/packages/devui-vue/docs/.vitepress/devui-theme/composables/activeBar.ts @@ -47,7 +47,7 @@ export function useActiveSidebarLinks(container: Ref, marker: Ref, marker: Ref = T extends Array ? (U & P)[] : never +type EnhanceArrayElement = T extends Array ? (U & P)[] : never; type Headers = EnhanceArrayElement< PageData['headers'], { - children?: Headers + children?: Headers; } -> +>; export const useToc = () => { - const { page } = useData() - - return computed(() => resolveHeaders(page.value.headers)) -} - -export const resolveLink = (base: string, path: string) => { - if (path === undefined) { - return path - } - // keep relative hash to the same page - if (path.startsWith('#')) { - return path - } - return joinUrl(base, path) -} - -export const resolveHeaders = (headers: PageData['headers']) => { - if (!headers) return; - - return mapHeaders(groupHeaders(headers)) -} - -export function groupHeaders(headers: PageData['headers']) { - headers = headers.map((h) => Object.assign({}, h)) - let lastH2 - - headers.forEach((h) => { - if (h.level === 3) { - lastH2 = h - } else if (lastH2) { - ;(lastH2.children || (lastH2.children = [])).push(h) - } - }) - return headers.filter((h) => h.level === 3) -} - -export function mapHeaders(headers: Headers) { - return headers.map((header) => ({ + const { page } = useData(); + + return computed(() => resolveHeaders(page.value.headers)); +}; + +export const resolveHeaders = (_headers: PageData['headers']) => { + if (!_headers) return; + + let headers = Array
(); + _headers.forEach((h: Header) => { + console.log(h, h.level); + if (h.level === 2) { + headers.push(h); + h.children.forEach((h: Header) => { + if (h.level === 3) headers.push(h); + }); + } else if (h.level === 3) headers.push(h); + }); + return headers.map((header: Header) => ({ text: header.title, link: `#${header.slug}`, - children: header.children ? mapHeaders(header.children) : undefined, - })) -} + children: undefined, + })); +};