Skip to content

Commit 9b27ab6

Browse files
SkyBird233MingcongBai
authored andcommitted
fix(markdown): fix several issues related to MarkdownDetail
1 parent 4abd8d3 commit 9b27ab6

File tree

3 files changed

+43
-16
lines changed

3 files changed

+43
-16
lines changed

src/components/MarkdownDetail.vue

Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,32 @@
22
import CategorySecond from './CategorySecond.vue';
33
import PageNotFound from './PageNotFound.vue';
44
import { useRoute } from 'vue-router';
5-
import { ref, shallowRef, defineAsyncComponent, watchEffect } from 'vue';
5+
import { useHead } from '@unhead/vue';
6+
import {
7+
shallowRef,
8+
defineAsyncComponent,
9+
watch,
10+
onMounted,
11+
onBeforeUnmount
12+
} from 'vue';
613
714
const props = defineProps({
15+
// const articles = import.meta.glob('./articles/*.zh.md');
816
articles: {
917
type: Object,
1018
required: true
1119
},
12-
title: String,
20+
defaultTitle: String,
1321
pathPrefix: String,
1422
pathSuffix: String
1523
});
16-
// const articles = import.meta.glob('./articles/*.zh.md');
24+
25+
useHead({ title: props.defaultTitle });
1726
1827
const route = useRoute();
1928
const loadError = shallowRef(false);
20-
const articleComponent = ref();
29+
const articleComponent = shallowRef();
30+
const title = shallowRef(props.defaultTitle);
2131
2232
function loadArticle(slug) {
2333
const path = `${props.pathPrefix ?? './articles/'}${slug.split('#')[0]}${props.pathSuffix ?? '.md'}`;
@@ -31,13 +41,27 @@ function loadArticle(slug) {
3141
});
3242
}
3343
34-
loadArticle(route.params.slug);
35-
watchEffect(
44+
watch(
3645
() => route.params.slug.split('#')[0],
37-
() => {
38-
loadArticle(route.params.slug);
39-
}
46+
(newSlug) => loadArticle(newSlug),
47+
{ immediate: true }
4048
);
49+
50+
// Update CategorySecond's title
51+
// I guess there would be a better solution :(
52+
let observer;
53+
onMounted(() => {
54+
observer = new MutationObserver((mutations) => {
55+
title.value = mutations[0].target.text.split('|')[0].trim();
56+
}).observe(document.querySelector('title'), {
57+
subtree: true,
58+
characterData: true,
59+
childList: true
60+
});
61+
});
62+
onBeforeUnmount(() => {
63+
if (observer) console.log(observer.disconnect());
64+
});
4165
</script>
4266
4367
<template>
@@ -46,11 +70,8 @@ watchEffect(
4670
<PageNotFound />
4771
</template>
4872
<template v-else>
49-
<CategorySecond v-if="title" :title="title" />
50-
<component
51-
class="vuepress-markdown-body"
52-
:is="articleComponent"
53-
ref="mdComponent" />
73+
<CategorySecond :title="title" />
74+
<component class="vuepress-markdown-body" :is="articleComponent" />
5475
</template>
5576
</div>
5677
</template>

src/pages/aoscc/AosccDetail.vue

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,8 @@ const articles = import.meta.glob('./articles/*.zh.md');
55
</script>
66

77
<template>
8-
<MarkdownDetail :articles="articles" title="AOSCC" path-suffix=".zh.md" />
8+
<MarkdownDetail
9+
:articles="articles"
10+
default-title="AOSCC"
11+
path-suffix=".zh.md" />
912
</template>

src/pages/crowdsourcing/CrowdsourcingDetail.vue

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,8 @@ const articles = import.meta.glob('./articles/*.zh.md');
55
</script>
66

77
<template>
8-
<MarkdownDetail :articles="articles" title="众筹详情" path-suffix=".zh.md" />
8+
<MarkdownDetail
9+
:articles="articles"
10+
default-title="众筹详情"
11+
path-suffix=".zh.md" />
912
</template>

0 commit comments

Comments
 (0)