Skip to content

Commit

Permalink
feat: Added Front matter utils
Browse files Browse the repository at this point in the history
  • Loading branch information
tgreyuk committed Feb 26, 2021
1 parent e8955ee commit a530828
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 107 deletions.
65 changes: 0 additions & 65 deletions packages/typedoc-plugin-markdown/src/components/front-matter.ts

This file was deleted.

54 changes: 54 additions & 0 deletions packages/typedoc-plugin-markdown/src/utils/front-matter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { PageEvent } from 'typedoc/dist/lib/output/events';

import { reflectionTitle } from '../resources/helpers/reflection-title';

/*
* Front Matter variable model.
*/
export interface FrontMatterVars {
[key: string]: string | number | boolean;
}

/*
* Prepends YAML block to top of page.
* @param page
* @param vars
*/
export const addYAML = (page: PageEvent, vars: FrontMatterVars) => {
if (page.contents) {
page.contents = page.contents
.replace(/^/, toYAML(vars) + '\n\n')
.replace(/[\r\n]{3,}/g, '\n\n');
}
};

/*
* Returns the page title as rendered in the document h1(# title)
* @param page
*/
export const getPageTitle = (page: PageEvent) => {
return reflectionTitle.call(page, false);
};

/**
* Converts YAML object to a YAML string
* @param vars
*/
const toYAML = (vars: FrontMatterVars) => {
const yaml = `---
${Object.entries(vars)
.map(
([key, value]) =>
`${key}: ${
typeof value === 'string' ? `"${escapeString(value)}"` : value
}`,
)
.join('\n')}
---`;
return yaml;
};

// prettier-ignore
const escapeString=(str: string)=> {
return str.replace(/([^\\])'/g, '$1\\\'').replace(/\"/g, '');
}

This file was deleted.

32 changes: 0 additions & 32 deletions packages/typedoc-plugin-markdown/test/specs/front-matter.spec.ts

This file was deleted.

0 comments on commit a530828

Please sign in to comment.