-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
Showing
4 changed files
with
82 additions
and
6 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
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 |
---|---|---|
@@ -0,0 +1,69 @@ | ||
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 { SitemapAndIndexStream, SitemapStream } from 'sitemap'; | ||
|
||
import type { AstroConfig } from 'astro'; | ||
import type { SitemapItem } from "./index.js"; | ||
|
||
type WriteSitemapConfig = { | ||
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) { | ||
|
||
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); | ||
|
||
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)); | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.