mirror of
https://github.com/withastro/astro.git
synced 2025-01-13 22:11:20 -05:00
[ci] format
This commit is contained in:
parent
0e22462d15
commit
5750ad12be
2 changed files with 67 additions and 64 deletions
|
@ -4,8 +4,8 @@ import type { AstroConfig, AstroIntegration } from 'astro';
|
|||
import type { EnumChangefreq, LinkItem as LinkItemBase, SitemapItemLoose } from 'sitemap';
|
||||
import { ZodError } from 'zod';
|
||||
|
||||
import { validateOptions } from './validate-options.js';
|
||||
import { generateSitemap } from './generate-sitemap.js';
|
||||
import { validateOptions } from './validate-options.js';
|
||||
import { writeSitemap } from './write-sitemap.js';
|
||||
|
||||
export { EnumChangefreq as ChangeFreqEnum } from 'sitemap';
|
||||
|
@ -167,13 +167,16 @@ const createPlugin = (options?: SitemapOptions): AstroIntegration => {
|
|||
}
|
||||
}
|
||||
const destDir = fileURLToPath(dir);
|
||||
await writeSitemap({
|
||||
hostname: finalSiteUrl.href,
|
||||
destinationDir: destDir,
|
||||
publicBasePath: config.base,
|
||||
sourceData: urlData,
|
||||
limit: entryLimit
|
||||
}, config)
|
||||
await writeSitemap(
|
||||
{
|
||||
hostname: finalSiteUrl.href,
|
||||
destinationDir: destDir,
|
||||
publicBasePath: config.base,
|
||||
sourceData: urlData,
|
||||
limit: entryLimit,
|
||||
},
|
||||
config
|
||||
);
|
||||
logger.info(`\`${OUTFILE}\` created at \`${path.relative(process.cwd(), destDir)}\``);
|
||||
} catch (err) {
|
||||
if (err instanceof ZodError) {
|
||||
|
|
|
@ -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 });
|
||||
|
||||
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);
|
||||
|
||||
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));
|
||||
}
|
||||
|
||||
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));
|
||||
}
|
||||
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));
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue