This repository has been archived by the owner on Oct 1, 2024. It is now read-only.
forked from decaporg/decap-cms
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: register custom formats take 2 (decaporg#6205)
* feat: register custom formats * fix: register custom formats validation * fix: change 'format' field validation to string instead of enum Format will have the same behaviour as the widget property * test: custom formats and register function * docs: explain usage and note manual initialization requirement * fix: remove unused imports * use default extension * remove manual init note * PR comments * fix: prettier * revert unnecessary changes? * chore: more revert? * chore: newline * chore: update import --------- Co-authored-by: Jean <jlabedo@gmail.com> Co-authored-by: Misha Kaletsky <mmkal@users.noreply.github.com> Co-authored-by: Erez Rokah <erezrokah@users.noreply.github.com>
- Loading branch information
1 parent
43d5b75
commit 99906fe
Showing
9 changed files
with
194 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
packages/decap-cms-core/src/formats/__tests__/formats.spec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import { Map } from 'immutable'; | ||
|
||
import { extensionFormatters, resolveFormat } from '../formats'; | ||
import { registerCustomFormat } from '../../lib/registry'; | ||
|
||
describe('custom formats', () => { | ||
const testEntry = { | ||
collection: 'testCollection', | ||
data: { x: 1 }, | ||
isModification: false, | ||
label: 'testLabel', | ||
mediaFiles: [], | ||
meta: {}, | ||
newRecord: true, | ||
partial: false, | ||
path: 'testPath1', | ||
raw: 'testRaw', | ||
slug: 'testSlug', | ||
author: 'testAuthor', | ||
updatedOn: 'testUpdatedOn', | ||
}; | ||
it('resolves builtint formats', () => { | ||
const collection = Map({ | ||
name: 'posts', | ||
}); | ||
expect(resolveFormat(collection, { ...testEntry, path: 'test.yml' })).toEqual( | ||
extensionFormatters.yml, | ||
); | ||
expect(resolveFormat(collection, { ...testEntry, path: 'test.yaml' })).toEqual( | ||
extensionFormatters.yml, | ||
); | ||
expect(resolveFormat(collection, { ...testEntry, path: 'test.toml' })).toEqual( | ||
extensionFormatters.toml, | ||
); | ||
expect(resolveFormat(collection, { ...testEntry, path: 'test.json' })).toEqual( | ||
extensionFormatters.json, | ||
); | ||
expect(resolveFormat(collection, { ...testEntry, path: 'test.md' })).toEqual( | ||
extensionFormatters.md, | ||
); | ||
expect(resolveFormat(collection, { ...testEntry, path: 'test.markdown' })).toEqual( | ||
extensionFormatters.markdown, | ||
); | ||
expect(resolveFormat(collection, { ...testEntry, path: 'test.html' })).toEqual( | ||
extensionFormatters.html, | ||
); | ||
}); | ||
|
||
it('resolves custom format', () => { | ||
registerCustomFormat('txt-querystring', 'txt', { | ||
fromFile: file => Object.fromEntries(new URLSearchParams(file)), | ||
toFile: value => new URLSearchParams(value).toString(), | ||
}); | ||
|
||
const collection = Map({ | ||
name: 'posts', | ||
format: 'txt-querystring', | ||
}); | ||
|
||
const formatter = resolveFormat(collection, { ...testEntry, path: 'test.txt' }); | ||
|
||
expect(formatter.toFile({ foo: 'bar' })).toEqual('foo=bar'); | ||
expect(formatter.fromFile('foo=bar')).toEqual({ foo: 'bar' }); | ||
}); | ||
|
||
it('can override existing formatters', () => { | ||
// simplified version of a more realistic use case: using a different yaml library like js-yaml | ||
// to make netlify-cms play nice with other tools that edit content and spit out yaml | ||
registerCustomFormat('bad-yaml', 'yml', { | ||
fromFile: file => Object.fromEntries(file.split('\n').map(line => line.split(': '))), | ||
toFile: value => | ||
Object.entries(value) | ||
.map(([k, v]) => `${k}: ${v}`) | ||
.join('\n'), | ||
}); | ||
|
||
const collection = Map({ | ||
name: 'posts', | ||
format: 'bad-yaml', | ||
}); | ||
|
||
const formatter = resolveFormat(collection, { ...testEntry, path: 'test.txt' }); | ||
|
||
expect(formatter.toFile({ a: 'b', c: 'd' })).toEqual('a: b\nc: d'); | ||
expect(formatter.fromFile('a: b\nc: d')).toEqual({ a: 'b', c: 'd' }); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters