-
I made a layout for the post(.md) page. In it I need to get the content of markdown to count the reading time and word count, how do I get its content? This is my layout content: <script setup lang="ts">
import readingTime from reading-time
const stats = readingTime(text)
// `text` is still missing, so how to get markdown content
</script>
<template>
<div>Reading Time: {{ stats.minutes }} mins</div>
<slot />
</template> |
Beta Was this translation helpful? Give feedback.
Answered by
ElMassimo
Feb 21, 2022
Replies: 1 comment
-
There are two main ways to achieve this, you can use a remark plugin, or you can render the markdown page and process the resulting text vnodes to count the words. I recommend that you try the first one. For example, using import { defineConfig } from 'iles'
let namedPlugins
export default defineConfig({
configResolved (config) {
namedPlugins = config.namedPlugins
},
markdown: {
remarkPlugins: [
'remark-reading-time',
() => (ast, vfile) => {
const page = namedPlugins.pages.api.pageForFilename(vfile.path)
page.frontmatter.meta.readingTime = vfile.data.readingTime
},
],
},
}) and then: <template>
<div>Reading Time: {{ $meta.readingTime.minutes }} mins</div>
<slot />
</template> I'm planning to add a setting to automatically serialize |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
ElMassimo
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
There are two main ways to achieve this, you can use a remark plugin, or you can render the markdown page and process the resulting text vnodes to count the words.
I recommend that you try the first one. For example, using
remark-reading-time
:and then: