11<script lang="ts">
2+ import type { MinimalNode } from ' @nuxt/content'
3+
24const ContentNotFound = defineComponent ({
35 setup() {
46 showError ({
@@ -10,50 +12,40 @@ const ContentNotFound = defineComponent({
1012 </script >
1113
1214<script setup lang="ts">
13- // eslint-disable-next-line import/first
14- import type { MarkdownNode , ParsedContent } from ' @nuxt/content'
15-
16- function parseNode(node : MarkdownNode ): number {
15+ function getWordCount(nodesList : MinimalNode []): number {
1716 let wordCount = 0
1817
19- if (node .type === ' text' && node .value ) {
20- wordCount += node .value .split (' ' ).length
21- }
22-
23- for (const child of node .children ?? []) {
24- wordCount += parseNode (child )
18+ for (const node of nodesList ) {
19+ if (Array .isArray (node )) {
20+ const [content, _data, ... childNode] = node
21+ wordCount += content .length + getWordCount (childNode )
22+ } else {
23+ wordCount += node .length
24+ }
2525 }
2626
2727 return wordCount
2828}
2929
30- function getWordCount(parsedContent : ParsedContent ): number {
31- let wordCount = 0
30+ const route = useRoute ()
3231
33- if (parsedContent .body ) {
34- for (const child of parsedContent .body .children ) {
35- wordCount += parseNode (child )
36- }
37- }
38-
39- return wordCount
40- }
32+ const { data : blog } = await useAsyncData (` blog-${route .path } ` , () => queryCollection (' blogs' ).path (route .path ).first ())
4133 </script >
4234
4335<template >
44- <content-doc >
45- <template # default = " { doc } " >
36+ <div >
37+ <template v-if = " blog " >
4638 <div class =" pb-4" >
47- <h1 >{{ doc .title }}</h1 >
48- <p class =" op-70 divide-x *:px -2 first:*:pl-0 " >
49- <span >{{ useDateFormat(doc .date, 'ddd, DD MMMM YYYY') }}</span >
50- <span >{{ getWordCount(doc ) }} words</span >
39+ <h1 >{{ blog .title }}</h1 >
40+ <p class =" op-70 divide-x *:pr -2 not- first:*:pl-2 " >
41+ <span >{{ useDateFormat(blog .date, 'ddd, DD MMMM YYYY') }}</span >
42+ <span >{{ getWordCount(blog.body.value ) }} words</span >
5143 </p >
5244 </div >
53- <content-renderer :value =" doc " />
45+ <content-renderer :value =" blog " />
5446 </template >
55- <template # not-found >
47+ <template v-else >
5648 <content-not-found />
5749 </template >
58- </content-doc >
50+ </div >
5951</template >
0 commit comments