From 716794dfece472772bb3b2dd3e4a1c3b0dc682e9 Mon Sep 17 00:00:00 2001 From: Divyansh Singh <40380293+brc-dd@users.noreply.github.com> Date: Wed, 20 Jul 2022 13:19:57 +0530 Subject: [PATCH 1/2] feat: provide transformHtml hook --- src/node/build/render.ts | 11 ++++++++++- src/node/config.ts | 24 ++++++++++++++++++++++-- 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/src/node/build/render.ts b/src/node/build/render.ts index a03729e5f421..0b7db3f84fd1 100644 --- a/src/node/build/render.ts +++ b/src/node/build/render.ts @@ -173,7 +173,16 @@ export async function renderPage( `.trim() const htmlFileName = path.join(config.outDir, page.replace(/\.md$/, '.html')) await fs.ensureDir(path.dirname(htmlFileName)) - await fs.writeFile(htmlFileName, html) + const transformedHtml = await config.transformHtml?.(html, htmlFileName, { + config, + siteData, + pageData, + title, + description, + head, + content + }) + await fs.writeFile(htmlFileName, transformedHtml || html) } function resolvePageImports( diff --git a/src/node/config.ts b/src/node/config.ts index 1fac1d1b083a..bff950899213 100644 --- a/src/node/config.ts +++ b/src/node/config.ts @@ -16,7 +16,8 @@ import { LocaleConfig, DefaultTheme, APPEARANCE_KEY, - createLangDictionary + createLangDictionary, + PageData } from './shared' import { resolveAliases, DEFAULT_THEME_PATH } from './alias' import { MarkdownOptions } from './markdown/markdown' @@ -77,6 +78,23 @@ export interface UserConfig { * @param siteConfig The resolved configuration. */ buildEnd?: (siteConfig: SiteConfig) => Promise + + /** + * HTML transform hook: runs before writing HTML to dist. + */ + transformHtml?: ( + code: string, + id: string, + ctx: { + config: SiteConfig + siteData: SiteData + pageData: PageData + title: string + description: string + head: HeadConfig[] + content: string + } + ) => Promise } export type RawConfigExports = @@ -95,6 +113,7 @@ export interface SiteConfig | 'lastUpdated' | 'ignoreDeadLinks' | 'buildEnd' + | 'transformHtml' > { root: string srcDir: string @@ -174,7 +193,8 @@ export async function resolveConfig( shouldPreload: userConfig.shouldPreload, mpa: !!userConfig.mpa, ignoreDeadLinks: userConfig.ignoreDeadLinks, - buildEnd: userConfig.buildEnd + buildEnd: userConfig.buildEnd, + transformHtml: userConfig.transformHtml } return config From dca85310543219706a8912a9f2c25100cec725ec Mon Sep 17 00:00:00 2001 From: Divyansh Singh <40380293+brc-dd@users.noreply.github.com> Date: Wed, 20 Jul 2022 14:27:32 +0530 Subject: [PATCH 2/2] Apply suggestions from code review --- src/node/build/render.ts | 2 +- src/node/config.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/node/build/render.ts b/src/node/build/render.ts index 0b7db3f84fd1..c38af2a3a216 100644 --- a/src/node/build/render.ts +++ b/src/node/build/render.ts @@ -174,7 +174,7 @@ export async function renderPage( const htmlFileName = path.join(config.outDir, page.replace(/\.md$/, '.html')) await fs.ensureDir(path.dirname(htmlFileName)) const transformedHtml = await config.transformHtml?.(html, htmlFileName, { - config, + siteConfig: config, siteData, pageData, title, diff --git a/src/node/config.ts b/src/node/config.ts index bff950899213..28ecdec87af8 100644 --- a/src/node/config.ts +++ b/src/node/config.ts @@ -86,7 +86,7 @@ export interface UserConfig { code: string, id: string, ctx: { - config: SiteConfig + siteConfig: SiteConfig siteData: SiteData pageData: PageData title: string