Skip to content

Commit

Permalink
feat(markdown): flag that the excerpt is a copy of the body
Browse files Browse the repository at this point in the history
  • Loading branch information
Qwertovsky committed Jan 6, 2023
1 parent 30c26e3 commit e94b9cd
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 2 deletions.
2 changes: 2 additions & 0 deletions docs/content/3.guide/1.writing/2.markdown.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ Full amount of content beyond the more divider.

Description property will contain the excerpt content unless defined within the Front Matter props.

If there is no `<!--more-->` divider in the text then excerpt is a copy of full content. And boolean variable `excerptIsBody` will be added into the document next to `excerpt`.

Example variables will be injected into the document:

```json
Expand Down
10 changes: 8 additions & 2 deletions src/runtime/markdown-parser/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,12 @@ export async function parse (file: string, userOptions: Partial<MarkdownOptions>
toc = generateToc(body, tocOption)
}

const excerptString = useExcerpt(content)
let excerptIsBody = undefined;
let excerptString = useExcerpt(content)
if (!excerptString) {
excerptString = content;
excerptIsBody = true;
}
const excerpt = excerptString
? await generateBody(excerptString, { ...options, data })
: undefined
Expand All @@ -86,6 +91,7 @@ export async function parse (file: string, userOptions: Partial<MarkdownOptions>
_empty: content.trim().length === 0,
title: heading.title,
description: heading.description,
excerptIsBody,
excerpt,
...data
}
Expand All @@ -105,5 +111,5 @@ function useExcerpt (content: string, delimiter = /<!--\s*?more\s*?-->/i) {
if (idx !== -1) {
return content.slice(0, idx)
}
return content
return null;
}
4 changes: 4 additions & 0 deletions src/runtime/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,10 @@ export interface MarkdownParsedContent extends ParsedContent {
* Content excerpt, generated from content
*/
excerpt?: MarkdownRoot
/**
* Content excerpt equals content
*/
excerptIsBody?: Boolean
/**
* Parsed Markdown body with included table of contents.
*/
Expand Down
38 changes: 38 additions & 0 deletions test/features/parser-markdown-excerpt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,5 +57,43 @@ export const testMarkdownParserExcerpt = () => {
}
`)
})

test('When no <!--more--> then excerpt = content', async () => {
const parsed = await $fetch('/api/parse', {
method: 'POST',
body: {
id: 'content:index.md',
content: [
'# Index',
'First paragraph',
'',
'Second paragraph'
].join('\n')
}
})

expect(parsed).toHaveProperty('body')
expect(parsed).toHaveProperty('excerpt')
expect(parsed.excerpt).toBeDefined()
expect(parsed.excerpt.children).toEqual(parsed.body.children)
})

test('Keep info that excerpt equals body', async () => {
const parsed = await $fetch('/api/parse', {
method: 'POST',
body: {
id: 'content:index.md',
content: [
'# Index',
'First paragraph',
'',
'Second paragraph'
].join('\n')
}
})

expect(parsed).toHaveProperty('excerptIsBody')
expect(parsed.excerptIsBody).toBe(true)
})
})
}

0 comments on commit e94b9cd

Please sign in to comment.