-
Notifications
You must be signed in to change notification settings - Fork 156
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #320 from ekalinin/simple-sitemap
simple sitemap, bugfixes
- Loading branch information
Showing
9 changed files
with
243 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
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,66 @@ | ||
import { | ||
SitemapAndIndexStream, | ||
SitemapStream, | ||
lineSeparatedURLsToSitemapOptions, | ||
} from '../index'; | ||
import { createGzip } from 'zlib'; | ||
import { createWriteStream, createReadStream } from 'fs'; | ||
import { resolve } from 'path'; | ||
import { Readable, pipeline as pline } from 'stream'; | ||
import { SitemapItemLoose } from './types'; | ||
import { promisify } from 'util'; | ||
import { URL } from 'url'; | ||
|
||
const pipeline = promisify(pline); | ||
export const simpleSitemapAndIndex = ({ | ||
hostname, | ||
sitemapHostname = hostname, // if different | ||
/** | ||
* Pass a line separated list of sitemap items or a stream or an array | ||
*/ | ||
sourceData, | ||
destinationDir, | ||
limit = 50000, | ||
}: { | ||
hostname: string; | ||
sitemapHostname?: string; | ||
sourceData: SitemapItemLoose | string | Readable | string[]; | ||
destinationDir: string; | ||
limit?: number; | ||
}): Promise<void> => { | ||
const sitemapAndIndexStream = new SitemapAndIndexStream({ | ||
limit, | ||
getSitemapStream: (i) => { | ||
const sitemapStream = new SitemapStream({ | ||
hostname, | ||
}); | ||
const path = `./sitemap-${i}.xml`; | ||
|
||
sitemapStream | ||
.pipe(createGzip()) // compress the output of the sitemap | ||
.pipe(createWriteStream(resolve(destinationDir, path + '.gz'))); // write it to sitemap-NUMBER.xml | ||
|
||
return [new URL(path, sitemapHostname).toString(), sitemapStream]; | ||
}, | ||
}); | ||
let src: Readable; | ||
if (typeof sourceData === 'string') { | ||
src = lineSeparatedURLsToSitemapOptions(createReadStream(sourceData)); | ||
} else if (sourceData instanceof Readable) { | ||
src = sourceData; | ||
} else if (Array.isArray(sourceData)) { | ||
src = Readable.from(sourceData); | ||
} else { | ||
throw new Error( | ||
"unhandled source type. You've passed in data that is not supported" | ||
); | ||
} | ||
return pipeline( | ||
src, | ||
sitemapAndIndexStream, | ||
createGzip(), | ||
createWriteStream(resolve(destinationDir, './sitemap-index.xml.gz')) | ||
); | ||
}; | ||
|
||
export default simpleSitemapAndIndex; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Oops, something went wrong.