Skip to content

Commit

Permalink
feat: provide transformHtml hook (#1022)
Browse files Browse the repository at this point in the history
  • Loading branch information
brc-dd committed Jul 20, 2022
1 parent 4b38736 commit 2b4b800
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
11 changes: 10 additions & 1 deletion src/node/build/render.ts
Expand Up @@ -173,7 +173,16 @@ export async function renderPage(
</html>`.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, {
siteConfig: config,
siteData,
pageData,
title,
description,
head,
content
})
await fs.writeFile(htmlFileName, transformedHtml || html)
}

function resolvePageImports(
Expand Down
24 changes: 22 additions & 2 deletions src/node/config.ts
Expand Up @@ -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'
Expand Down Expand Up @@ -77,6 +78,23 @@ export interface UserConfig<ThemeConfig = any> {
* @param siteConfig The resolved configuration.
*/
buildEnd?: (siteConfig: SiteConfig) => Promise<void>

/**
* HTML transform hook: runs before writing HTML to dist.
*/
transformHtml?: (
code: string,
id: string,
ctx: {
siteConfig: SiteConfig
siteData: SiteData
pageData: PageData
title: string
description: string
head: HeadConfig[]
content: string
}
) => Promise<string | void>
}

export type RawConfigExports<ThemeConfig = any> =
Expand All @@ -95,6 +113,7 @@ export interface SiteConfig<ThemeConfig = any>
| 'lastUpdated'
| 'ignoreDeadLinks'
| 'buildEnd'
| 'transformHtml'
> {
root: string
srcDir: string
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 2b4b800

Please sign in to comment.