Skip to content

Commit f996b3d

Browse files
SkyBird233MingcongBai
authored andcommitted
refactor(markdown): extract MarkdownDetail component for AOSCC and Crowdsourcing pages
1 parent 0b75bd6 commit f996b3d

File tree

3 files changed

+60
-50
lines changed

3 files changed

+60
-50
lines changed

src/components/MarkdownDetail.vue

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<script setup>
2+
import CategorySecond from './CategorySecond.vue';
3+
import PageNotFound from './PageNotFound.vue';
4+
import { useRoute } from 'vue-router';
5+
import { ref, shallowRef, defineAsyncComponent, watchEffect } from 'vue';
6+
7+
const props = defineProps({
8+
articles: {
9+
type: Object,
10+
required: true
11+
},
12+
title: String,
13+
pathPrefix: String,
14+
pathSuffix: String
15+
});
16+
// const articles = import.meta.glob('./articles/*.zh.md');
17+
18+
const route = useRoute();
19+
const loadError = shallowRef(false);
20+
const articleComponent = ref();
21+
22+
function loadArticle(slug) {
23+
const path = `${props.pathPrefix ?? './articles/'}${slug.split('#')[0]}${props.pathSuffix ?? '.md'}`;
24+
const loader = props.articles[path];
25+
if (!loader) loadError.value = true;
26+
articleComponent.value = defineAsyncComponent({
27+
loader,
28+
onError: (error, retry, fail, attempts) => {
29+
loadError.value = error;
30+
}
31+
});
32+
}
33+
34+
loadArticle(route.params.slug);
35+
watchEffect(
36+
() => route.params.slug.split('#')[0],
37+
() => {
38+
loadArticle(route.params.slug);
39+
}
40+
);
41+
</script>
42+
43+
<template>
44+
<div class="pl-[1px]">
45+
<template v-if="loadError">
46+
<PageNotFound />
47+
</template>
48+
<template v-else>
49+
<CategorySecond v-if="title" :title="title" />
50+
<component
51+
class="vuepress-markdown-body"
52+
:is="articleComponent"
53+
ref="mdComponent" />
54+
</template>
55+
</div>
56+
</template>

src/pages/aoscc/AosccDetail.vue

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,9 @@
11
<script setup>
2-
import PageNotFound from '../../components/PageNotFound.vue';
3-
import { useRoute } from 'vue-router';
4-
import { ref, defineAsyncComponent, watch } from 'vue';
2+
import MarkdownDetail from '../../components/MarkdownDetail.vue';
53
6-
const route = useRoute();
7-
const error = ref();
8-
const articleComponent = ref();
94
const articles = import.meta.glob('./articles/*.zh.md');
10-
11-
function loadArticle(slug) {
12-
const path = `./articles/${slug.split('#')[0]}.zh.md`;
13-
const loader = articles[path];
14-
if (!loader) error.value = true;
15-
articleComponent.value = defineAsyncComponent(loader);
16-
}
17-
18-
loadArticle(route.params.slug);
19-
watch(() => route.params.slug.split('#')[0], loadArticle(route.params.slug));
205
</script>
216

227
<template>
23-
<div class="pl-[1px]">
24-
<template v-if="error">
25-
<page-not-found />
26-
</template>
27-
<template v-else>
28-
<component class="vuepress-markdown-body" :is="articleComponent" />
29-
</template>
30-
</div>
8+
<MarkdownDetail :articles="articles" title="AOSCC" path-suffix=".zh.md" />
319
</template>
Lines changed: 2 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,9 @@
11
<script setup>
2-
import CategorySecond from '../../components/CategorySecond.vue';
3-
import PageNotFound from '../../components/PageNotFound.vue';
4-
import { useRoute } from 'vue-router';
5-
import { ref, defineAsyncComponent, watch } from 'vue';
2+
import MarkdownDetail from '../../components/MarkdownDetail.vue';
63
7-
const route = useRoute();
8-
const error = ref(false);
9-
const articleComponent = ref();
104
const articles = import.meta.glob('./articles/*.zh.md');
11-
12-
function loadArticle(slug) {
13-
const path = `./articles/${slug.split('#')[0]}.zh.md`;
14-
const loader = articles[path];
15-
if (!loader) error.value = true;
16-
articleComponent.value = defineAsyncComponent(loader);
17-
}
18-
19-
loadArticle(route.params.slug);
20-
watch(() => route.params.slug.split('#')[0], loadArticle(route.params.slug));
215
</script>
226

237
<template>
24-
<div class="pl-[1px]">
25-
<template v-if="error">
26-
<page-not-found />
27-
</template>
28-
<template v-else>
29-
<category-second v-if="!error" title="AOSCC" />
30-
<component class="vuepress-markdown-body" :is="articleComponent" />
31-
</template>
32-
</div>
8+
<MarkdownDetail :articles="articles" title="众筹详情" path-suffix=".zh.md" />
339
</template>

0 commit comments

Comments
 (0)