Skip to content

Commit

Permalink
Compute generation date string on client-side
Browse files Browse the repository at this point in the history
  • Loading branch information
Drarig29 committed Jul 15, 2022
1 parent c719a7d commit dc4cee6
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 8 deletions.
14 changes: 13 additions & 1 deletion src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,25 @@ export function appendFavicon(html: string, url: string): string {
* @param value A string value.
*/
export function appendToFooter(html: string, value: string): string {
return html.replace(/(<p>Generated using.*TypeDoc.*)(<\/p>)/,
return html.replace(/(<p.*>Generated using.*TypeDoc.*)(<\/p>)/,
'$1' + // Start of <p>
value +
'$2' // End of <p>
);
}

/**
* Sets up the newline in footer.
* @param html HTML string to append to.
*/
export function setupNewlineInFooter(html: string): string {
return html.replace(/(<p)(>Generated using.*TypeDoc)/,
'$1' + // Opening of <p> tag
' style="line-height: 28px;"' + // Add spacing between the first line of footer and the date.
'$2' // End of "Generated using TypeDoc"
);
}

/**
* Determines whether a string is a URL.
* @param url The URL to check.
Expand Down
20 changes: 13 additions & 7 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import {
isUrl,
replaceTopMostTitle,
replaceTopMostTitleLink,
replaceDescription
replaceDescription,
setupNewlineInFooter
} from './helpers';

const TYPEDOC_VERSION = Application.VERSION;
Expand Down Expand Up @@ -105,17 +106,22 @@ function onPageRendered(this: PluginOptions, page: PageEvent) {
page.contents = appendToFooter(page.contents, ` v${TYPEDOC_VERSION}`);
}

page.contents = setupNewlineInFooter(page.contents);

// Add generation date and/or time.
if (!options.hideGenerator && (options.footerDate || options.footerTime)) {
const now = new Date();
const date = ` on ${now.toLocaleDateString()}`;
const time = ` at ${now.toLocaleTimeString()}`;

let dateTime = ',';
if (options.footerDate) dateTime += date;
if (options.footerTime) dateTime += time;
let args = [];
if (options.footerDate) args.push("dateStyle: 'medium'");
if (options.footerTime) args.push("timeStyle: 'long'");

const dateFormatter = `new Intl.DateTimeFormat(navigator.language, {${args.join(',')}})`

// Compute the generation date string on client-side.
const time = `<br><span id="generation-date"></span><script>window.GENERATION_DATE=${now.getTime()};document.getElementById('generation-date').innerText=${dateFormatter}.format(window.GENERATION_DATE)</script>`;

page.contents = appendToFooter(page.contents, dateTime);
page.contents = appendToFooter(page.contents, time);
}

// Set custom title.
Expand Down

0 comments on commit dc4cee6

Please sign in to comment.