From a5308280662b0efe5d14f03a5854d26c5e42852e Mon Sep 17 00:00:00 2001 From: tgreyuk Date: Fri, 26 Feb 2021 23:13:48 +0000 Subject: [PATCH] feat: Added Front matter utils --- .../src/components/front-matter.ts | 65 ------------------- .../src/utils/front-matter.ts | 54 +++++++++++++++ .../__snapshots__/front-matter.spec.ts.snap | 10 --- .../test/specs/front-matter.spec.ts | 32 --------- 4 files changed, 54 insertions(+), 107 deletions(-) delete mode 100644 packages/typedoc-plugin-markdown/src/components/front-matter.ts create mode 100644 packages/typedoc-plugin-markdown/src/utils/front-matter.ts delete mode 100644 packages/typedoc-plugin-markdown/test/specs/__snapshots__/front-matter.spec.ts.snap delete mode 100644 packages/typedoc-plugin-markdown/test/specs/front-matter.spec.ts diff --git a/packages/typedoc-plugin-markdown/src/components/front-matter.ts b/packages/typedoc-plugin-markdown/src/components/front-matter.ts deleted file mode 100644 index fb8ed3ccd..000000000 --- a/packages/typedoc-plugin-markdown/src/components/front-matter.ts +++ /dev/null @@ -1,65 +0,0 @@ -import * as path from 'path'; - -import { - Component, - ContextAwareRendererComponent, -} from 'typedoc/dist/lib/output/components'; -import { PageEvent } from 'typedoc/dist/lib/output/events'; - -import { reflectionTitle } from '../resources/helpers/reflection-title'; - -@Component({ name: 'front-matter' }) -export class FrontMatterComponent extends ContextAwareRendererComponent { - initialize() { - super.initialize(); - this.listenTo(this.application.renderer, { - [PageEvent.END]: this.onPageEnd, - }); - } - - onPageEnd(page: PageEvent) { - if (page.contents) { - page.contents = page.contents - .replace(/^/, this.getYamlString(this.getYamlItems(page)) + '\n\n') - .replace(/[\r\n]{3,}/g, '\n\n'); - } - } - - getYamlString(yamlItems: { [key: string]: string | number | boolean }) { - const yaml = `--- -${Object.entries(yamlItems) - .map( - ([key, value]) => - `${key}: ${ - typeof value === 'string' ? `"${this.escapeYAMLString(value)}"` : value - }`, - ) - .join('\n')} ----`; - return yaml; - } - - getYamlItems(page: PageEvent) { - return this.getDefaultValues(page); - } - - getDefaultValues(page: PageEvent) { - return { - id: this.getId(page), - title: this.getTitle(page), - }; - } - - getId(page: PageEvent) { - return path.basename(page.url, path.extname(page.url)); - } - - getTitle(page: PageEvent) { - return reflectionTitle.call(page, false); - } - - // prettier-ignore - escapeYAMLString(str: string) { - return str.replace(/([^\\])'/g, '$1\\\'').replace(/\"/g, ''); - } -} diff --git a/packages/typedoc-plugin-markdown/src/utils/front-matter.ts b/packages/typedoc-plugin-markdown/src/utils/front-matter.ts new file mode 100644 index 000000000..398de9069 --- /dev/null +++ b/packages/typedoc-plugin-markdown/src/utils/front-matter.ts @@ -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, ''); + } diff --git a/packages/typedoc-plugin-markdown/test/specs/__snapshots__/front-matter.spec.ts.snap b/packages/typedoc-plugin-markdown/test/specs/__snapshots__/front-matter.spec.ts.snap deleted file mode 100644 index e78f71bbf..000000000 --- a/packages/typedoc-plugin-markdown/test/specs/__snapshots__/front-matter.spec.ts.snap +++ /dev/null @@ -1,10 +0,0 @@ -// Jest Snapshot v1, https://goo.gl/fbAQLP - -exports[`FrontMatter: should prepend YAML block to start of page 1`] = ` -"--- -id: \\"frontmatterclass\\" -title: \\"Class: FrontMatterClass\\" ---- - -CONTENTS" -`; diff --git a/packages/typedoc-plugin-markdown/test/specs/front-matter.spec.ts b/packages/typedoc-plugin-markdown/test/specs/front-matter.spec.ts deleted file mode 100644 index f62670da3..000000000 --- a/packages/typedoc-plugin-markdown/test/specs/front-matter.spec.ts +++ /dev/null @@ -1,32 +0,0 @@ -import { PageEvent } from 'typedoc/dist/lib/output/events'; - -import { FrontMatterComponent } from '../../dist/components/front-matter'; -import { TestApp } from '../test-app'; - -describe(`FrontMatter:`, () => { - let testApp: TestApp; - let frontMatterComponent: FrontMatterComponent; - beforeAll(async () => { - testApp = new TestApp(['frontmatter.ts']); - await testApp.bootstrap(); - testApp.renderer.addComponent( - 'frontmatter', - new FrontMatterComponent(testApp.renderer), - ); - frontMatterComponent = testApp.renderer.getComponent( - 'frontmatter', - ) as FrontMatterComponent; - }); - - test(`should prepend YAML block to start of page`, () => { - const reflection = testApp.findReflection('FrontMatterClass'); - const page = { - project: testApp.project, - model: reflection, - url: reflection.url, - contents: 'CONTENTS', - } as PageEvent; - frontMatterComponent.onPageEnd(page); - expect(page.contents).toMatchSnapshot(); - }); -});