diff --git a/CHANGELOG.md b/CHANGELOG.md index 97e72e854..39bc81583 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ### Features +- Added `stripYamlFrontmatter` config option, #2381. - Navigation is now written to a JS file and built dynamically, which significantly decreases document generation time with large projects and also provides large space benefits. Themes may now override `DefaultTheme.buildNavigation` to customize the displayed navigation tree, #2287. diff --git a/src/lib/converter/plugins/PackagePlugin.ts b/src/lib/converter/plugins/PackagePlugin.ts index 4685aad89..2032a92b7 100644 --- a/src/lib/converter/plugins/PackagePlugin.ts +++ b/src/lib/converter/plugins/PackagePlugin.ts @@ -24,6 +24,9 @@ export class PackagePlugin extends ConverterComponent { @Option("readme") accessor readme!: string; + @Option("stripYamlFrontmatter") + accessor stripYamlFrontmatter!: boolean; + @Option("entryPointStrategy") accessor entryPointStrategy!: EntryPointStrategy; @@ -99,7 +102,9 @@ export class PackagePlugin extends ConverterComponent { if (this.readme) { // Readme path provided, read only that file. try { - this.readmeContents = readFile(this.readme); + this.readmeContents = this.processReadmeContents( + readFile(this.readme), + ); this.readmeFile = this.readme; } catch { this.application.logger.error( @@ -118,11 +123,23 @@ export class PackagePlugin extends ConverterComponent { if (result) { this.readmeFile = result.file; - this.readmeContents = result.content; + this.readmeContents = this.processReadmeContents( + result.content, + ); } } } + private processReadmeContents(contents: string) { + if (this.stripYamlFrontmatter) { + return contents.replace( + /^\s*---\r?\n[\s\S]*?\r?\n---\s*?\r?\n\s*/, + "", + ); + } + return contents; + } + private onBeginResolve(context: Context) { this.addEntries(context.project); } diff --git a/src/lib/utils/options/declaration.ts b/src/lib/utils/options/declaration.ts index e31e81a9b..c74904af2 100644 --- a/src/lib/utils/options/declaration.ts +++ b/src/lib/utils/options/declaration.ts @@ -113,6 +113,7 @@ export interface TypeDocOptionMap { gitRevision: string; gitRemote: string; readme: string; + stripYamlFrontmatter: boolean; // Output out: string; diff --git a/src/lib/utils/options/sources/typedoc.ts b/src/lib/utils/options/sources/typedoc.ts index ad3513eae..ab6bd7753 100644 --- a/src/lib/utils/options/sources/typedoc.ts +++ b/src/lib/utils/options/sources/typedoc.ts @@ -328,6 +328,11 @@ export function addTypeDocOptions(options: Pick) { help: "Path to the readme file that should be displayed on the index page. Pass `none` to disable the index page and start the documentation on the globals page.", type: ParameterType.Path, }); + options.addDeclaration({ + name: "stripYamlFrontmatter", + help: "Strip YAML frontmatter from markdown files.", + type: ParameterType.Boolean, + }); options.addDeclaration({ name: "cname", help: "Set the CNAME file text, it's useful for custom domains on GitHub Pages.",