Skip to content

Commit

Permalink
fix(remark-fff): 🐛 use deepmerge
Browse files Browse the repository at this point in the history
  • Loading branch information
kwaa committed Dec 26, 2023
1 parent 41de824 commit 05012a2
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 10 deletions.
5 changes: 5 additions & 0 deletions .changeset/four-clocks-buy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"remark-fff": patch
---

Version 1.2.0-alpha.2
1 change: 1 addition & 0 deletions packages/remark-fff/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
"api-extractor": "api-extractor run --local --verbose"
},
"dependencies": {
"@fastify/deepmerge": "^1.3.0",
"fff-flavored-frontmatter": "workspace:*",
"unified": "^11.0.4"
},
Expand Down
10 changes: 6 additions & 4 deletions packages/remark-fff/src/plugin.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { Plugin, Transformer } from 'unified'

import deepmerge from '@fastify/deepmerge'
import { strict, transform } from 'fff-flavored-frontmatter'

import type { RemarkFFFOptions } from './types'
Expand All @@ -19,6 +20,7 @@ export const remarkFFF: Plugin<[RemarkFFFOptions]>
},
): Transformer =>
(_tree, file) => {
const merge = deepmerge({ all: true })
let targets: string[] | undefined

// Compatible with old target parameters before 1.2.
Expand Down Expand Up @@ -62,9 +64,9 @@ export const remarkFFF: Plugin<[RemarkFFFOptions]>
)

file.data = targets
? {
...file.data,
...targets.reduceRight((output, key) => ({ [key]: output }), output),
}
? merge(
file.data,
targets.reduceRight((output, key) => ({ [key]: output }), output),
)
: output
}
30 changes: 24 additions & 6 deletions packages/remark-fff/test/frontmatter.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,30 @@ describe('remark-fff', () => {
it('mdsvex-hugo', () => {
const file = new VFile({
data: {
a: 'b',
fm: {
draft: true,
images: 'https://fff.js.org/glowing_star.svg',
},
},
})
const { fm } = remark().use(remarkFrontmatter).use(remarkFFF, {
presets: [hugo],
target: 'mdsvex',
}).processSync(file).data as { fm: FFFFlavoredFrontmatter }
const { a, fm } = remark()
.use(remarkFrontmatter).use(remarkFFF, {
presets: [hugo],
target: 'mdsvex',
}).processSync(file).data as {
a: string
fm: FFFFlavoredFrontmatter
}
expect(a).toEqual('b')
// expect(fm.image).toEqual('https://fff.js.org/glowing_star.svg')
expect(fm.flags).toEqual(['draft'])
})
it('astro-hexo', () => {
const file = new VFile({
data: {
astro: {
a: 'b',
frontmatter: {
categories: ['foo', 'bar', 'baz'],
date: '2023-01-01',
Expand All @@ -39,15 +46,17 @@ describe('remark-fff', () => {
},
},
})
const { frontmatter: fm } = remark()
const { a, frontmatter: fm } = remark()
.use(remarkFrontmatter)
.use(remarkFFF, {
presets: [hexo],
target: 'astro',
})
.processSync(file).data.astro as {
a: string
frontmatter: FFFFlavoredFrontmatter
}
expect(a).toEqual('b')
expect(fm.created).toEqual('2023-01-01')
expect(fm.summary).toEqual('lorem ipsum')
// expect(fm.tags).toEqual(['fooo', 'baar', 'baaz', 'foo', 'bar', 'baz'])
Expand Down Expand Up @@ -142,6 +151,9 @@ describe('remark-fff strict mode', () => {
},
},
},
e: {
f: 'g',
},
},
},
})
Expand All @@ -151,7 +163,13 @@ describe('remark-fff strict mode', () => {
presets: [{ created: 'date' }],
target: ['a', 'b', 'c', 'd'],
})
.processSync(file).data as { a: { b: { c: { d: FFFFlavoredFrontmatter } } } }
.processSync(file).data as {
a: {
b: { c: { d: FFFFlavoredFrontmatter } }
e: { f: string }
}
}
expect(data.a.b.c.d.created).toEqual('2023-12-25')
expect(data.a.e.f).toEqual('g')
})
})
7 changes: 7 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 05012a2

Please sign in to comment.