diff --git a/CHANGELOG.md b/CHANGELOG.md
index 8d0e26ad..b3706f43 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,5 +1,10 @@
+# master
+Add a pretty print option to `toString(false)`
+pass true pretty print
+
# 4.0.2
Fix npx script error - needs the shebang
+
# 4.0.1
Validation functions which depend on xmllint will now warn if you do not have xmllint installed.
diff --git a/README.md b/README.md
index ccff793a..05dc65d4 100644
--- a/README.md
+++ b/README.md
@@ -16,22 +16,22 @@ Table of Contents
* [Installation](#installation)
* [Usage](#usage)
- * [CLI](#CLI)
+ * [CLI](#cli)
* [Example of using sitemap.js with express:](#example-of-using-sitemapjs-with-express)
* [Example of dynamic page manipulations into sitemap:](#example-of-dynamic-page-manipulations-into-sitemap)
* [Example of most of the options you can use for sitemap](#example-of-most-of-the-options-you-can-use-for-sitemap)
* [Building just the sitemap index file](#building-just-the-sitemap-index-file)
* [Auto creating sitemap and index files from one large list](#auto-creating-sitemap-and-index-files-from-one-large-list)
- * [API](#API)
+ * [API](#api)
* [Create Sitemap](#create-sitemap)
* [Sitemap](#sitemap)
* [buildSitemapIndex](#buildsitemapindex)
* [createSitemapIndex](#createsitemapindex)
* [Sitemap Item Options](#sitemap-item-options)
- * [ISitemapImage](#ISitemapImage)
- * [IVideoItem](#IVideoItem)
- * [ILinkItem](#ILinkItem)
- * [INewsItem](#INewsItem)
+ * [ISitemapImage](#isitemapimage)
+ * [IVideoItem](#ivideoitem)
+ * [ILinkItem](#ilinkitem)
+ * [INewsItem](#inewsitem)
* [License](#license)
Installation
@@ -58,8 +58,6 @@ Or verify an existing sitemap
## As a library
-The main functions you want to use in the sitemap module are
-
```javascript
const { createSitemap } = require('sitemap')
// Creates a sitemap object given the input configuration with URLs
@@ -222,11 +220,11 @@ const smi = createSitemapIndex({
## API
-## Sitemap
+### Sitemap
```
const { Sitemap } = require('sitemap')
-const sm = new Sitemap({
+const smi = new Sitemap({
urls: [{url: '/path'}],
hostname: 'http://example.com',
cacheTime: 0, // default
@@ -234,8 +232,75 @@ const sm = new Sitemap({
})
sm.toString() // returns the xml as a string
```
-
-## buildSitemapIndex
+
+__toString__
+ ```
+ smi.toString(true)
+ ```
+ Converts the urls stored in an instance of Sitemap to a valid sitemap xml document as a string. Accepts a boolean as its first argument to designate on whether to pretty print. Defaults to false.
+
+__toXML__
+alias for toString
+
+__toGzip__
+ ```
+ smi.toGzip ((xmlGzippedBuffer) => console.log(xmlGzippedBuffer));
+ smi.toGzip();
+ ```
+ like toString, it builds the xmlDocument, then it runs gzip on the resulting string and returns it as a Buffer via callback or direct invokation
+
+__clearCache__
+ ```
+ smi.clearCache()
+ ```
+ cache will be emptied and will be bipassed until set again
+
+__isCacheValid__
+ ```
+ smi.isCacheValid()
+ ```
+ returns true if it has been less than cacheTimeout ms since cache was set
+
+__setCache__
+ ```
+ smi.setCache('...xmlDoc')
+ ```
+ stores the passed in string on the instance to be used when toString is called within the configured cacheTimeout
+ returns the passed in string unaltered
+
+__add__
+ ```
+ smi.add('/path', 'warn')
+ ```
+ adds the provided url to the sitemap instance
+ takes an optional parameter level for whether to print a console warning in the event of bad data 'warn' (default), throw an exception 'throw', or quietly ignore bad data 'silent'
+ returns the number of locations currently in the sitemap instance
+
+__contains__
+ ```
+ smi.contains('/path')
+ ```
+ Returns true if path is already a part of the sitemap instance, false otherwise.
+
+__del__
+ ```
+ smi.del('/path')
+ ```
+ removes the provided url or url option from the sitemap instance
+
+__normalizeURL__
+ ```
+ Sitemap.normalizeURL('/', undefined, 'http://example.com')
+ ```
+ static function that returns the stricter form of a options passed to SitemapItem
+
+__normalizeURLs__
+ ```
+ Sitemap.normalizeURLs(['http://example.com', {url: 'http://example.com'}])
+ ```
+ static function that takes an array of urls and returns a Map of their resolved url to the strict form of SitemapItemOptions
+
+### buildSitemapIndex
Build a sitemap index file
```
const { buildSitemapIndex } = require('sitemap')
@@ -245,7 +310,7 @@ const index = buildSitemapIndex({
})
```
-## createSitemapIndex
+### createSitemapIndex
Create several sitemaps and an index automatically from a list of urls
```
const { createSitemapIndex } = require('sitemap')
@@ -262,7 +327,7 @@ createSitemapIndex({
})
```
-## Sitemap Item Options
+### Sitemap Item Options
|Option|Type|eg|Description|
|------|----|--|-----------|
@@ -278,7 +343,7 @@ createSitemapIndex({
|mobile|boolean or string|||
|cdata|boolean|true|wrap url in cdata xml escape|
-## ISitemapImage
+### ISitemapImage
Sitemap image
https://support.google.com/webmasters/answer/178636?hl=en&ref_topic=4581190
@@ -291,7 +356,7 @@ https://support.google.com/webmasters/answer/178636?hl=en&ref_topic=4581190
|geoLocation|string - optional|'Limerick, Ireland'|The geographic location of the image.|
|license|string - optional|'http://example.com/license.txt'|A URL to the license of the image.|
-## IVideoItem
+### IVideoItem
Sitemap video. https://support.google.com/webmasters/answer/80471?hl=en&ref_topic=4581190
@@ -326,7 +391,7 @@ Sitemap video. https://support.google.com/webmasters/answer/80471?hl=en&ref_topi
|requires_subscription|string 'YES'\|'NO' - optional|'YES'|Indicates whether a subscription (either paid or free) is required to view the video. Allowed values are yes or no.|
|live|string 'YES'\|'NO' - optional|'NO'|Indicates whether the video is a live stream. Supported values are yes or no.|
-## ILinkItem
+### ILinkItem
https://support.google.com/webmasters/answer/189077
@@ -335,7 +400,7 @@ https://support.google.com/webmasters/answer/189077
|lang|string|'en'||
|url|string|'http://example.com/en/'||
-## INewsItem
+### INewsItem
https://support.google.com/webmasters/answer/74288?hl=en&ref_topic=4581190
diff --git a/lib/sitemap.ts b/lib/sitemap.ts
index 9953fc80..1dfa6baf 100644
--- a/lib/sitemap.ts
+++ b/lib/sitemap.ts
@@ -299,7 +299,7 @@ export class Sitemap {
* Synchronous alias for toXML()
* @return {String}
*/
- toString (): string {
+ toString (pretty = false): string {
if (this.root.children.length) {
this.root.children = []
}
@@ -325,8 +325,11 @@ export class Sitemap {
for (let [, smi] of this.urls) {
(new SitemapItem(smi, this.root)).buildXML()
}
-
- return this.setCache(this.root.end())
+ let opts
+ if (pretty) {
+ opts = {pretty}
+ }
+ return this.setCache(this.root.end(opts))
}
toGzip (callback: CompressCallback): void;
diff --git a/package-lock.json b/package-lock.json
index 29f1da05..2ffbd2ed 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -1,6 +1,6 @@
{
"name": "sitemap",
- "version": "3.2.2",
+ "version": "4.0.2",
"lockfileVersion": 1,
"requires": true,
"dependencies": {
diff --git a/tests/sitemap.test.ts b/tests/sitemap.test.ts
index d60ac71c..e4c620c0 100644
--- a/tests/sitemap.test.ts
+++ b/tests/sitemap.test.ts
@@ -51,6 +51,17 @@ describe('sitemap', () => {
'')
})
+ it('pretty prints', () => {
+ var ssp = new Sitemap({urls: ['http://ya.ru']})
+ expect(ssp.toString(true)).toBe(
+ xmlDef + '\n' +
+ urlset + '\n' +
+ ' \n ' +
+ xmlLoc + '\n' +
+ ' \n' +
+ '')
+ })
+
describe('normalizeURL', () => {
it('turns strings into full urls', () => {
expect(Sitemap.normalizeURL('http://example.com', create('urlset'))).toHaveProperty('url', 'http://example.com/')