Skip to content

Commit c6af47e

Browse files
SkyBird233MingcongBai
authored andcommitted
refactor(markdown): extract MarkdownDetail component for AOSCC and Crowdsourcing pages
1 parent 9b27ab6 commit c6af47e

File tree

1 file changed

+14
-35
lines changed

1 file changed

+14
-35
lines changed

src/components/MarkdownDetail.vue

Lines changed: 14 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,22 @@
22
import CategorySecond from './CategorySecond.vue';
33
import PageNotFound from './PageNotFound.vue';
44
import { useRoute } from 'vue-router';
5-
import { useHead } from '@unhead/vue';
6-
import {
7-
shallowRef,
8-
defineAsyncComponent,
9-
watch,
10-
onMounted,
11-
onBeforeUnmount
12-
} from 'vue';
5+
import { ref, shallowRef, defineAsyncComponent, watchEffect } from 'vue';
136
147
const props = defineProps({
15-
// const articles = import.meta.glob('./articles/*.zh.md');
168
articles: {
179
type: Object,
1810
required: true
1911
},
20-
defaultTitle: String,
12+
title: String,
2113
pathPrefix: String,
2214
pathSuffix: String
2315
});
24-
25-
useHead({ title: props.defaultTitle });
16+
// const articles = import.meta.glob('./articles/*.zh.md');
2617
2718
const route = useRoute();
2819
const loadError = shallowRef(false);
29-
const articleComponent = shallowRef();
30-
const title = shallowRef(props.defaultTitle);
20+
const articleComponent = ref();
3121
3222
function loadArticle(slug) {
3323
const path = `${props.pathPrefix ?? './articles/'}${slug.split('#')[0]}${props.pathSuffix ?? '.md'}`;
@@ -41,27 +31,13 @@ function loadArticle(slug) {
4131
});
4232
}
4333
44-
watch(
34+
loadArticle(route.params.slug);
35+
watchEffect(
4536
() => route.params.slug.split('#')[0],
46-
(newSlug) => loadArticle(newSlug),
47-
{ immediate: true }
37+
() => {
38+
loadArticle(route.params.slug);
39+
}
4840
);
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-
});
6541
</script>
6642
6743
<template>
@@ -70,8 +46,11 @@ onBeforeUnmount(() => {
7046
<PageNotFound />
7147
</template>
7248
<template v-else>
73-
<CategorySecond :title="title" />
74-
<component class="vuepress-markdown-body" :is="articleComponent" />
49+
<CategorySecond v-if="title" :title="title" />
50+
<component
51+
class="vuepress-markdown-body"
52+
:is="articleComponent"
53+
ref="mdComponent" />
7554
</template>
7655
</div>
7756
</template>

0 commit comments

Comments
 (0)