File tree 2 files changed +32
-3
lines changed
2 files changed +32
-3
lines changed Original file line number Diff line number Diff line change @@ -173,7 +173,16 @@ export async function renderPage(
173
173
</html>` . trim ( )
174
174
const htmlFileName = path . join ( config . outDir , page . replace ( / \. m d $ / , '.html' ) )
175
175
await fs . ensureDir ( path . dirname ( htmlFileName ) )
176
- await fs . writeFile ( htmlFileName , html )
176
+ const transformedHtml = await config . transformHtml ?.( html , htmlFileName , {
177
+ siteConfig : config ,
178
+ siteData,
179
+ pageData,
180
+ title,
181
+ description,
182
+ head,
183
+ content
184
+ } )
185
+ await fs . writeFile ( htmlFileName , transformedHtml || html )
177
186
}
178
187
179
188
function resolvePageImports (
Original file line number Diff line number Diff line change @@ -16,7 +16,8 @@ import {
16
16
LocaleConfig ,
17
17
DefaultTheme ,
18
18
APPEARANCE_KEY ,
19
- createLangDictionary
19
+ createLangDictionary ,
20
+ PageData
20
21
} from './shared'
21
22
import { resolveAliases , DEFAULT_THEME_PATH } from './alias'
22
23
import { MarkdownOptions } from './markdown/markdown'
@@ -77,6 +78,23 @@ export interface UserConfig<ThemeConfig = any> {
77
78
* @param siteConfig The resolved configuration.
78
79
*/
79
80
buildEnd ?: ( siteConfig : SiteConfig ) => Promise < void >
81
+
82
+ /**
83
+ * HTML transform hook: runs before writing HTML to dist.
84
+ */
85
+ transformHtml ?: (
86
+ code : string ,
87
+ id : string ,
88
+ ctx : {
89
+ siteConfig : SiteConfig
90
+ siteData : SiteData
91
+ pageData : PageData
92
+ title : string
93
+ description : string
94
+ head : HeadConfig [ ]
95
+ content : string
96
+ }
97
+ ) => Promise < string | void >
80
98
}
81
99
82
100
export type RawConfigExports < ThemeConfig = any > =
@@ -95,6 +113,7 @@ export interface SiteConfig<ThemeConfig = any>
95
113
| 'lastUpdated'
96
114
| 'ignoreDeadLinks'
97
115
| 'buildEnd'
116
+ | 'transformHtml'
98
117
> {
99
118
root : string
100
119
srcDir : string
@@ -174,7 +193,8 @@ export async function resolveConfig(
174
193
shouldPreload : userConfig . shouldPreload ,
175
194
mpa : ! ! userConfig . mpa ,
176
195
ignoreDeadLinks : userConfig . ignoreDeadLinks ,
177
- buildEnd : userConfig . buildEnd
196
+ buildEnd : userConfig . buildEnd ,
197
+ transformHtml : userConfig . transformHtml
178
198
}
179
199
180
200
return config
You can’t perform that action at this time.
0 commit comments