@@ -86,10 +86,6 @@ export default defineConfig({
8686 // other locale specific properties...
8787 } ,
8888 } ,
89- //Logs every page loaded on build. Good way to catch errors not caught by other things.
90- async transformPageData ( pageData , { siteConfig } ) {
91- console . log ( pageData . filePath ) ;
92- } ,
9389
9490 //
9591
@@ -171,6 +167,58 @@ export default defineConfig({
171167 ] ,
172168 } ,
173169
170+ //Logs every page loaded on build. Good way to catch errors not caught by other things.
171+ async transformPageData ( pageData , { siteConfig } ) {
172+ console . log ( pageData . filePath ) ;
173+ } ,
174+
175+ async transformHead ( { pageData } ) {
176+ // Start with an empty array to accumulate all head tags
177+ const head = [ ] ;
178+
179+ let canonicalUrlToAdd ; // This will be undefined initially
180+
181+ // Get value from frontmatter if defined
182+ // Assumed to be an absolute URL in the frontmatter
183+ const frontmatterCanonicalUrl = pageData . frontmatter ?. canonicalUrl ;
184+ if ( frontmatterCanonicalUrl ) {
185+ canonicalUrlToAdd = frontmatterCanonicalUrl ;
186+ } else {
187+ // No frontmatter override, generate default based on site config
188+ // Hostname and base path used for adding canonical URLs to pages
189+ const hostname = "https://docs.px4.io/main/" ;
190+
191+ let path = pageData . relativePath . replace ( / \. m d $ / , "" ) ;
192+
193+ if ( path === "index" ) {
194+ path = "" ; // For the homepage (index.md), the path is empty
195+ } else if ( path . endsWith ( "/index" ) ) {
196+ path = path . slice ( 0 , - "/index" . length ) ; // For directory index pages (e.g., /my-folder/index.md -> /my-folder/)
197+ }
198+
199+ // Ensure fullPath does not start with a slash (makes it relative to the root)
200+ const fullPath = path . startsWith ( "/" ) ? path . slice ( 1 ) : path ;
201+ // Construct the default canonical URL using hostname with main base and path
202+ try {
203+ const url = new URL ( fullPath , hostname ) ;
204+ canonicalUrlToAdd = url . href ;
205+ } catch ( error ) {
206+ // Fallback, though less robust
207+ canonicalUrlToAdd = `${ hostname } ${ fullPath } ` ;
208+ }
209+ }
210+
211+ // Add canonical link to accumulated head array
212+ if ( canonicalUrlToAdd ) {
213+ head . push ( [ "link" , { rel : "canonical" , href : canonicalUrlToAdd } ] ) ;
214+ }
215+
216+ // Add any other custom head tags you might want later
217+
218+ // Return head that will be merged.
219+ return head ;
220+ } ,
221+
174222 head : [
175223 [
176224 "script" ,
0 commit comments