-
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.
Merge pull request #14 from weseek/feat/save-with-frontmatter
feat: Save cmsMetadata in frontmatter
- Loading branch information
Showing
8 changed files
with
89 additions
and
0 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
42 changes: 42 additions & 0 deletions
42
apps/app/src/features/cms/server/utils/extract-frontmatter.spec.ts
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,42 @@ | ||
import { extract } from './extract-frontmatter'; | ||
|
||
describe('extract-frontmatter', () => { | ||
|
||
it('returns undefined when the document does not have frontmatter', () => { | ||
// when | ||
const result = extract('# Heading\nsome text'); | ||
// then | ||
expect(result).toBeUndefined(); | ||
}); | ||
|
||
it('returns undefined when the frontmatter does not have "cmsMetadata" key', () => { | ||
// when | ||
const result = extract(`--- | ||
title: This is the Title | ||
--- | ||
# Heading | ||
some text | ||
`); | ||
// then | ||
expect(result).toBeUndefined(); | ||
}); | ||
|
||
it('extract data collectly', () => { | ||
// when | ||
const result = extract(`--- | ||
title: This is the Title | ||
cms: | ||
foo: bar | ||
--- | ||
# Heading | ||
some text | ||
`); | ||
// then | ||
expect(result).toStrictEqual({ | ||
foo: 'bar', | ||
}); | ||
}); | ||
|
||
}); |
14 changes: 14 additions & 0 deletions
14
apps/app/src/features/cms/server/utils/extract-frontmatter.ts
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,14 @@ | ||
// TODO: replace with remark | ||
import fm from 'front-matter'; | ||
import yaml from 'js-yaml'; | ||
|
||
export const extract = (markdown: string): Record<string, any> | undefined => { | ||
const extractedData = fm(markdown); | ||
|
||
const { frontmatter } = extractedData; | ||
if (frontmatter == null) { | ||
return undefined; | ||
} | ||
|
||
return yaml.load(frontmatter).cms; | ||
}; |
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 @@ | ||
export * from './extract-frontmatter'; |
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