Skip to content

Commit 1be65dc

Browse files
authored
feat!: move grayMatterOptions under frontmatterOptions (#8)
1 parent 1554932 commit 1be65dc

File tree

5 files changed

+72
-22
lines changed

5 files changed

+72
-22
lines changed

src/markdown.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,10 @@ export function createMarkdown(options: ResolvedOptions) {
5151

5252
if (options.frontmatter || options.excerpt) {
5353
markdown.use(frontmatterPlugin, {
54-
renderExcerpt: false,
54+
...options.frontmatterOptions,
5555
grayMatterOptions: {
5656
excerpt: options.excerpt,
57-
...options.grayMatterOptions,
57+
...options.frontmatterOptions.grayMatterOptions,
5858
},
5959
})
6060
}

src/options.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,22 @@ export function resolveOptions(userOptions: Options): ResolvedOptions {
88
headEnabled: false,
99
headField: '',
1010
frontmatter: true,
11-
include: null,
12-
exclude: null,
1311
excerpt: false,
1412
exposeFrontmatter: true,
1513
exposeExcerpt: false,
1614
escapeCodeTagInterpolation: true,
1715
customSfcBlocks: ['route', 'i18n', 'style'],
1816
componentOptions: {},
17+
frontmatterOptions: {},
1918
markdownItOptions: {},
2019
markdownItUses: [],
2120
markdownItSetup: () => {},
22-
grayMatterOptions: {},
2321
wrapperComponent: null,
2422
transforms: {},
2523
vueVersion: userOptions.vueVersion || getVueVersion(),
2624
wrapperClasses: 'markdown-body',
25+
include: null,
26+
exclude: null,
2727
}
2828
const options = userOptions.frontmatterPreprocess
2929
? { ...defaultOptions, ...userOptions }

src/types.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,11 @@ export interface Options {
9191
*/
9292
componentOptions?: ComponentPluginOptions
9393

94+
/**
95+
* Options passed to [@mdit-vue/plugin-frontmatter](https://github.com/mdit-vue/mdit-vue/tree/main/packages/plugin-frontmatter)
96+
*/
97+
frontmatterOptions?: FrontmatterPluginOptions
98+
9499
/**
95100
* Custom function to provide defaults to the frontmatter and
96101
* move certain attributes into the "meta" category.
@@ -149,11 +154,6 @@ export interface Options {
149154
*/
150155
markdownItSetup?: (MarkdownIt: MarkdownIt) => void
151156

152-
/**
153-
* Options passed to [gray-matter](https://github.com/jonschlinkert/gray-matter#options)
154-
*/
155-
grayMatterOptions?: FrontmatterPluginOptions['grayMatterOptions']
156-
157157
/**
158158
* Class names for wrapper div
159159
*
Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Vitest Snapshot v1
22

3-
exports[`excerpt > basic-excerpt 1`] = `
4-
"<template><div class=\\"markdown-body\\"><p>This is an excerpt.</p>
3+
exports[`excerpt > raw excerpt 1`] = `
4+
"<template><div class=\\"markdown-body\\"><p>This is an excerpt which is kept as <strong>raw Markdown</strong>.</p>
55
<!-- more -->
66
<h1>Hello</h1>
77
<ul>
@@ -13,10 +13,31 @@ exports[`excerpt > basic-excerpt 1`] = `
1313
<script setup>
1414
const frontmatter = {\\"title\\":\\"Hey\\"}
1515
defineExpose({ frontmatter })
16-
const excerpt = \\"\\\\nThis is an excerpt.\\\\n\\\\n\\"
16+
const excerpt = \\"\\\\nThis is an excerpt which is kept as **raw Markdown**.\\\\n\\\\n\\"
1717
</script>
1818
<script>
1919
export const title = \\"Hey\\"
20-
export const excerpt = \\"\\\\nThis is an excerpt.\\\\n\\\\n\\"
20+
export const excerpt = \\"\\\\nThis is an excerpt which is kept as **raw Markdown**.\\\\n\\\\n\\"
21+
</script>"
22+
`;
23+
24+
exports[`excerpt > rendered excerpt 1`] = `
25+
"<template><div class=\\"markdown-body\\"><p>This is an excerpt which has been rendered to <strong>HTML</strong>.</p>
26+
<!-- more -->
27+
<h1>Hello</h1>
28+
<ul>
29+
<li>A</li>
30+
<li>B</li>
31+
<li>C</li>
32+
</ul>
33+
</div></template>
34+
<script setup>
35+
const frontmatter = {\\"title\\":\\"Hey\\"}
36+
defineExpose({ frontmatter })
37+
const excerpt = \\"<p>This is an excerpt which has been rendered to <strong>HTML</strong>.</p>\\\\n\\"
38+
</script>
39+
<script>
40+
export const title = \\"Hey\\"
41+
export const excerpt = \\"<p>This is an excerpt which has been rendered to <strong>HTML</strong>.</p>\\\\n\\"
2142
</script>"
2243
`;

test/excerpt.test.ts

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,50 @@ import { createMarkdown } from '../src/markdown'
33
import { resolveOptions } from '../src/options'
44

55
describe('excerpt', () => {
6-
const options = resolveOptions({
7-
excerpt: true,
8-
grayMatterOptions: {
6+
it('rendered excerpt', () => {
7+
const options = resolveOptions({
98
excerpt: true,
10-
excerpt_separator: '<!-- more -->',
11-
},
9+
frontmatterOptions: {
10+
grayMatterOptions: {
11+
excerpt: true,
12+
excerpt_separator: '<!-- more -->',
13+
},
14+
},
15+
})
16+
const markdownToVue = createMarkdown(options)
17+
const md = `---
18+
title: Hey
19+
---
20+
21+
This is an excerpt which has been rendered to **HTML**.
22+
23+
<!-- more -->
24+
25+
# Hello
26+
27+
- A
28+
- B
29+
- C`
30+
expect(markdownToVue('', md)).toMatchSnapshot()
1231
})
13-
const markdownToVue = createMarkdown(options)
1432

15-
it('basic-excerpt', () => {
33+
it('raw excerpt', () => {
34+
const options = resolveOptions({
35+
excerpt: true,
36+
frontmatterOptions: {
37+
renderExcerpt: false,
38+
grayMatterOptions: {
39+
excerpt: true,
40+
excerpt_separator: '<!-- more -->',
41+
},
42+
},
43+
})
44+
const markdownToVue = createMarkdown(options)
1645
const md = `---
1746
title: Hey
1847
---
1948
20-
This is an excerpt.
49+
This is an excerpt which is kept as **raw Markdown**.
2150
2251
<!-- more -->
2352

0 commit comments

Comments
 (0)