-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0e22462
commit 5750ad1
Showing
2 changed files
with
67 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,69 +1,69 @@ | ||
import { type WriteStream, createWriteStream } from 'fs'; | ||
import { normalize, resolve } from 'path'; | ||
import { createWriteStream, type WriteStream } from 'fs' | ||
import { mkdir } from 'fs/promises'; | ||
import { promisify } from 'util'; | ||
import { Readable, pipeline } from 'stream'; | ||
import replace from 'stream-replace-string' | ||
import { promisify } from 'util'; | ||
import { mkdir } from 'fs/promises'; | ||
import replace from 'stream-replace-string'; | ||
|
||
import { SitemapAndIndexStream, SitemapStream } from 'sitemap'; | ||
|
||
import type { AstroConfig } from 'astro'; | ||
import type { SitemapItem } from "./index.js"; | ||
import type { SitemapItem } from './index.js'; | ||
|
||
type WriteSitemapConfig = { | ||
hostname: string; | ||
sitemapHostname?: string; | ||
sourceData: SitemapItem[]; | ||
destinationDir: string; | ||
publicBasePath?: string; | ||
limit?: number; | ||
} | ||
hostname: string; | ||
sitemapHostname?: string; | ||
sourceData: SitemapItem[]; | ||
destinationDir: string; | ||
publicBasePath?: string; | ||
limit?: number; | ||
}; | ||
|
||
// adapted from sitemap.js/sitemap-simple | ||
export async function writeSitemap({ hostname, sitemapHostname = hostname, | ||
sourceData, destinationDir, limit = 50000, publicBasePath = './', }: WriteSitemapConfig, astroConfig: AstroConfig) { | ||
export async function writeSitemap( | ||
{ | ||
hostname, | ||
sitemapHostname = hostname, | ||
sourceData, | ||
destinationDir, | ||
limit = 50000, | ||
publicBasePath = './', | ||
}: WriteSitemapConfig, | ||
astroConfig: AstroConfig | ||
) { | ||
await mkdir(destinationDir, { recursive: true }); | ||
|
||
const sitemapAndIndexStream = new SitemapAndIndexStream({ | ||
limit, | ||
getSitemapStream: (i) => { | ||
const sitemapStream = new SitemapStream({ | ||
hostname, | ||
}); | ||
const path = `./sitemap-${i}.xml`; | ||
const writePath = resolve(destinationDir, path); | ||
if (!publicBasePath.endsWith('/')) { | ||
publicBasePath += '/'; | ||
} | ||
const publicPath = normalize(publicBasePath + path); | ||
|
||
await mkdir(destinationDir, { recursive: true }) | ||
let stream: WriteStream; | ||
if (astroConfig.trailingSlash === 'never' || astroConfig.build.format === 'file') { | ||
// workaround for trailing slash issue in sitemap.js: https://github.com/ekalinin/sitemap.js/issues/403 | ||
const host = hostname.endsWith('/') ? hostname.slice(0, -1) : hostname; | ||
const searchStr = `<loc>${host}/</loc>`; | ||
const replaceStr = `<loc>${host}</loc>`; | ||
stream = sitemapStream | ||
.pipe(replace(searchStr, replaceStr)) | ||
.pipe(createWriteStream(writePath)); | ||
} else { | ||
stream = sitemapStream.pipe(createWriteStream(writePath)); | ||
} | ||
|
||
const sitemapAndIndexStream = new SitemapAndIndexStream({ | ||
limit, | ||
getSitemapStream: (i) => { | ||
const sitemapStream = new SitemapStream({ | ||
hostname, | ||
}); | ||
const path = `./sitemap-${i}.xml`; | ||
const writePath = resolve(destinationDir, path); | ||
if (!publicBasePath.endsWith('/')) { | ||
publicBasePath += '/'; | ||
} | ||
const publicPath = normalize(publicBasePath + path); | ||
return [new URL(publicPath, sitemapHostname).toString(), sitemapStream, stream]; | ||
}, | ||
}); | ||
|
||
let stream: WriteStream | ||
if (astroConfig.trailingSlash === 'never' || astroConfig.build.format === 'file') { | ||
// workaround for trailing slash issue in sitemap.js: https://github.com/ekalinin/sitemap.js/issues/403 | ||
const host = hostname.endsWith('/') ? hostname.slice(0, -1) : hostname | ||
const searchStr = `<loc>${host}/</loc>` | ||
const replaceStr = `<loc>${host}</loc>` | ||
stream = sitemapStream.pipe(replace(searchStr, replaceStr)).pipe(createWriteStream(writePath)) | ||
} else { | ||
stream = sitemapStream.pipe(createWriteStream(writePath)) | ||
} | ||
|
||
return [ | ||
new URL( | ||
publicPath, | ||
sitemapHostname | ||
).toString(), | ||
sitemapStream, | ||
stream, | ||
]; | ||
}, | ||
}); | ||
|
||
let src = Readable.from(sourceData) | ||
const indexPath = resolve( | ||
destinationDir, | ||
`./sitemap-index.xml` | ||
); | ||
return promisify(pipeline)(src, sitemapAndIndexStream, createWriteStream(indexPath)); | ||
} | ||
let src = Readable.from(sourceData); | ||
const indexPath = resolve(destinationDir, `./sitemap-index.xml`); | ||
return promisify(pipeline)(src, sitemapAndIndexStream, createWriteStream(indexPath)); | ||
} |