Skip to content

Commit

Permalink
feat: Discover prettier config files
Browse files Browse the repository at this point in the history
  • Loading branch information
tgreyuk committed Apr 25, 2023
1 parent 8d80ccf commit 8051a37
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 5 deletions.
16 changes: 13 additions & 3 deletions packages/typedoc-plugin-markdown/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ The plugin replaces the default HTML theme with a built-in Markdown theme and ex

> Please note the `next` pre-release version may contain breaking changes within the same semantic version.
> [TypeDoc](https://typedoc.org) and [Prettier](https://prettier.io/) are both required peer dependencies.
```bash
npm install typedoc-plugin-markdown@next --save-dev
```
Expand All @@ -25,8 +27,6 @@ npm install typedoc-plugin-markdown@next --save-dev
typedoc --plugin typedoc-plugin-markdown
```

Generated Markdown is now parsed with [Prettier](https://prettier.io/) which is backed by the remark-parse package. This does require an additional peer dependency, however introduces a consistent output format.

## Options

The following options can be used in addition to relevant [TypeDoc options](https://typedoc.org/options/)
Expand Down Expand Up @@ -90,7 +90,17 @@ See [Front matter options](./docs/frontmatter.md) for further documentation.
- **`--baseUrl`**<br>
Specifies the base url for internal link. If omitted all urls will be relative. Defaults to `.`

## Documentation
## Output formatting (Prettier)

Generated Markdown is now parsed with [Prettier](https://prettier.io/) which is backed by the remark-parse package. This does require an additional peer dependency but has several benefits:

- Produces a consistent format.
- Remove unnecessary escape characters.
- Formats code blocks inside comment fenced blocks.

Any [prettier configuration](https://prettier.io/docs/en/configuration.html) files discovered will be passed as options to the parser.

## Further Documentation

- [File output options](./docs/file-output-options.md)
- [Frontmatter options](./docs/frontmatter.md)
Expand Down
18 changes: 16 additions & 2 deletions packages/typedoc-plugin-markdown/src/theme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { TemplateMapping } from './models';
import { URL_PREFIX } from './support/constants';
import { slugify } from './support/utils';
import { MarkdownThemeRenderContext } from './theme-context';

export class MarkdownTheme extends Theme {
@BindOption('entryDocument') entryDocument!: string;
@BindOption('entryPoints') entryPoints!: string[];
Expand All @@ -26,8 +27,8 @@ export class MarkdownTheme extends Theme {
@BindOption('preserveAnchorCasing') preserveAnchorCasing!: boolean;
@BindOption('readme') readme!: string;

private _renderContext?: MarkdownThemeRenderContext;

private _renderContext: MarkdownThemeRenderContext;
private _prettierOptions: prettier.Options | null;
private anchors: Record<string, string[]>;

constructor(renderer: Renderer) {
Expand All @@ -50,6 +51,18 @@ export class MarkdownTheme extends Theme {
return this._renderContext;
}

getPrettierOptions() {
if (!this._prettierOptions) {
this._prettierOptions = this.resolvePrettierOptions();
}
return this._prettierOptions;
}

resolvePrettierOptions() {
const prettierFile = prettier.resolveConfigFile.sync();
return prettierFile ? prettier.resolveConfig.sync(prettierFile) : null;
}

readmeTemplate = (pageEvent: PageEvent<ProjectReflection>) => {
return this.getRenderContext().templates.readmeTemplate(pageEvent);
};
Expand All @@ -71,6 +84,7 @@ export class MarkdownTheme extends Theme {
template: RenderTemplate<PageEvent<Reflection>>,
): string {
return prettier.format(template(page) as string, {
...this.getPrettierOptions(),
parser: 'markdown',
});
}
Expand Down

0 comments on commit 8051a37

Please sign in to comment.