Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: adds yaml frontmatter support #49

Merged
merged 1 commit into from
Jul 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions package-lock.json

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

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,8 @@
"typedoc": "^0.23.9",
"typescript": "^4.7.4",
"uglify-js": "^3.16.3"
},
"dependencies": {
"yaml": "^2.4.5"
}
}
4 changes: 3 additions & 1 deletion src/defaults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { codeblockRenderer } from './renderers/codeblock';
import { dlRenderer } from './renderers/dl';
import { emojiRenderer } from './renderers/emoji';
import { footnoteRenderer } from './renderers/footnote';
import { frontmatter, frontmatterRenderer } from './renderers/frontmatter';
import { h1Renderer } from './renderers/h1';
import { h2Renderer } from './renderers/h2';
import { h3Renderer } from './renderers/h3';
Expand All @@ -18,7 +19,6 @@ import { italicRenderer } from './renderers/italic';
import { linkRenderer } from './renderers/link';
import { olRenderer } from './renderers/ol';
import { pRenderer } from './renderers/p';
import { strikethroughRenderer } from './renderers/strikethrough';
import {
bigintRenderer,
booleanRenderer,
Expand All @@ -28,6 +28,7 @@ import {
stringRenderer,
undefinedRenderer,
} from './renderers/primitives';
import { strikethroughRenderer } from './renderers/strikethrough';
import { subRenderer } from './renderers/sub';
import { supRenderer } from './renderers/sup';
import { tableRenderer } from './renderers/table';
Expand Down Expand Up @@ -79,6 +80,7 @@ export function getRenderers(customRenderers: Renderers = {}): Renderers {
tasks: tasksRenderer,
text: textRenderer,
ul: ulRenderer,
frontmatter: frontmatterRenderer,
...customRenderers,
};
}
32 changes: 32 additions & 0 deletions src/renderers/frontmatter.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { tsMarkdown } from '../rendering';
import { FrontmatterEntry, frontmatter } from './frontmatter';

describe('given a frontmatter entry', () => {
describe('with an object value', () => {
const entry: FrontmatterEntry = {
frontmatter: {
date: '1970-01-01',
},
};

test('renders a frontmatter header with the specified object as content', () => {
expect(tsMarkdown([entry])).toBe(
`---\n${Object.keys(entry.frontmatter)[0]}: ${
Object.values(entry.frontmatter)[0]
}\n---`
);
});
});

describe('using a helper with an object value', () => {
const entry = frontmatter({ date: '1970-01-01' });

test('renders a frontmatter header with the specified object as content', () => {
expect(tsMarkdown([entry])).toBe(
`---\n${Object.keys(entry.frontmatter)[0]}: ${
Object.values(entry.frontmatter)[0]
}\n---`
);
});
});
});
40 changes: 40 additions & 0 deletions src/renderers/frontmatter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { stringify } from 'yaml';
import { getMarkdownString } from '../rendering';
import { MarkdownRenderer, RenderOptions } from '../rendering.types';
import { MarkdownEntry } from '../shared.types';

/**
* Markdown entry for generating frontmatter elements
*/
export interface FrontmatterEntry extends MarkdownEntry {
frontmatter: Object;
}

/**
* Renderer for frontmatter entries
*
* @param entry the frontmatter object entry
* @param options
* @returns
*/
export const frontmatterRenderer: MarkdownRenderer = (
entry: FrontmatterEntry
) => {
if ('frontmatter' in entry) {
const frontmatterText = stringify(entry.frontmatter);
return {
markdown: `---\n${frontmatterText}---`,
blockLevel: true,
};
}

throw new Error('Entry is not a frontmatter object. Unable to render');
};

export function frontmatter(
content: FrontmatterEntry['frontmatter']
): FrontmatterEntry {
return {
frontmatter: content,
};
}
Loading