From c1f8c514b0d890428645cca1187868932ec9bacb Mon Sep 17 00:00:00 2001 From: Andreas Nedbal Date: Sat, 18 Dec 2021 23:42:58 +0100 Subject: [PATCH] Add option to specify a custom description --- README.md | 4 ++++ src/helpers.ts | 13 +++++++++++++ src/main.ts | 16 +++++++++++++++- 3 files changed, 32 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index bc98dda..f28957f 100644 --- a/README.md +++ b/README.md @@ -42,6 +42,10 @@ The following arguments can be used in addition to the default [TypeDoc argument Specify a custom link for the top-most title.
Example: `https://parent-docs-site.com` +- `--customDescription`
+ Specify a custom meta description.
+ Example: `A test description` + - `--favicon`
Specify the name or URL of the favicon file.
Example: `public/favicon.ico` diff --git a/src/helpers.ts b/src/helpers.ts index 636f6bf..ccabd59 100644 --- a/src/helpers.ts +++ b/src/helpers.ts @@ -65,6 +65,19 @@ export function replaceTopMostTitle(html: string, title: string): string { ); } +/** + * Replaces the meta description + * @param html HTML string to replace into. + * @param description The new description to set. + */ +export function replaceDescription(html: string, description: string): string { + return html.replace(/()/, + '$1' + // Start of meta tag + description.replace('"', '') + // The description (also preventing escaping the attribute) + '$3' // end of meta tag + ); +} + /** * Replaces the top-most title link. * @param html HTML string to replace into. diff --git a/src/main.ts b/src/main.ts index 4e91f41..d2dd337 100644 --- a/src/main.ts +++ b/src/main.ts @@ -7,7 +7,8 @@ import { makeRelativeToRoot, isUrl, replaceTopMostTitle, - replaceTopMostTitleLink + replaceTopMostTitleLink, + replaceDescription } from './helpers'; const TYPEDOC_VERSION = Application.VERSION; @@ -22,6 +23,7 @@ const pluginOptions = (app: Application) => ({ footerTypedocVersion: app.options.getValue('footerTypedocVersion') as boolean, customTitle: app.options.getValue('customTitle') as string | undefined, customTitleLink: app.options.getValue('customTitleLink') as string | undefined, + customDescription: app.options.getValue('customDescription') as string | undefined }), }); @@ -70,6 +72,13 @@ export function load(app: Application) { defaultValue: undefined }); + app.options.addDeclaration({ + name: 'customDescription', + help: 'Extras Plugin: Specify a custom description for the website.', + type: ParameterType.String, + defaultValue: undefined + }); + const options = pluginOptions(app); app.renderer.on(PageEvent.END, onPageRendered.bind(options)); @@ -118,6 +127,11 @@ function onPageRendered(this: PluginOptions, page: PageEvent) { if (options.customTitleLink) { page.contents = replaceTopMostTitleLink(page.contents, options.customTitleLink); } + + // Set custom description + if (options.customDescription) { + page.contents = replaceDescription(page.contents, options.customDescription); + } } function onRenderFinished(this: PluginOptions) {