Skip to content

Commit

Permalink
Add option to specify a custom description
Browse files Browse the repository at this point in the history
  • Loading branch information
pixeldesu authored and Drarig29 committed Dec 19, 2021
1 parent c9b8b34 commit c1f8c51
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.<br>
Example: `https://parent-docs-site.com`

- `--customDescription`<br>
Specify a custom meta description.<br>
Example: `A test description`

- `--favicon`<br>
Specify the name or URL of the favicon file.<br>
Example: `public/favicon.ico`
Expand Down
13 changes: 13 additions & 0 deletions src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(/(<meta name="description" content=")([^<]*)("\/>)/,
'$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.
Expand Down
16 changes: 15 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import {
makeRelativeToRoot,
isUrl,
replaceTopMostTitle,
replaceTopMostTitleLink
replaceTopMostTitleLink,
replaceDescription
} from './helpers';

const TYPEDOC_VERSION = Application.VERSION;
Expand All @@ -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
}),
});

Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit c1f8c51

Please sign in to comment.