Skip to content

Commit

Permalink
fix: use breadcrumb generator for read-more links
Browse files Browse the repository at this point in the history
Signed-off-by: ZTL-UwU <zhangtianli2006@163.com>
  • Loading branch information
ZTL-UwU committed Aug 9, 2024
1 parent 6454d2b commit 04d7f7c
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 37 deletions.
16 changes: 3 additions & 13 deletions components/content/ReadMore.vue
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,7 @@ const props = defineProps<{
target?: string;
}>();
function createBreadcrumb(link: string = 'here') {
if (link.startsWith('http'))
return link.replace(/^https?:\/\//, '');
return link
.split('/')
.filter(Boolean)
.join(' > ')
.replace('Api', 'API');
}
// Guess title from link!
const computedTitle = computed<string>(() => props.title || createBreadcrumb(props.to));
const computedTitle = computed<string>(
() => props.title || useBreadcrumb(props.to).map(x => x.title).join(' > '),
);
</script>
25 changes: 1 addition & 24 deletions components/layout/Breadcrumb.vue
Original file line number Diff line number Diff line change
Expand Up @@ -18,29 +18,6 @@

<script setup lang="ts">
const route = useRoute();
const { navigation } = useContent();
interface Item {
title: string;
href: string;
}
function generateBreadcrumb(url: string): Item[] {
const breadcrumbItems: Item[] = [];
const segments = url.split('/').filter(segment => segment !== ''); // Remove empty segments
// Construct breadcrumb for each segment
let href = '';
let nav = navigation.value;
for (let i = 0; i < segments.length; i++) {
const segment = segments[i].replace('.html', '');
href += `/${segment}`;
const page = nav.find(x => (x._path as string) === href);
nav = page?.children;
breadcrumbItems.push({ title: page?.title ?? segment, href });
}
return breadcrumbItems;
}
const breadcrumbs = computed(() => generateBreadcrumb(route.path));
const breadcrumbs = computed(() => useBreadcrumb(route.path));
</script>
24 changes: 24 additions & 0 deletions composables/useBreadcrumb.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
interface BreadcrumbItem {
title: string;
href: string;
}

export function useBreadcrumb(url: string): BreadcrumbItem[] {
const { navigation } = useContent();

const breadcrumbItems: BreadcrumbItem[] = [];
// Remove empty segments
const segments = url.split('/').filter(segment => segment !== '');

// Construct breadcrumb for each segment
let href = '';
let nav = navigation.value;
for (let i = 0; i < segments.length; i++) {
const segment = segments[i].replace('.html', '');
href += `/${segment}`;
const page = nav.find(x => (x._path as string) === href);
nav = page?.children;
breadcrumbItems.push({ title: page?.title ?? segment, href });
}
return breadcrumbItems;
}

0 comments on commit 04d7f7c

Please sign in to comment.