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

fix(ContentRenderer): render contents only with excerpt #2246

Merged
merged 1 commit into from
Aug 24, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/runtime/components/ContentRenderer.vue
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,9 @@ export default defineComponent({

const { value, excerpt, tag } = ctx

if (!value?.body?.children?.length && slots?.empty) {
const markdownAST = excerpt ? value?.excerpt : value?.body

if (!markdownAST?.children?.length && slots?.empty) {
// Fallback on `empty` slot.
return slots.empty({ value, excerpt, tag, ...this.$attrs })
}
Expand All @@ -70,7 +72,7 @@ export default defineComponent({
}

// Use built-in ContentRendererMarkdown
if (value?._type === 'markdown' && value?.body?.children?.length) {
if (markdownAST?.type === 'root' && markdownAST?.children?.length) {
return h(
ContentRendererMarkdown,
{
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/query/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export function createQuery <T = ParsedContent> (fetcher: ContentQueryFetcher<T>
}
}

return result?._path || Array.isArray(result) ? result : result?.result
return result?._path || Array.isArray(result) || !Object.prototype.hasOwnProperty.call(result, 'result') ? result : result?.result
}

return result
Expand Down
6 changes: 6 additions & 0 deletions test/basic.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ describe('Basic usage', async () => {
expect(html).contains('<p><!--[-->p1<!--]--></p>')
})

test('Fetch only title and excerpt', async () => {
const html = await $fetch('/excerpt-only')
expect(html).contains('Excerpt paragraph')
expect(html).not.contains('Rest of the content')
})

// test('Warning for invalid file name', () => {
// expect(spyConsoleWarn).toHaveBeenCalled()
// expect(spyConsoleWarn).toHaveBeenCalledWith('Ignoring [content:with-\'invalid\'-char.md]. File name should not contain any of the following characters: \', ", ?, #, /')
Expand Down
8 changes: 8 additions & 0 deletions test/fixtures/basic/content/excerpt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Index


Excerpt paragraph

<!-- more -->

Rest of the content
11 changes: 11 additions & 0 deletions test/fixtures/basic/pages/excerpt-only.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<template>
<div>
<ContentRenderer :value="data!" :excerpt="true" />
</div>
</template>

<script lang="ts" setup>
import { queryContent, useAsyncData } from '#imports'

const { data } = await useAsyncData('excerpt', () => queryContent('/excerpt').only(['title', 'excerpt']).findOne())
</script>