Skip to content

Commit

Permalink
fix(ContentRenderer): render contents only with excerpt
Browse files Browse the repository at this point in the history
  • Loading branch information
farnabaz committed Aug 24, 2023
1 parent 5002a37 commit faacf2f
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 3 deletions.
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>

0 comments on commit faacf2f

Please sign in to comment.