refactor(ArticleMenu): 🎨 New resolve method for ArticlesMenu (#310)

pull/304/head
北雁云依 2024-04-15 18:54:46 +08:00 committed by GitHub
parent 6361395286
commit ac59d96a77
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 23 additions and 16 deletions

View File

@ -5,10 +5,21 @@ import { data } from './articlesmenu.data'
const route = useRoute()
const articles = computed(() =>
data.menu[route.path].items.map(item => ({
...item,
link: item.link.replace('.md', ''),
})),
data
.filter((article) => {
if (!article.url.startsWith(route.path))
return false
if (article.url === route.path)
return false
const relateUrl = article.url.replace(route.path, '')
const slashCount = relateUrl.split('/').length - 1
if (slashCount > 1)
return false
if (slashCount === 1 && !relateUrl.endsWith('/'))
return false
return true
})
.map(article => ({ link: article.url, text: article.title })),
)
</script>

View File

@ -1,14 +1,10 @@
import { sidebar } from "../../sidebar";
import { createContentLoader } from 'vitepress'
declare const data: {
menu: {
[key: string]: { base: string; items: { text: string; link: string }[] };
};
};
export { data };
declare const data: { url: string, title: string }[]
export default {
load() {
return { menu: sidebar };
},
};
export { data }
export default createContentLoader('**/*.md', {
transform: list =>
list.map(item => ({ url: item.url, title: item.frontmatter.title })),
})