makestatic-sitemap v1.2.4
Sitemap
Generate sitemap files
Plugin that inspects the resource graph and generates a sitemap.
It can generate plain text and XML sitemaps for robots, a JSON document for further processing and an HTML document for humans.
See google sitemaps and sitemaps for more information.
For the text and xml formats we recommended assigning this plugin to the emit phase. For the html format it is best used during the transform phase so the modified sitemap template is optimized.
Install
yarn add makestatic-sitemapAPI
SiteMap
Inspect the resource graph and generate a sitemap.
Suppported formats are xml, text, json and html.
Enable this plugin for the emit phase when writing the text, xml and
json formats. For the html format the transform phase is recommended
so the modified sitemap template is optimized.
The xml and text formats are designed to be exposed to robots to
indicate how they should crawl the site whilst the json format allows
programmatic processing of the sitemap, for example if you wanted to
fetch resources after invalidating a CDN cache.
The html format is designed to be exposed to humans on your website to
allow them to find relevant information. It generates an unordered list
of the sitemap tree hierarchy. When using the html format you should also
let us know the template that the list will be injected into using the
template option.
The template file must have an HTML AST available and declare an element
with an id attribute of sitemap the generated list will be injected as
a child of that element. This allows you to work on the sitemap markup and
styles as you would work with any other document and ensure the sitemap data
is always consistent with the website structure.
See Also
SiteMap
new SiteMap(context, options)Create a SiteMap plugin.
The base URL used to make links absolute first uses the base option
otherwise looks for a url configuration option and finally will try to
extract the homepage from a package.json file in the current working
directory.
When no formats are given the xml format is used.
The name option should not include a file extension.
The rules option allows you to set fields for the xml output based on
regular expression test patterns, for example:
{
rules: [
{
test: /docs\//,
changefreq: 'weekly',
priority: 0.8
}
]
}If changefreq is invalid it will be ignored, if priority is outside of
the zero to one range it is clamped.
When the image option is set the xml output format will include image
resources in documents. The image:loc node is always set to an absolute
URL using the src attribute of the img element.
The meta data to add to the sitemap is extracted from attributes on the
element so you can declare sitemap meta data in the HTML document.
The attribute to XML node name map for img elements:
title: image:titledata-caption: image:captiondata-geo-location: image:geo_locationdata-license: image:license
Image elements in sitemap URLs have a limit of 1000 if the number of images in a page exceeds this limit the sitemap will only include the first 1000.
When the video option is set the xml output format will include video
resources in documents. The video:content_loc node is always set to
an absolute URL using the src attribute of the video element.
Video sitemap meta data is extracted from the element attributes. The
attribute to XML node name map for video elements:
title: video:titledata-description: video:descriptiondata-thumbnail-loc: video:thumbnail_locdata-duration: video:durationdata-expiration-date: video:expiration_datedata-rating: video:ratingdata-view-count: video:view_countdata-publication-date: video:publication_datedata-family-friendly: video:family_friendlydata-tag: video:tagdata-category: video:categorydata-restriction: video:restrictiondata-restriction-relationship: video:restriction (relationship attribute)data-gallery-loc: video:gallery_locdata-gallery-loc-title: video:gallery_loc (title attribute)data-price: video:pricedata-price-currency: video:price (currency attribute)data-requires-subscription: video:requires_subscriptiondata-uploader: video:uploaderdata-uploader-info: video:uploader (info attribute)data-platform: video:platformdata-platform-relationship: video:platform (relationship attribute)data-live: video:live
For the data-tag attribute you can separate multiple tags with a comma
and they are expanded to multiple video:tag elements in the xml.
If the video element contains child embed elements a video:player_loc
xml element is created for each child embed element with a src
attribute.
Videos are also limited to 1000 per page.
No validation is performed on the video attributes you should read the corresponding documentation to verify attribute values are correct.
You can pass options specific to a format using the renderer option
and the format key, for example:
{
renderer: {
html: {
builder: CustomHtmlBuilder
}
}
}When the robots option is set you should have a robots.txt file being
processed and enabled the parse-robots plugin otherwise the option will
have no effect. When configured correctly this option adds Sitemap
entries for the robots.txt file for the text and xml formats.
If you are creating sitemaps in both text and xml formats two
Sitemap entries will be created.
contextObject the processing context.optionsObject the plugin options.
Options
baseString URL for the site domain.pathString name of a sub-directory path.indexString=index.html name of index documents.nameString=sitemap name of the output file.formatsString|Array=xml list of output formats to emit.rulesArray setchangefreq,priorityfields for xml.cleanBoolean=true use clean URLs for index documents.slashBoolean=true use trailing slashes.robotsBoolean=false addSitemapentries to robots.txt.imageBoolean=false generate image xml entries.videoBoolean=false generate video xml entries.includeBoolean=false should the sitemap template be included.indentNumber=0 number of spaces to indent.templateString=sitemap.html template for the HTML format.rendererObject format specific renderer options.
Throws
Errorif no resource graph is available.Errorif no base URL is available.Errorif an unknown format is detected.
.before
SiteMap.prototype.before(context, options)Generate the sitemap.
When the exclude option is given each entry should be a regular
expression pattern. If a pattern matches an HTML document id in the
resource graph it is not included in the sitemap.
contextObject the processing context.optionsObject the plugin options.
Options
excludeArray list of regular expressions to exclude.
HtmlBuilder
Default implementation for generating a DOM structure of the sitemap within the sitemap template file.
It is recommended that you use this default implementation and style the lists but if you really want to use different elements for the sitemap you can supply an alternative builder class as a renderer option.
HtmlBuilder
new HtmlBuilder(context, sitemap, ast, template, options)Create an HtmlBuilder.
Returns a string href.
contextObject the processing context.sitemapObject raw sitemap data.astObject the sitemap AST.templateObject reference to the sitemap template.optionsObject the renderer options.
.getHref
HtmlBuilder.prototype.getHref(node)Get an href attribute value.
Returns a string href.
nodeObject the sitemap tree node.
.getTitle
HtmlBuilder.prototype.getTitle(node)Get the title for a node.
This value is used for the link text and the link title attribute.
Returns a string title.
nodeObject the sitemap tree node.
.getLinkText
HtmlBuilder.prototype.getLinkText(node)Get the text for a link node.
Prefers a title when available otherwise uses the page name.
Returns a string for the link text.
nodeObject the sitemap tree node.
.getDescription
HtmlBuilder.prototype.getDescription(node)Get the description for a node.
Extract the content attribute from a meta element with name set to
description.
Returns a string description.
nodeObject the sitemap tree node.
.getRootElement
HtmlBuilder.prototype.getRootElement(node)Create the root element to append as a child of the element with an id
of sitemap in the template file.
This implementation returns a ul element.
Returns an element.
nodeObject the sitemap AST node.
.getItemElement
HtmlBuilder.prototype.getItemElement(node)Get an element for each tree node item.
This implementation returns a li element.
Returns an element.
nodeObject the sitemap AST node.
onEnter
onEnter(node)Invoked when a node is entered.
nodeObject the sitemap tree node.
onExit
onExit(node)Invoked when a node is exited.
nodeObject the sitemap tree node.
.build
HtmlBuilder.prototype.build(parent)Main DOM builder function, generates unordered lists representing the sitemap.
parentObject the parent DOM node.
TextRenderer
Renders the sitemap as plain text.
#render
static render(context, sitemap)Render the plain text format.
Returns an object with the text file content.
contextObject the processing context.sitemapObject raw sitemap data.
extension
static extensionGet the file extension for the text format.
HtmlRenderer
Renders the sitemap as HTML.
#render
static render(context, sitemap, options)Render the HTML format.
Unlike other formats this renderer does not return a content string as
it modifies the template AST and marks the AST as dirty before updating
the file content.
When no builder option is given the default HtmlBuilder class is used.
When the strategy option is given it should be one of root, absolute
or relative. The default strategy root builds links with a leading
slash, the absolute strategy uses the base URL to make links include
the domain name and the relative strategy resolves links relative to
the sitemap template file.
If an unsupported strategy is given the default is used.
Returns an object with the sitemap ast.
contextObject the processing context.sitemapObject raw sitemap data.optionsObject renderer options.
Options
strategyString=root link href strategy.selectorString=#sitemap query for the parent element.builderFunction the HTML builder class.
Throws
Errorif the sitemap template could not be found.Errorif the parent element could not be found.
JsonRenderer
Renders the sitemap as JSON and generates an AST of the sitemap structure.
extension
static extensionGet the file extension for the json format.
XmlRenderer
Renders the sitemap as XML.
#render
static render(context, sitemap)Render the XML format.
Returns an object with the xml file content.
contextObject the processing context.sitemapObject raw sitemap data.
Options
imageString=false include document images.videoString=false include document videos.rulesArray list of document rules.
extension
static extensionGet the file extension for the xml format.
License
MIT
Created by mkdoc on March 12, 2017
8 years ago
8 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago