Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions docs/content/3.guide/2.displaying/4.typescript.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,27 @@ const { data } = await useAsyncData(
</script>
```

## Markdown Specific Types

If you know the content being fetched will be Markdown, then you can extend the `MarkdownParsedContent`{lang="ts"} type for improved
type-safety.

```vue
<script setup lang="ts">
import type { MarkdownParsedContent } from '@nuxt/content/dist/runtime/types'

interface Article extends MarkdownParsedContent {
author: string
}
const { data } = await useAsyncData(
'first-article',
() => queryContent<Article>('articles').findOne()
)
// data.value.author will be typed as well as markdown specific entries
</script>
```


## The future

TypeScript support is a strong focus for us.
Expand Down
4 changes: 2 additions & 2 deletions src/runtime/markdown-parser/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import remarkGfm from 'remark-gfm'
import rehypeSortAttributeValues from 'rehype-sort-attribute-values'
import rehypeSortAttributes from 'rehype-sort-attributes'
import rehypeRaw from 'rehype-raw'
import { MarkdownOptions, Toc } from '../types'
import { MarkdownOptions, MarkdownParsedContent, Toc } from '../types'
import { parseFrontMatter } from './remark-mdc/frontmatter'
import { generateToc } from './toc'
import { contentHeading, generateBody } from './content'
Expand Down Expand Up @@ -61,7 +61,7 @@ export async function parse (file: string, userOptions: Partial<MarkdownOptions>
*/
const heading = contentHeading(body)

return {
return <{ meta: Partial<MarkdownParsedContent>, body: MarkdownParsedContent['body'] }> {
body: {
...body,
toc
Expand Down
3 changes: 2 additions & 1 deletion src/runtime/server/transformers/markdown.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { parse } from '../../markdown-parser'
import type { MarkdownOptions } from '../../types'
import { MarkdownParsedContent } from '../../types'
import { useRuntimeConfig } from '#imports'

const importPlugin = async (p: [string, any]) => ([
Expand All @@ -17,7 +18,7 @@ export default {

const parsed = await parse(content, config)

return {
return <MarkdownParsedContent> {
...parsed.meta,
body: parsed.body,
_type: 'markdown',
Expand Down
22 changes: 22 additions & 0 deletions src/runtime/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,28 @@ export interface Toc {
links: TocLink[]
}

export interface MarkdownParsedContent extends ParsedContent {
_type: 'markdown',
/**
* Content is empty
*/
_empty: boolean
/**
* Content description
*/
description: string
/**
* Content excerpt, generated from content
*/
excerpt?: MarkdownRoot
/**
* Parsed Markdown body with included table of contents.
*/
body: MarkdownRoot & {
toc?: Toc
}
}

export interface ContentTransformer {
name: string
extentions: string[]
Expand Down