1
+ import { type WriteStream , createWriteStream } from 'fs' ;
1
2
import { normalize , resolve } from 'path' ;
2
- import { createWriteStream , type WriteStream } from 'fs'
3
- import { mkdir } from 'fs/promises' ;
4
- import { promisify } from 'util' ;
5
3
import { Readable , pipeline } from 'stream' ;
6
- import replace from 'stream-replace-string'
4
+ import { promisify } from 'util' ;
5
+ import { mkdir } from 'fs/promises' ;
6
+ import replace from 'stream-replace-string' ;
7
7
8
8
import { SitemapAndIndexStream , SitemapStream } from 'sitemap' ;
9
9
10
10
import type { AstroConfig } from 'astro' ;
11
- import type { SitemapItem } from " ./index.js" ;
11
+ import type { SitemapItem } from ' ./index.js' ;
12
12
13
13
type WriteSitemapConfig = {
14
- hostname : string ;
15
- sitemapHostname ?: string ;
16
- sourceData : SitemapItem [ ] ;
17
- destinationDir : string ;
18
- publicBasePath ?: string ;
19
- limit ?: number ;
20
- }
14
+ hostname : string ;
15
+ sitemapHostname ?: string ;
16
+ sourceData : SitemapItem [ ] ;
17
+ destinationDir : string ;
18
+ publicBasePath ?: string ;
19
+ limit ?: number ;
20
+ } ;
21
21
22
22
// adapted from sitemap.js/sitemap-simple
23
- export async function writeSitemap ( { hostname, sitemapHostname = hostname ,
24
- sourceData, destinationDir, limit = 50000 , publicBasePath = './' , } : WriteSitemapConfig , astroConfig : AstroConfig ) {
23
+ export async function writeSitemap (
24
+ {
25
+ hostname,
26
+ sitemapHostname = hostname ,
27
+ sourceData,
28
+ destinationDir,
29
+ limit = 50000 ,
30
+ publicBasePath = './' ,
31
+ } : WriteSitemapConfig ,
32
+ astroConfig : AstroConfig
33
+ ) {
34
+ await mkdir ( destinationDir , { recursive : true } ) ;
35
+
36
+ const sitemapAndIndexStream = new SitemapAndIndexStream ( {
37
+ limit,
38
+ getSitemapStream : ( i ) => {
39
+ const sitemapStream = new SitemapStream ( {
40
+ hostname,
41
+ } ) ;
42
+ const path = `./sitemap-${ i } .xml` ;
43
+ const writePath = resolve ( destinationDir , path ) ;
44
+ if ( ! publicBasePath . endsWith ( '/' ) ) {
45
+ publicBasePath += '/' ;
46
+ }
47
+ const publicPath = normalize ( publicBasePath + path ) ;
25
48
26
- await mkdir ( destinationDir , { recursive : true } )
49
+ let stream : WriteStream ;
50
+ if ( astroConfig . trailingSlash === 'never' || astroConfig . build . format === 'file' ) {
51
+ // workaround for trailing slash issue in sitemap.js: https://github.com/ekalinin/sitemap.js/issues/403
52
+ const host = hostname . endsWith ( '/' ) ? hostname . slice ( 0 , - 1 ) : hostname ;
53
+ const searchStr = `<loc>${ host } /</loc>` ;
54
+ const replaceStr = `<loc>${ host } </loc>` ;
55
+ stream = sitemapStream
56
+ . pipe ( replace ( searchStr , replaceStr ) )
57
+ . pipe ( createWriteStream ( writePath ) ) ;
58
+ } else {
59
+ stream = sitemapStream . pipe ( createWriteStream ( writePath ) ) ;
60
+ }
27
61
28
- const sitemapAndIndexStream = new SitemapAndIndexStream ( {
29
- limit,
30
- getSitemapStream : ( i ) => {
31
- const sitemapStream = new SitemapStream ( {
32
- hostname,
33
- } ) ;
34
- const path = `./sitemap-${ i } .xml` ;
35
- const writePath = resolve ( destinationDir , path ) ;
36
- if ( ! publicBasePath . endsWith ( '/' ) ) {
37
- publicBasePath += '/' ;
38
- }
39
- const publicPath = normalize ( publicBasePath + path ) ;
62
+ return [ new URL ( publicPath , sitemapHostname ) . toString ( ) , sitemapStream , stream ] ;
63
+ } ,
64
+ } ) ;
40
65
41
- let stream : WriteStream
42
- if ( astroConfig . trailingSlash === 'never' || astroConfig . build . format === 'file' ) {
43
- // workaround for trailing slash issue in sitemap.js: https://github.com/ekalinin/sitemap.js/issues/403
44
- const host = hostname . endsWith ( '/' ) ? hostname . slice ( 0 , - 1 ) : hostname
45
- const searchStr = `<loc>${ host } /</loc>`
46
- const replaceStr = `<loc>${ host } </loc>`
47
- stream = sitemapStream . pipe ( replace ( searchStr , replaceStr ) ) . pipe ( createWriteStream ( writePath ) )
48
- } else {
49
- stream = sitemapStream . pipe ( createWriteStream ( writePath ) )
50
- }
51
-
52
- return [
53
- new URL (
54
- publicPath ,
55
- sitemapHostname
56
- ) . toString ( ) ,
57
- sitemapStream ,
58
- stream ,
59
- ] ;
60
- } ,
61
- } ) ;
62
-
63
- let src = Readable . from ( sourceData )
64
- const indexPath = resolve (
65
- destinationDir ,
66
- `./sitemap-index.xml`
67
- ) ;
68
- return promisify ( pipeline ) ( src , sitemapAndIndexStream , createWriteStream ( indexPath ) ) ;
69
- }
66
+ let src = Readable . from ( sourceData ) ;
67
+ const indexPath = resolve ( destinationDir , `./sitemap-index.xml` ) ;
68
+ return promisify ( pipeline ) ( src , sitemapAndIndexStream , createWriteStream ( indexPath ) ) ;
69
+ }
0 commit comments