2
2
import CategorySecond from ' ./CategorySecond.vue' ;
3
3
import PageNotFound from ' ./PageNotFound.vue' ;
4
4
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' ;
6
13
7
14
const props = defineProps ({
15
+ // const articles = import.meta.glob('./articles/*.zh.md');
8
16
articles: {
9
17
type: Object ,
10
18
required: true
11
19
},
12
- title : String ,
20
+ defaultTitle : String ,
13
21
pathPrefix: String ,
14
22
pathSuffix: String
15
23
});
16
- // const articles = import.meta.glob('./articles/*.zh.md');
24
+
25
+ useHead ({ title: props .defaultTitle });
17
26
18
27
const route = useRoute ();
19
28
const loadError = shallowRef (false );
20
- const articleComponent = ref ();
29
+ const articleComponent = shallowRef ();
30
+ const title = shallowRef (props .defaultTitle );
21
31
22
32
function loadArticle (slug ) {
23
33
const path = ` ${ props .pathPrefix ?? ' ./articles/' }${slug .split (' #' )[0 ]}${props .pathSuffix ?? ' .md' }` ;
@@ -31,13 +41,27 @@ function loadArticle(slug) {
31
41
});
32
42
}
33
43
34
- loadArticle(route.params.slug);
35
- watchEffect(
44
+ watch(
36
45
() => route.params.slug.split('#')[0],
37
- () => {
38
- loadArticle(route.params.slug);
39
- }
46
+ (newSlug) => loadArticle(newSlug),
47
+ { immediate: true }
40
48
);
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
+ });
41
65
</script>
42
66
43
67
<template>
@@ -46,11 +70,8 @@ watchEffect(
46
70
<PageNotFound />
47
71
</template>
48
72
<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" />
54
75
</template>
55
76
</div>
56
77
</template>
0 commit comments