Skip to content

Commit

Permalink
Merge pull request #235 from jaredwray/feat-set-the-front-matter-on-a…
Browse files Browse the repository at this point in the history
…-source

feat: set the front matter on a source
  • Loading branch information
jaredwray authored Jan 1, 2025
2 parents 52a65e8 + f097072 commit 70d6767
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -557,6 +557,7 @@ Ecto has added in some helper functions for frontmatter in markdown files. Front

* `.hasFrontMatter(source: string): boolean` - This function checks if the markdown file has frontmatter. It takes in a string and returns a boolean value.
* `.getFrontMatter(source: string): object` - This function gets the frontmatter from the markdown file. It takes in a string and returns an object.
* `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.
* `.removeFrontMatter(source: string): string` - This function removes the frontmatter from the markdown file. It takes in a string and returns a string.

# How to Contribute
Expand Down
12 changes: 12 additions & 0 deletions src/ecto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,18 @@ export class Ecto {
return writr.frontMatter;
}

/**
* Will set the front matter in the source and return the source
* @param {string} source - The source to set the front matter
* @param {Record<string, unknown>} data - The front matter data
* @returns {string} - The source with the front matter
*/
public setFrontMatter(source: string, data: Record<string, unknown>): string {
const writr = new Writr(source);
writr.frontMatter = data;
return writr.content;
}

/**
* Remove the Front Matter from the source
* @param {string} source
Expand Down
34 changes: 34 additions & 0 deletions test/frontmatter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,38 @@ describe('Ecto FrontMatter', async () => {
const content = ecto.removeFrontMatter(frontMatterDocument);
expect(ecto.hasFrontMatter(content)).toBe(false);
});

test('should add front matter', async () => {
const ecto = new Ecto();
const frontMatter = {
title: 'Project Title',
date: '2023-10-01',
tags: ['project', 'documentation', 'example'],
};
const content = ecto.setFrontMatter(noFrontMatterDocument, frontMatter);
expect(ecto.hasFrontMatter(content)).toBe(true);
});

test('should add / update front matter with already existing', async () => {
const ecto = new Ecto();
const frontMatter = {
title: 'Project Title',
date: '2023-10-01',
tags: ['project', 'documentation', 'example'],
};
const content = ecto.setFrontMatter('', frontMatter);
expect(ecto.hasFrontMatter(content)).toBe(true);

const updatedFrontMatter = {
title: 'Updated Project Title',
date: '2023-10-02',
tags: ['project', 'documentation'],
};

const updatedContent = ecto.setFrontMatter(content, updatedFrontMatter);
const updatedFrontMatterContent = ecto.getFrontMatter(updatedContent);
expect(updatedFrontMatterContent?.title).toEqual('Updated Project Title');
expect(updatedFrontMatterContent?.date).toEqual('2023-10-02');
expect(updatedFrontMatterContent?.tags).toEqual(['project', 'documentation']);
});
});

0 comments on commit 70d6767

Please sign in to comment.