Skip to content

Commit 70d6767

Browse files
authoredJan 1, 2025
Merge pull request #235 from jaredwray/feat-set-the-front-matter-on-a-source
feat: set the front matter on a source
2 parents 52a65e8 + f097072 commit 70d6767

File tree

3 files changed

+47
-0
lines changed

3 files changed

+47
-0
lines changed
 

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -557,6 +557,7 @@ Ecto has added in some helper functions for frontmatter in markdown files. Front
557557

558558
* `.hasFrontMatter(source: string): boolean` - This function checks if the markdown file has frontmatter. It takes in a string and returns a boolean value.
559559
* `.getFrontMatter(source: string): object` - This function gets the frontmatter from the markdown file. It takes in a string and returns an object.
560+
* `setFrontMatter(source:string, data: Record<string, unknown>)` - This function sets the front matter even if it already exists and returns the full source with the new front matter.
560561
* `.removeFrontMatter(source: string): string` - This function removes the frontmatter from the markdown file. It takes in a string and returns a string.
561562

562563
# How to Contribute

‎src/ecto.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -432,6 +432,18 @@ export class Ecto {
432432
return writr.frontMatter;
433433
}
434434

435+
/**
436+
* Will set the front matter in the source and return the source
437+
* @param {string} source - The source to set the front matter
438+
* @param {Record<string, unknown>} data - The front matter data
439+
* @returns {string} - The source with the front matter
440+
*/
441+
public setFrontMatter(source: string, data: Record<string, unknown>): string {
442+
const writr = new Writr(source);
443+
writr.frontMatter = data;
444+
return writr.content;
445+
}
446+
435447
/**
436448
* Remove the Front Matter from the source
437449
* @param {string} source

‎test/frontmatter.test.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,38 @@ describe('Ecto FrontMatter', async () => {
2929
const content = ecto.removeFrontMatter(frontMatterDocument);
3030
expect(ecto.hasFrontMatter(content)).toBe(false);
3131
});
32+
33+
test('should add front matter', async () => {
34+
const ecto = new Ecto();
35+
const frontMatter = {
36+
title: 'Project Title',
37+
date: '2023-10-01',
38+
tags: ['project', 'documentation', 'example'],
39+
};
40+
const content = ecto.setFrontMatter(noFrontMatterDocument, frontMatter);
41+
expect(ecto.hasFrontMatter(content)).toBe(true);
42+
});
43+
44+
test('should add / update front matter with already existing', async () => {
45+
const ecto = new Ecto();
46+
const frontMatter = {
47+
title: 'Project Title',
48+
date: '2023-10-01',
49+
tags: ['project', 'documentation', 'example'],
50+
};
51+
const content = ecto.setFrontMatter('', frontMatter);
52+
expect(ecto.hasFrontMatter(content)).toBe(true);
53+
54+
const updatedFrontMatter = {
55+
title: 'Updated Project Title',
56+
date: '2023-10-02',
57+
tags: ['project', 'documentation'],
58+
};
59+
60+
const updatedContent = ecto.setFrontMatter(content, updatedFrontMatter);
61+
const updatedFrontMatterContent = ecto.getFrontMatter(updatedContent);
62+
expect(updatedFrontMatterContent?.title).toEqual('Updated Project Title');
63+
expect(updatedFrontMatterContent?.date).toEqual('2023-10-02');
64+
expect(updatedFrontMatterContent?.tags).toEqual(['project', 'documentation']);
65+
});
3266
});

0 commit comments

Comments
 (0)