Skip to content

Commit

Permalink
Merge pull request #83 from quasibit/docs/readme-shortcode
Browse files Browse the repository at this point in the history
docs(readme): shortcode markdown info
  • Loading branch information
nunof07 authored Jan 23, 2025
2 parents 43c165c + ec377db commit f43231b
Showing 1 changed file with 39 additions and 36 deletions.
75 changes: 39 additions & 36 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ Add the plugin to your [Eleventy configuration](https://www.11ty.dev/docs/config
```js
const sitemap = require("@quasibit/eleventy-plugin-sitemap");

module.exports = function(eleventyConfig) {
module.exports = function (eleventyConfig) {
eleventyConfig.addPlugin(sitemap, {
sitemap: {
hostname: "https://example.com",
Expand Down Expand Up @@ -67,6 +67,7 @@ As the first argument to the shortcode, you pass the collection of items that
you want to use in the sitemap.

This shortcode is available for Liquid, Nunjucks, and Javascript templates.
It will not work if placed in a Markdown file.

You can also copy this sample from the examples and adapt it to your needs:

Expand Down Expand Up @@ -117,6 +118,7 @@ sitemap:
changefreq: weekly
priority: 0.8
---

```

Alternatively, you can apply the properties to a folder of items by creating a
Expand All @@ -132,8 +134,8 @@ module.exports = () => {
changefreq: "weekly",
priority: 0.8,
},
}
}
};
};
```

For a full list of options, please refer to [Sitemap Item Options](https://github.com/ekalinin/sitemap.js/blob/master/api.md#sitemap-item-options).
Expand All @@ -149,6 +151,7 @@ property, which will exclude them from all collections.
---
eleventyExcludeFromCollections: true
---

```

You can use the `ignore` property on the `sitemap` data, which will only
Expand All @@ -159,15 +162,16 @@ exclude it from the sitemap.
sitemap:
ignore: true
---

```

Or you can create a [custom collection](https://www.11ty.dev/docs/collections/#advanced-custom-filtering-and-sorting)
that excludes the items that you don't want in the sitemap and then pass that
collection to the shortcode.

```js
eleventyConfig.addCollection("sitemap", function(collectionApi) {
return collectionApi.getAll().filter(item => {
eleventyConfig.addCollection("sitemap", function (collectionApi) {
return collectionApi.getAll().filter((item) => {
// Place your condition here for excluding items from the sitemap.
return true;
});
Expand All @@ -183,44 +187,43 @@ Specify the collection in the shortcode:
### Create a multilingual sitemap

For creating a [multilingual sitemap](https://webmasters.googleblog.com/2012/05/multilingual-and-multinational-site.html)
you need to add alternate language child links for each link.
you need to add alternate language child links for each link.

You could do this with [front matter](#customizing-sitemap-properties) and the
[links property](https://github.com/ekalinin/sitemap.js/blob/master/api.md#sitemap-item-options)
, but in most cases it's easier to do this with a custom collection.

```js
eleventyConfig.addCollection("sitemap", function(collectionApi) {
return collectionApi
.getAll()
.map((item, index, all) => {
return {
url: item.url,
date: item.date,
data: {
...item.data,
sitemap: {
...item.data.sitemap,
links:
all
// Find all the translations of the current item.
// This assumes that all translated items that belong together
// have the same `translationKey` value in the front matter.
.filter(other => other.data.translationKey === item.data.translationKey)

// Map each translation into an alternate link. See https://github.com/ekalinin/sitemap.js/blob/master/api.md#ILinkItem
// Here we assume each item has a `lang` value in the front
// matter. See https://support.google.com/webmasters/answer/189077#language-codes
.map(translation => {
return {
url: translation.url,
lang: translation.data.lang,
};
}),
},
eleventyConfig.addCollection("sitemap", function (collectionApi) {
return collectionApi.getAll().map((item, index, all) => {
return {
url: item.url,
date: item.date,
data: {
...item.data,
sitemap: {
...item.data.sitemap,
links: all
// Find all the translations of the current item.
// This assumes that all translated items that belong together
// have the same `translationKey` value in the front matter.
.filter(
(other) => other.data.translationKey === item.data.translationKey
)

// Map each translation into an alternate link. See https://github.com/ekalinin/sitemap.js/blob/master/api.md#ILinkItem
// Here we assume each item has a `lang` value in the front
// matter. See https://support.google.com/webmasters/answer/189077#language-codes
.map((translation) => {
return {
url: translation.url,
lang: translation.data.lang,
};
}),
},
}
});
},
};
});
});
```

Expand Down

0 comments on commit f43231b

Please sign in to comment.