Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: update types #2156

Merged
merged 11 commits into from
Sep 18, 2023
21 changes: 21 additions & 0 deletions playground/document-driven/components/content/ShowArticle.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<script lang="ts" setup>
import { ParsedArticle } from '../../types/article'

const { data } = await useAsyncData('key', () => queryContent('data').findOne())

const content = useContent<ParsedArticle>()
</script>

<template>
<h1>Article</h1>

<h2>data</h2>
<pre>
{{ data }}
</pre>

<h2>content</h2>
<pre>
{{ content }}
</pre>
</template>
12 changes: 12 additions & 0 deletions playground/document-driven/content/11.article.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
cover:
image: /images/cover/cover-11.jpg
alt: "Image alt text"
publishedAt: 2017-04-01
---

:show-article

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla euismod, nisl quis.

<!--more-->
Empty file.
3 changes: 2 additions & 1 deletion playground/document-driven/layouts/debug.vue
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ const { layout } = useContent()
</template>

<style>
body, html {
body,
html {
margin: 0;
padding: 0;
min-height: 100vh;
Expand Down
9 changes: 9 additions & 0 deletions playground/document-driven/types/article.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { ParsedContent } from '../../../src/runtime/types'

export interface ParsedArticle extends ParsedContent {
cover: {
image: string,
alt: string
}
publishedAt: string
}
22 changes: 11 additions & 11 deletions src/runtime/composables/content.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,28 @@ import { withoutTrailingSlash } from 'ufo'
import type { NavItem, ParsedContent } from '../types'
import { computed, shallowReactive, shallowRef, useState, useRoute } from '#imports'

export const useContentState = () => {
export const useContentState = <G extends object = Record<string, unknown>>() => {
/**
* Map of loaded pages.
*/
*/
const pages = useState<Record<string, ParsedContent>>('dd-pages', () => shallowRef(shallowReactive({})))

/**
* Previous and next page data.
* Format: [prev, next]
* Format: { 'route': [prev, next] }
*/
const surrounds = useState<Record<string, Omit<ParsedContent, 'body'>>>('dd-surrounds', () => shallowRef(shallowReactive({})))
const surrounds = useState<Record<string, [Omit<ParsedContent, 'body'> | null, Omit<ParsedContent, 'body'> | null]>>('dd-surrounds', () => shallowRef(shallowReactive({})))

/**
* Navigation tree from root of app.
*/
const navigation = useState<NavItem[]>('dd-navigation')

/**
* Globally loaded content files.
* Format: { [key: string]: ParsedContent }
* Globally loaded themes files.
* Format: { [key: string]: unknown }
*/
const globals = useState<Record<string, ParsedContent>>('dd-globals', () => shallowRef(shallowReactive({})))
const globals = useState<Record<string, G>>('dd-globals', () => shallowRef(shallowReactive({})))

return {
pages,
Expand All @@ -33,15 +33,15 @@ export const useContentState = () => {
}
}

export const useContent = () => {
const { navigation, pages, surrounds, globals } = useContentState()
export const useContent = <T extends ParsedContent, G extends object = Record<string, unknown>>() => {
const { navigation, pages, surrounds, globals } = useContentState<G>()

const _path = computed(() => withoutTrailingSlash(useRoute().path))

/**
* Current `page` key, computed from path and content state.
*/
const page = computed(() => pages.value[_path.value])
const page = computed(() => pages.value[_path.value] as T)

/**
* Current `surround` key, computed from path and content state.
Expand All @@ -56,7 +56,7 @@ export const useContent = () => {
/**
* Content type from `page`.
*/
const type = computed(() => page.value?.type)
const type = computed(() => page.value?._type)

/**
* Excerpt from `page`.
Expand Down
14 changes: 10 additions & 4 deletions src/runtime/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,30 +33,35 @@ export interface ParsedContentInternalMeta {
/**
* File type of the content, i.e `markdown`
*/
_type?: string
_type?: 'markdown' | 'yaml' | 'json' | 'csv'
/**
* Path to the file relative to the content directory
*/
_file?: string
/**
* Extension of the file
*/
_extension?: string
_extension?: 'md' | 'yaml' | 'yml' | 'json' | 'json5' | 'csv'
}

export interface ParsedContentMeta extends ParsedContentInternalMeta {
/**
* Layout
*/
layout?: string

[key: string]: any
}

export interface ParsedContent extends ParsedContentMeta {
/**
* Excerpt
*/
excerpt?: any
excerpt?: MarkdownRoot
/**
* Content body
*/
body: any
body: MarkdownRoot
}

//
Expand All @@ -81,6 +86,7 @@ export interface MarkdownRoot {
type: 'root'
children: MarkdownNode[]
props?: Record<string, any>
toc?: Toc
}

export interface MarkdownPlugin extends Record<string, any> {}
Expand Down