Skip to content

Commit

Permalink
fix: frontmatter
Browse files Browse the repository at this point in the history
  • Loading branch information
tgreyuk committed Apr 29, 2023
1 parent 037af87 commit 31440a6
Show file tree
Hide file tree
Showing 8 changed files with 123 additions and 4 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

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

2 changes: 1 addition & 1 deletion packages/typedoc-plugin-markdown/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"test": "jest --colors --updateSnapshot",
"build-and-test": "npm run build && npm run test",
"docs": "npm run build && npm-run-all docs:*",
"docs:md-1": "typedoc --plugin typedoc-plugin-markdown --plugin typedoc-plugin-mdn-links --options ../../stubs/typedoc.json --enableFrontmatter --out ./out/md/md-1",
"docs:md-1": "typedoc --plugin typedoc-plugin-markdown --plugin typedoc-plugin-mdn-links --plugin ../../stubs/custom-plugin --options ../../stubs/typedoc.json --enableFrontmatter --out ./out/md/md-1",
"docs:md-2": "typedoc --plugin typedoc-plugin-markdown --options ../../stubs/typedoc.json --indexPageTitle \"Overview\" --indentifiersAsCodeBlocks --groupByKinds --propertiesFormat \"table\" --enumMembersFormat \"table\" --readme \"none\" --kindsWithOwnFile \"none\" --out ./out/md/md-2",
"docs:html": "typedoc --options ../../stubs/typedoc.json --out ./out/html"
},
Expand Down
69 changes: 69 additions & 0 deletions packages/typedoc-plugin-markdown/src/events.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import { Event, PageEvent, ProjectReflection } from 'typedoc';

export class MarkdownPageEvent<out Model = unknown> extends PageEvent {
/**
* The project the renderer is currently processing.
*/
project!: ProjectReflection;

/**
* The filename the page will be written to.
*/
filename!: string;

/**
* The url this page will be located at.
*/
url!: string;

/**
* The model that should be rendered on this page.
*/
readonly model: Model;

/**
* The final html content of this page.
*
* Should be rendered by layout templates and can be modified by plugins.
*/
contents?: string;

/**
* Front matter of the current page
*/
frontmatter?: any;

/**
* Triggered before a document will be rendered.
* @event
*/
static readonly BEGIN = 'beginPage';

/**
* Triggered after a document has been rendered, just before it is written to disc.
* @event
*/
static readonly END = 'endPage';

constructor(name: string, model: Model) {
super(name, model);
}
}

export class FrontmatterEvent extends Event {
/**
* The page that this markdown is being parsed for.
*/
readonly page: MarkdownPageEvent;

/**
* Triggered on the renderer when this plugin parses a markdown string.
* @event
*/
static readonly PREPARE_FRONTMATTER = 'prepareFrontmatter';

constructor(name: string, page: MarkdownPageEvent) {
super(name);
this.page = page;
}
}
30 changes: 30 additions & 0 deletions packages/typedoc-plugin-markdown/src/frontmatter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { Application, DeclarationReflection } from 'typedoc';
import { FrontmatterEvent, MarkdownPageEvent } from './events';

export function loadFrontmatter(app: Application) {
app.renderer.on(
MarkdownPageEvent.BEGIN,
(page: MarkdownPageEvent<DeclarationReflection>) => {
const enableFronmatter = app.options.getValue('enableFrontmatter');
if (enableFronmatter) {
const event = new FrontmatterEvent(
FrontmatterEvent.PREPARE_FRONTMATTER,
page,
);
page.frontmatter = { title: 'HELLO' };
app.renderer.trigger(event);
}
},
);

app.renderer.on(MarkdownPageEvent.END, (page: MarkdownPageEvent) => {
const enableFronmatter = app.options.getValue('enableFrontmatter');
if (enableFronmatter) {
try {
console.log(page.frontmatter);
} catch {
console.log('Caught');
}
}
});
}
4 changes: 4 additions & 0 deletions packages/typedoc-plugin-markdown/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Application } from 'typedoc';
import { loadFrontmatter } from './frontmatter';
import { defineOptions } from './options';
import { generateMarkdown, renderMarkdown } from './renderer';
import { MarkdownTheme } from './theme';
Expand All @@ -22,11 +23,14 @@ export function load(app: Application) {
value: renderMarkdown,
configurable: true,
});

loadFrontmatter(app);
}

/**
* Expose global entrypoints
*/
export * from './events';
export * from './models';
export * from './options-reader';
export { MarkdownRendererEvent } from './renderer';
Expand Down
1 change: 1 addition & 0 deletions packages/typedoc-plugin-markdown/src/renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { NavigationItem } from './models';

export class MarkdownRendererEvent extends RendererEvent {
navigation: NavigationItem[];
frontmatter: any;
}

export async function generateMarkdown(
Expand Down
8 changes: 6 additions & 2 deletions packages/typedoc-plugin-markdown/src/theme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
Renderer,
Theme,
} from 'typedoc';
import { MarkdownPageEvent } from './events';
import { MarkdownThemeConverterContext } from './theme-converter-context';
import { MarkdownThemeRenderContext } from './theme-render-context';

Expand Down Expand Up @@ -56,9 +57,12 @@ export class MarkdownTheme extends Theme {
}

render(
page: PageEvent<Reflection>,
template: RenderTemplate<PageEvent<Reflection>>,
page: MarkdownPageEvent<Reflection>,
template: RenderTemplate<MarkdownPageEvent<Reflection>>,
): string {
if (this.application.options.getValue('enableFrontmatter')) {
console.log('YES', page.frontmatter);
}
return prettier.format(template(page) as string, {
...this.getPrettierOptions(),
parser: 'markdown',
Expand Down
11 changes: 11 additions & 0 deletions stubs/custom-plugin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const {
FrontmatterEvent,
} = require('../packages/typedoc-plugin-markdown/dist');

module.exports = {
load(app) {
app.renderer.on(FrontmatterEvent.PREPARE_FRONTMATTER, (event) => {
event.page.frontmatter = { ...event.page.frontmatter, updated: true };
});
},
};

0 comments on commit 31440a6

Please sign in to comment.