Giter Site home page Giter Site logo

xast-util-sitemap's Introduction

xast-util-sitemap

Build Coverage Downloads Size Sponsors Backers Chat

xast utility to build a sitemap.xml. Supports localization as suggested by Google.

This package focusses on a small set of widely used parts of sitemaps. It has a few good options instead of overwhelming with everything that could be done. If you do need more things, well: this utility gives you a syntax tree, which you can change.

Intended for sites with up to 50k URLs and a resulting serialized contents of up to 50MB. Wrapping this project into something that generates sitemap index files is left as an exercise to the reader.

See Google’s recommendations for whether you need a sitemap

You should place sitemaps in the root of your site and reference them in robots.txt. You might also report sitemap changes to Google.

Note that this package is ESM only: Node 12+ is required to use it and it must be imported instead of required.

Install

npm:

npm install xast-util-sitemap

Use

Say we have the following module, example.mjs

import {sitemap} from 'xast-util-sitemap'
import toXml from 'xast-util-to-xml'

var tree = sitemap([
  'https://example.com/alpha/',
  {url: 'https://example.com/bravo/'},
  {url: 'https://example.com/charlie/', modified: new Date(2018, 1, 2, 3)},
  {
    url: 'https://example.com/delta/',
    lang: 'en',
    alternate: {
      nl: 'https://example.com/dirk/',
      'fr-BE': 'https://example.com/désiré/'
    }
  }
])

console.log(toXml(tree))

Now, running node example.mjs yields (pretty printed):

<?xml version="1.0" encoding="utf-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml">
  <url>
    <loc>https://example.com/alpha/</loc>
  </url>
  <url>
    <loc>https://example.com/bravo/</loc>
  </url>
  <url>
    <loc>https://example.com/charlie/</loc>
    <lastmod>2018-02-02T02:00:00.000Z</lastmod>
  </url>
  <url>
    <loc>https://example.com/delta/</loc>
    <xhtml:link rel="alternate" hreflang="en" href="https://example.com/delta/" />
    <xhtml:link rel="alternate" hreflang="nl" href="https://example.com/dirk/" />
    <xhtml:link rel="alternate" hreflang="fr-BE" href="https://example.com/d%C3%A9sir%C3%A9/" />
  </url>
  <url>
    <loc>https://example.com/dirk/</loc>
    <xhtml:link rel="alternate" hreflang="en" href="https://example.com/delta/" />
    <xhtml:link rel="alternate" hreflang="nl" href="https://example.com/dirk/" />
    <xhtml:link rel="alternate" hreflang="fr-BE" href="https://example.com/d%C3%A9sir%C3%A9/" />
  </url>
  <url>
    <loc>https://example.com/d%C3%A9sir%C3%A9/</loc>
    <xhtml:link rel="alternate" hreflang="en" href="https://example.com/delta/" />
    <xhtml:link rel="alternate" hreflang="nl" href="https://example.com/dirk/" />
    <xhtml:link rel="alternate" hreflang="fr-BE" href="https://example.com/d%C3%A9sir%C3%A9/" />
  </url>
</urlset>

API

sitemap(data)

Build a sitemap.

data

URLs to build a sitemap for. data is an Array.<url | Entry>. url is string and equivalent to an {url: url} entry.

Returns

Rootxast root.

Entry

Entries represent a single URL and describe them with metadata.

entry.url

Full URL (<loc>; string, required, example: https://example.org/)

entry.modified

Value indicating when the page last changed (<lastmod>; Date or value for new Date(x), optional).

entry.lang

BCP 47 tag indicating the language of the page (string, required w/ alternate, example: 'en-GB').

entry.alternate

Translations of the page, where each key is a BCP 47 tag and each value an entry (Object<url | Entry>, optional, example: {nl: 'https://example.nl/'}).

Alternate resources “inherit” fields (modified) from the entry they are described in. To define different fields, either use a full entry object:

[
  {
    url: 'https://example.com/delta/',
    modified: '05 October 2011 14:48 UTC',
    lang: 'en',
    alternate: {nl: {url: 'https://example.com/dirk/', modified: '20 January 2020 00:00 UTC'}}
  }
]

Or define them separately:

[
  {
    url: 'https://example.com/delta/',
    modified: '05 October 2011 14:48 UTC',
    lang: 'en',
    alternate: {nl: 'https://example.com/dirk/'}
  },
  {
    url: 'https://example.com/dirk/',
    modified: '20 January 2020 00:00 UTC',
    // `xast-util-sitemap` is smart enough to know about the next two already,
    // but they’re shown here for clarity.
    lang: 'nl',
    alternate: {en: 'https://example.com/delta/'}
  }
]

Security

XML can be a dangerous language: don’t trust user-provided data. Sitemaps also indicate “ownership” of URLs: crawlers assume that the origin of the sitemap.xml file is also an owner

Related

Contribute

See contributing.md in syntax-tree/.github for ways to get started. See support.md for ways to get help.

This project has a code of conduct. By interacting with this repository, organization, or community you agree to abide by its terms.

License

MIT © Titus Wormer

xast-util-sitemap's People

Contributors

wooorm avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.