Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

astro-sitemap

Package Overview
Dependencies
Maintainers
1
Versions
19
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

astro-sitemap - npm Package Compare versions

Comparing version 0.2.4 to 0.2.7

11

dist/index.d.ts
import type { AstroIntegration } from 'astro';
import { SitemapItemLoose, EnumChangefreq as ChangeFreq } from 'sitemap';
export { SitemapItemLoose, LinkItem, EnumChangefreq as ChangeFreq } from 'sitemap';
import { SitemapItemLoose as SitemapItemLooseBase, EnumChangefreq, LinkItem as LinkItemBase } from 'sitemap';
export declare type LinkItem = LinkItemBase;
export declare type ChangeFreq = EnumChangefreq;
export declare type SitemapItemLoose = SitemapItemLooseBase;
export declare type SitemapItem = Pick<SitemapItemLoose, 'url' | 'lastmod' | 'changefreq' | 'priority' | 'links'>;

@@ -13,5 +15,6 @@ export interface NSArgs {

export declare type SitemapOptions = {
canonicalURL?: string;
customPages?: string[];
filter?(page: string): boolean;
customPages?: string[];
canonicalURL?: string;
exclude?: string[];
i18n?: {

@@ -18,0 +21,0 @@ defaultLocale: string;

@@ -22,4 +22,8 @@ "use strict";

}
toArray(msg) {
return Array.isArray(msg) ? msg : [msg];
}
log(msg, prefix = "") {
console.log(`%s${this.packageName}:%s ${msg}
const s = Array.isArray(msg) ? msg.join("\n") : msg;
console.log(`%s${this.packageName}:%s ${s}
`, prefix, prefix ? this.colors.reset : "");

@@ -34,8 +38,6 @@ }

warn(msg) {
this.log(`Skipped!
${msg}`, this.colors.fg.yellow);
this.log(["Skipped!", ...this.toArray(msg)], this.colors.fg.yellow);
}
error(msg) {
this.log(`Failed!
${msg}`, this.colors.fg.red);
this.log(["Failed!", ...this.toArray(msg)], this.colors.fg.red);
}

@@ -78,2 +80,7 @@ };

// ../utils/src/error-helpers.ts
function getErrorMessage(err) {
return err instanceof Error ? err.message : String(err);
}
// src/validate-options.ts

@@ -97,6 +104,10 @@ import { z as z2 } from "zod";

var localeKeySchema = z.string().min(1);
var changefreqSchema = z.nativeEnum(ChangeFreq).optional();
var lastmodSchema = z.date().optional();
var prioritySchema = z.number().min(0).max(1).optional();
var SitemapOptionsSchema = z.object({
canonicalURL: z.string().url().optional(),
filter: z.function().args(z.string()).returns(z.boolean()).optional(),
exclude: z.string().min(1).array().optional(),
customPages: z.string().url().array().optional(),
canonicalURL: z.string().url().optional(),
i18n: z.object({

@@ -121,5 +132,5 @@ defaultLocale: localeKeySchema,

serialize: z.function().args(z.any()).returns(z.any()).optional(),
changefreq: z.nativeEnum(ChangeFreq).optional(),
lastmod: z.date().optional(),
priority: z.number().min(0).max(1).optional(),
changefreq: changefreqSchema,
lastmod: lastmodSchema,
priority: prioritySchema,
createLinkInHead: z.boolean().optional().default(SITEMAP_CONFIG_DEFAULTS.createLinkInHead)

@@ -219,3 +230,3 @@ }).strict().default(SITEMAP_CONFIG_DEFAULTS);

import { normalize, resolve } from "path";
import { Readable, pipeline as pline } from "stream";
import { Readable, pipeline } from "stream";
import { createGzip } from "zlib";

@@ -230,3 +241,3 @@ import { promisify } from "util";

// src/sitemap/sitemap-simple-extended.ts
var pipeline = promisify(pline);
var pipelineAsync = promisify(pipeline);
var simpleSitemapAndIndexExtended = async ({

@@ -239,7 +250,9 @@ hostname,

publicBasePath = "./",
gzip = false,
lastmodDateOnly = false,
xslUrl = void 0,
xmlns
xmlns,
gzip = false
}) => {
const result = [];
const suffix = gzip ? ".gz" : "";
await promises.mkdir(destinationDir, { recursive: true });

@@ -250,3 +263,3 @@ const sitemapAndIndexStream = new SitemapAndIndexStream({

xslUrl,
getSitemapStream: (i) => {
getSitemapStream(i) {
const sitemapStream = new SitemapStream({

@@ -258,8 +271,9 @@ hostname,

});
const path2 = SITEMAP_CHUNK_TEMPLATE.replace("%d", i.toString(10));
const writePath2 = resolve(destinationDir, path2 + (gzip ? ".gz" : ""));
const chunkName = SITEMAP_CHUNK_TEMPLATE.replace("%d", i.toString(10)) + suffix;
result.push(chunkName);
const writePath2 = resolve(destinationDir, chunkName);
if (!publicBasePath.endsWith("/")) {
publicBasePath += "/";
}
const publicPath = normalize(publicBasePath + path2);
const publicPath = normalize(publicBasePath + chunkName);
let pipeline2;

@@ -271,12 +285,15 @@ if (gzip) {

}
return [new URL2(`${publicPath}${gzip ? ".gz" : ""}`, sitemapHostname).toString(), sitemapStream, pipeline2];
return [new URL2(publicPath, sitemapHostname).toString(), sitemapStream, pipeline2];
}
});
const src = Readable.from(sourceData);
const writePath = resolve(destinationDir, `./${SITEMAP_INDEX_FILE_NAME}${gzip ? ".gz" : ""}`);
const indexName = SITEMAP_INDEX_FILE_NAME + suffix;
const writePath = resolve(destinationDir, `./${indexName}`);
if (gzip) {
return pipeline(src, sitemapAndIndexStream, createGzip(), createWriteStream(writePath));
await pipelineAsync(src, sitemapAndIndexStream, createGzip(), createWriteStream(writePath));
} else {
return pipeline(src, sitemapAndIndexStream, createWriteStream(writePath));
await pipelineAsync(src, sitemapAndIndexStream, createWriteStream(writePath));
}
result.unshift(indexName);
return result;
};

@@ -319,2 +336,13 @@

// src/helpers/exclude-routes.ts
import mmpkg from "minimatch";
function excludeRoutes(patterns, routes) {
patterns.forEach((pattern) => {
const minimatch = new mmpkg.Minimatch(pattern);
minimatch.negate = true;
routes = routes.filter((path2) => minimatch.match(path2));
});
return routes;
}
// src/data/pkg-name.ts

@@ -324,3 +352,3 @@ var packageName = "astro-sitemap";

// src/index.ts
import { SitemapItemLoose as SitemapItemLoose2, LinkItem, EnumChangefreq } from "sitemap";
var logger = new Logger(packageName);
function formatConfigErrorMessage(err) {

@@ -330,11 +358,2 @@ const errorList = err.issues.map((issue) => `${issue.path.join(".")} ${issue.message + "."}`);

}
var logger = new Logger(packageName);
var isEmptyData = (n) => {
if (n === 0) {
logger.warn(`No data for sitemap.
\`${SITEMAP_INDEX_FILE_NAME}\` is not created.`);
return true;
}
return false;
};
var createPlugin = (options) => {

@@ -348,3 +367,3 @@ let config;

},
"astro:build:done": async ({ dir, pages }) => {
"astro:build:done": async ({ dir, pages: srcPages }) => {
const namespace = packageName.replace("astro-", "");

@@ -355,3 +374,3 @@ const external = await loadConfig(namespace, config.root);

const opts = validateOptions(config.site, merged);
const { filter, customPages, canonicalURL, serialize, createLinkInHead, entryLimit, lastmodDateOnly, xslUrl, xmlns } = opts;
const { filter, exclude, customPages, canonicalURL, entryLimit, lastmodDateOnly, xslUrl, xmlns, serialize, createLinkInHead } = opts;
let finalSiteUrl;

@@ -366,19 +385,37 @@ if (canonicalURL) {

}
let pageUrls = pages.map((p) => {
const path2 = finalSiteUrl.pathname + p.pathname;
let pages = srcPages.map(({ pathname }) => pathname);
if (exclude && exclude.length > 0) {
try {
pages = excludeRoutes(exclude, pages);
} catch (err) {
logger.error(["Error exclude pages", getErrorMessage(err)]);
return;
}
}
if (filter) {
try {
pages = pages.filter(filter);
} catch (err) {
logger.error(["Error filtering pages", getErrorMessage(err)]);
return;
}
}
let pageUrls = pages.map((pathname) => {
const path2 = finalSiteUrl.pathname + pathname;
return new URL(path2, finalSiteUrl).href;
});
try {
if (filter) {
pageUrls = pageUrls.filter(filter);
}
} catch (err) {
logger.error(`Error filtering pages
${err.toString()}`);
return;
}
if (customPages) {
pageUrls = [...pageUrls, ...customPages];
}
if (isEmptyData(pageUrls.length)) {
if (pageUrls.length === 0) {
if (typeof config.adapter !== "undefined") {
logger.warn([
"No pages found!",
'`We can only detect sitemap routes for "static" projects. Since you are using an SSR adapter, we recommend manually listing your sitemap routes using the "customPages" integration option.',
"",
"Example: `sitemap({ customPages: ['https://example.com/route'] })`"
]);
} else {
logger.warn("No pages found!");
}
return;

@@ -396,3 +433,4 @@ }

}
if (isEmptyData(serializedUrls.length)) {
if (serializedUrls.length === 0) {
logger.warn("No pages found!");
return;

@@ -402,8 +440,7 @@ }

} catch (err) {
logger.error(`Error serializing pages
${err.toString()}`);
logger.error(["Error serializing pages", getErrorMessage(err)]);
return;
}
}
await simpleSitemapAndIndexExtended({
const fileNames = await simpleSitemapAndIndexExtended({
hostname: finalSiteUrl.href,

@@ -417,8 +454,8 @@ destinationDir: fileURLToPath(dir),

});
logger.success(`\`${SITEMAP_INDEX_FILE_NAME}\` is created.`);
logger.success([`${fileNames.map((name) => `\`${name}\``).join(", ")} are created.`, `Total entries: ${urlData.length}.`]);
if (createLinkInHead) {
const sitemapHref = path.posix.join(config.base, SITEMAP_INDEX_FILE_NAME);
const sitemapHref = path.posix.join(config.base, fileNames[0]);
const headHTML = `<link rel="sitemap" type="application/xml" href="${sitemapHref}">`;
await processPages(pages, dir, headHTML, config.build.format);
logger.success("Sitemap links are created in <head> section of generated pages.");
await processPages(srcPages, dir, headHTML, config.build.format);
logger.success("Sitemap links are inserted into <head> section of generated pages.");
}

@@ -438,6 +475,3 @@ } catch (err) {

export {
EnumChangefreq as ChangeFreq,
LinkItem,
SitemapItemLoose2 as SitemapItemLoose,
src_default as default
};
{
"name": "astro-sitemap",
"version": "0.2.4",
"version": "0.2.7",
"description": "Generate a sitemap for Astro with more control",

@@ -36,2 +36,3 @@ "keywords": [

"deepmerge": "^4.2.2",
"minimatch": "^5.1.0",
"node-html-parser": "^5.3.3",

@@ -42,4 +43,5 @@ "sitemap": "^7.1.1",

"devDependencies": {
"@types/minimatch": "^3.0.5",
"@types/node": "^18.0.0",
"astro": "^1.0.0-beta.50",
"astro": "^1.0.0-beta.54",
"at-scripts": "0.0.4",

@@ -49,3 +51,3 @@ "c8": "^7.11.3",

"vite": "^2.9.12",
"vitest": "^0.15.2"
"vitest": "^0.16.0"
},

@@ -52,0 +54,0 @@ "publishConfig": {

@@ -15,7 +15,6 @@ [![Help Ukraine now!](https://raw.githubusercontent.com/alextim/help-ukraine-win-flag/master/stop-russian-agressian-help-ukraine-now-link.svg 'Help Ukraine now!')](https://bank.gov.ua/en/about/support-the-armed-forces)

Some of functionality from **astro-sitemap v0.2.2** became an update for the official integration [@astrojs/sitemap](https://github.com/withastro/astro/tree/main/packages/integrations/sitemap) from v0.1.2 to v0.2.0.
Some of functionality from **astro-sitemap v0.2.2** became an update for the official integration [@astrojs/sitemap](https://github.com/withastro/astro/tree/main/packages/integrations/sitemap) from v0.1.2 to v0.2.0.
From now you can use the official integration in most cases.
Shared functionality with the official **@astrojs/sitemap**:

@@ -31,2 +30,3 @@

- Exclude pages from sitemap by glob patterns.
- More control on sitemap output:

@@ -39,2 +39,4 @@ - manage xml namespaces;

:exclamation: Both integrations, the official and **astro-sitemap** don't support SSR.
---

@@ -125,7 +127,6 @@

**sitemap-0.xml**
<?xml version="1.0" encoding="UTF-8"?>
```xml
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:news="http://www.google.com/schemas/sitemap-news/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns:image="http://www.google.com/schemas/sitemap-image/1.1" xmlns:video="http://www.google.com/schemas/sitemap-video/1.1">
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml">
<url>

@@ -150,21 +151,17 @@ <loc>https://example.com/</loc>

## Options
### Options shared with the official integration
| Name | Type | Required | Default | Description |
| :------------: | :------------------------: | :------: | :-----: | :---------------------------------------------------------------------------------------------- |
| `filter` | `(page: String):`<br/>`Boolean`| No | | The same as official. Function to filter generated pages to exclude some paths from a sitemap. |
| `customPages` | `String[]` | No | | The same as official. Absolute url list. It will be merged with generated pages urls. |
| `canonicalURL` | `String` | No | | The same as official. Absolute url. The integration needs `site` from astro.config or `canonicalURL`. If both values are provided then only `canonicalURL` will be used by the integration. |
| `entryLimit` | `Number` | No | 45000 | Number of entries per sitemap file, a sitemap index and multiple sitemaps are created if you have more entries. See more on [Google]((<https://developers.google.com/search/docs/advanced/sitemaps/large-sitemaps/>) |
| `xslUrl` | `String` | No | | Absolute URL of XSL file to transform XML to other format. Ignored by search engines. |
| `xmlns` | `NSArgs` | No | | Manage xml namespaces in the `<urlset>` |
| `lastmodDateOnly` | `Boolean` | No | | If it's `true` the XML output will contain a date part only. |
search/docs/advanced/sitemaps/large-sitemaps) |
| `filter` | `(page: String):`<br/>`Boolean`| No | | Function to filter generated pages to exclude some paths from a sitemap. |
| `customPages` | `String[]` | No | | Absolute URL list. It will be merged with generated pages urls.<br/>You should also use `customPages` to manually list sitemap pages when using an SSR adapter. Currently, integration cannot detect your site's pages unless you are building statically. To avoid an empty sitemap, list all pages (including the base origin) with this configuration option! |
| `canonicalURL` | `String` | No | | Absolute URL. The integration needs `site` from astro.config or `canonicalURL`. If both values are provided then only `canonicalURL` will be used by the integration. |
| `entryLimit` | `Number` | No | 45000 | Number of entries per sitemap file, a sitemap index and multiple sitemaps are created if you have more entries. See more on [Google](https://developers.google.com/search/docs/advanced/sitemaps/large-sitemaps/) |
| `changefreq` | `ChangeFreq` | No | | Sitemap specific. Ignored by Google.<br/>How frequently the page is likely to change.<br/>Available values: `always`\|`hourly`\|`daily`\|`weekly`\|`monthly`\| `yearly`\|`never` |
| `lastmod` | `Date` | No | | Sitemap specific. The date of page last modification. |
| `priority` | `Number` | No | | Sitemap specific. Ignored by Google.<br/>The priority of this URL relative to other URLs on your site. Valid values range from 0.0 to 1.0 |
| `serialize` | `(item: SitemapItem):`<br>`SitemapItemLoose`\|`undefined`\|<br/>Promise<`SitemapItemLoose`\|`undefined`>| No | | Function to process an array of sitemap entries just before writing them to disk. Async or sync.<br/>The `undefined` return value excludes the passed entry from sitemap. |
| `createLinkInHead`| `Boolean` | No | true | Create a link on the sitemap in `<head>` of generated pages.<br/>The final output reprocessing is used for this. It could impact on a build time for large sites. |
| **i18n** | `object` | No | | Provide this object to start |
| `defaultLocale`| `String` | Yes | | Its value has to be exists as one of `locales` keys. |
| `serialize` | `(item: SitemapItem):`<br>`SitemapItemLoose`\|`undefined`\|<br/>Promise<`SitemapItemLoose`\|`undefined`>| No | | Function to process an array of sitemap entries just before writing them to disk. Async or sync.<br/>**Modified behavior** compared to the official integration: `undefined` return value excludes the passed entry from sitemap. |
| **i18n** | `object` | No | | |
| `defaultLocale`| `String` | Yes | | Its value has to be exist as one the `locales` keys. |
| `locales` | `Record<String, String>` | Yes | | Key/value - pairs.<br/>The key is used to look for a locale part in a page path.<br/> The value is a language attribute, only English alphabet and hyphen allowed. See more on [MDN](https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/lang) |

@@ -176,14 +173,4 @@

### NSArgs
#### SitemapItem
| Name | Type | Required | Default | Description |
| :------: | :-------: | :------: | :-----: | :----------------------------------------------------------------------- |
| `xhtml` | `Boolean` | No | true | `xmlns:xhtml="http://www.w3.org/1999/xhtml"` |
| `news` | `Boolean` | No | | `xmlns:news="http://www.google.com/schemas/sitemap-news/0.9"` |
| `video` | `Boolean` | No | | `xmlns:video="http://www.google.com/schemas/sitemap-video/1.1"` |
| `image` | `Boolean` | No | | `xmlns:image="http://www.google.com/schemas/sitemap-image/1.1"` |
| `custom` | `String[]`| No | | Any custom namespace. Elements of array are used `as is`, no validation. |
### SitemapItem
| Name | Type | Required | Description |

@@ -197,4 +184,31 @@ | :------------: | :----------: | :------: | :----------------- |

### SitemapItemLoose
#### LinkItem
| Name | Type | Required | Description |
| :------------: | :-------------: | :------: | :------------------------ |
| `url` | `String` | Yes | Absolute url |
| `lang` | `String` | Yes | hreflag, example: 'en-US' |
### Extra options
| Name | Type | Required | Default | Description |
| :------------: | :----------: | :------: | :-----: | :---------------------------------------------------------------------------------------------- |
| `exclude` | `String[]` | No | | The `exclude` option is an array of [glob patterns](https://github.com/isaacs/minimatch#features) to exclude static routes from the generated sitemap. |
| `xslUrl` | `String` | No | | Absolute URL of XSL file to style XML or transform it to other format. Ignored by search engines. |
| `xmlns` | `NSArgs` | No | | Set the XML namespaces by xmlns attributes in `<urlset>` element. |
| `lastmodDateOnly` | `Boolean` | No | | If it's `true` the XML output will contain a date part only. |
| `createLinkInHead`| `Boolean` | No | true | Create a link on the sitemap in `<head>` of generated pages.<br/>The final output reprocessing is used for this. It could impact on a build time for large sites. |
#### NSArgs
| Name | Type | Required | Default | Description |
| :------: | :-------: | :------: | :-----: | :----------------------------------------------------------------------- |
| `xhtml` | `Boolean` | No | true | `xmlns:xhtml="http://www.w3.org/1999/xhtml"` |
| `news` | `Boolean` | No | | `xmlns:news="http://www.google.com/schemas/sitemap-news/0.9"` |
| `video` | `Boolean` | No | | `xmlns:video="http://www.google.com/schemas/sitemap-video/1.1"` |
| `image` | `Boolean` | No | | `xmlns:image="http://www.google.com/schemas/sitemap-image/1.1"` |
| `custom` | `String[]`| No | | Any custom namespace. Elements of array'll be used `as is` without any validation. |
#### SitemapItemLoose
The `SitemapItemLoose` interface is a base for the `SitemapItem`.

@@ -206,9 +220,2 @@

### LinkItem
| Name | Type | Required | Description |
| :------------: | :-------------: | :------: | :------------------------ |
| `url` | `String` | Yes | Absolute url |
| `lang` | `String` | Yes | hreflag, example: 'en-US' |
**Sample of _astro.config.mjs_**

@@ -244,5 +251,26 @@

// This function is called just before a sitemap writing to disk.
// You have more control on resulting output.
// sync or async
serialize(item: SitemapItem): SitemapItem {
if (/special-page/.test(item.url)) {
item.changefreq = 'daily';
item.lastmod = new Date();
item.priority = 0.9;
}
return item;
},
// The integration creates a separate `sitemap-${i}.xml` file for each batch of 45000 and adds this file to index - `sitemap-index.xml`.
entryLimit: 1000, // default - 45000
// sitemap specific
changefreq: 'yearly',
lastmod: new Date('May 01, 2019 03:24:00'),
priority: 0.2,
/**
* `astro-sitemap` integration extra options
*/
exclude: ['404', 'blog-*/']
// print date not time

@@ -266,22 +294,2 @@ lastmodDateOnly: false,

// This function is called just before a sitemap writing to disk.
// You have more control on resulting output.
// sync or async
serialize(item: SitemapItem): SitemapItem {
if (/special-page/.test(item.url)) {
item.changefreq = 'daily';
item.lastmod = new Date();
item.priority = 0.9;
}
return item;
},
// The integration creates a separate `sitemap-${i}.xml` file for each batch of 45000 and adds this file to index - `sitemap-index.xml`.
entryLimit: 1000, // default - 45000
// sitemap specific
changefreq: 'yearly',
lastmod: new Date('May 01, 2019 03:24:00'),
priority: 0.2,
// Create or not a link to sitemap in '<head>' section of generated pages

@@ -441,2 +449,2 @@ createLinkInHead: true, // default - true

The `astro-sitemap` is based on the official integration [@astrojs/sitemap](https://github.com/withastro/astro/tree/main/packages/integrations/sitemap). Many thanks to the [Astro](https://astro.build/) team for their work!
Module based on the awesome [sitemap.js](https://github.com/ekalinin/sitemap.js) package ❤️.
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc