import type * as d from '../../declarations' ; import { catchError } from '@utils' ; import { join } from 'path' ; export const generateSitemapXml = async ( manager ) => { if ( manager . prerenderConfig . sitemapXml === null ) { return null ; } try { if ( typeof manager . prerenderConfig . sitemapXml !== 'function' ) { manager . prerenderConfig . sitemapXml = function sitemapXml ( opts ) { const content = [ ] ; content . push ( `<?xml version="1.0" encoding="UTF-8"?>` ) ; content . push ( `<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">` ) ; for ( const url of opts . urls ) { content . push ( `<url><loc>${ url }</loc></url>` ) ; } content . push ( `</urlset>` ) ; return content . join ( '\n' ) ; } ; } const opts = { urls : getSitemapUrls ( manager ) , baseUrl : manager . outputTarget . baseUrl , dir : manager . outputTarget . appDir , } ; const userResults = manager . prerenderConfig . sitemapXml ( opts ) ; if ( userResults == null ) { return null ; } const results = { content : null , filePath : null , url : null , } ; if ( typeof userResults === 'string' ) { results . content = userResults ; } else { results . content = userResults . content ; results . filePath = userResults . filePath ; } if ( typeof results . content !== 'string' ) { return null ; } if ( typeof results . filePath !== 'string' ) { results . filePath = join ( manager . outputTarget . appDir , `sitemap.xml` ) ; } if ( typeof results . url !== 'string' ) { const sitemapUrl = new URL ( `sitemap.xml` , manager . outputTarget . baseUrl ) ; results . url = sitemapUrl . href ; } await manager . config . sys . writeFile ( results . filePath , results . content ) ; return results ; } catch ( e ) { catchError ( manager . diagnostics , e ) ; return null ; } } ; export const getSitemapUrls = ( manager ) => { const urls = [ ] ; if ( typeof manager . prerenderConfig . canonicalUrl === 'function' ) { for ( const url of manager . urlsCompleted ) { const canonicalUrl = manager . prerenderConfig . canonicalUrl ( new URL ( url ) ) ; if ( typeof canonicalUrl === 'string' && canonicalUrl . trim ( ) !== '' ) { urls . push ( canonicalUrl ) ; } } } else { for ( const url of manager . urlsCompleted ) { if ( typeof url === 'string' ) { urls . push ( url ) ; } } } return urls . sort ( sortUrls ) ; } ; const sortUrls = ( a , b ) => { const partsA = a . split ( '/' ) . length ; const partsB = b . split ( '/' ) . length ; if ( partsA < partsB ) return - 1 ; if ( partsA > partsB ) return 1 ; if ( a < b ) return - 1 ; if ( a > b ) return 1 ; return 0 ; } ;