From 3681d71448c8740f2f684ac9e7fd6abc6770dda0 Mon Sep 17 00:00:00 2001 From: Patrick Weygand Date: Fri, 16 Aug 2019 19:59:04 -0700 Subject: [PATCH 1/6] add pretty print, improve docs --- README.md | 56 +++++++++++++++++++++++++++++++++++++++++++ lib/sitemap.ts | 9 ++++--- package-lock.json | 2 +- tests/sitemap.test.ts | 11 +++++++++ 4 files changed, 74 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index ccff793a..c33c22d5 100644 --- a/README.md +++ b/README.md @@ -235,6 +235,62 @@ const sm = new Sitemap({ sm.toString() // returns the xml as a string ``` +*toGzip* + ``` + toGzip ((xmlGzippedBuffer) => console.log(xmlGzippedBuffer)); + 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 +*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. +*clearCache* + ``` + smi.clearCache() + ``` +*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 +*toXML* +alias for toString +*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 ``` 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/') From 447d9fa3e10477254739a3f05f10feb21843d654 Mon Sep 17 00:00:00 2001 From: Patrick Weygand Date: Fri, 16 Aug 2019 20:14:36 -0700 Subject: [PATCH 2/6] Update README.md --- README.md | 32 +++++++++++++++++++++----------- 1 file changed, 21 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index c33c22d5..3805750b 100644 --- a/README.md +++ b/README.md @@ -235,57 +235,67 @@ const sm = new Sitemap({ sm.toString() // returns the xml as a string ``` -*toGzip* +__toGzip__ ``` toGzip ((xmlGzippedBuffer) => console.log(xmlGzippedBuffer)); 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 -*toString* + +__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. -*clearCache* + +__clearCache__ ``` smi.clearCache() ``` -*isCacheValid* + +__isCacheValid__ ``` smi.isCacheValid() ``` returns true if it has been less than cacheTimeout ms since cache was set -*setCache* + +__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* + +__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* + +__contains__ ``` smi.contains('/path') ``` Returns true if path is already a part of the sitemap instance, false otherwise. -*del* + +__del__ ``` smi.del('/path') ``` removes the provided url or url option from the sitemap instance -*toXML* + +__toXML__ alias for toString -*normalizeURL* + +__normalizeURL__ ``` Sitemap.normalizeURL('/', undefined, 'http://example.com') ``` static function that returns the stricter form of a options passed to SitemapItem -*normalizeURLs* + +__normalizeURLs__ ``` Sitemap.normalizeURLs(['http://example.com', {url: 'http://example.com'}]) ``` From 82d8d02b57aec9612cbe55c8cd2d823ae930191c Mon Sep 17 00:00:00 2001 From: Patrick Weygand Date: Fri, 16 Aug 2019 20:33:03 -0700 Subject: [PATCH 3/6] update docs --- README.md | 49 ++++++++++++++++++++++++------------------------- 1 file changed, 24 insertions(+), 25 deletions(-) diff --git a/README.md b/README.md index 3805750b..5618c70d 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,7 +220,7 @@ const smi = createSitemapIndex({ ## API -## Sitemap +### Sitemap ``` const { Sitemap } = require('sitemap') @@ -234,6 +232,15 @@ const sm = new Sitemap({ }) sm.toString() // returns the xml as a string ``` + +__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__ ``` @@ -242,16 +249,11 @@ __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 -__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. - __clearCache__ ``` smi.clearCache() ``` + cache will be emptied and will be bipassed until set again __isCacheValid__ ``` @@ -285,9 +287,6 @@ __del__ smi.del('/path') ``` removes the provided url or url option from the sitemap instance - -__toXML__ -alias for toString __normalizeURL__ ``` @@ -301,7 +300,7 @@ __normalizeURLs__ ``` static function that takes an array of urls and returns a Map of their resolved url to the strict form of SitemapItemOptions -## buildSitemapIndex +### buildSitemapIndex Build a sitemap index file ``` const { buildSitemapIndex } = require('sitemap') @@ -311,7 +310,7 @@ const index = buildSitemapIndex({ }) ``` -## createSitemapIndex +### createSitemapIndex Create several sitemaps and an index automatically from a list of urls ``` const { createSitemapIndex } = require('sitemap') @@ -328,7 +327,7 @@ createSitemapIndex({ }) ``` -## Sitemap Item Options +### Sitemap Item Options |Option|Type|eg|Description| |------|----|--|-----------| @@ -344,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 @@ -357,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 @@ -392,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 @@ -401,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 From ad9f10f2f301ce8dbc704c7da5cac818f1298e63 Mon Sep 17 00:00:00 2001 From: Patrick Weygand Date: Fri, 16 Aug 2019 20:38:52 -0700 Subject: [PATCH 4/6] update docs --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 5618c70d..05dc65d4 100644 --- a/README.md +++ b/README.md @@ -224,7 +224,7 @@ const smi = createSitemapIndex({ ``` const { Sitemap } = require('sitemap') -const sm = new Sitemap({ +const smi = new Sitemap({ urls: [{url: '/path'}], hostname: 'http://example.com', cacheTime: 0, // default @@ -244,8 +244,8 @@ alias for toString __toGzip__ ``` - toGzip ((xmlGzippedBuffer) => console.log(xmlGzippedBuffer)); - 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 From ebedded606852aafd42869e4ec8a0a7fa6a96b5a Mon Sep 17 00:00:00 2001 From: Patrick Weygand Date: Fri, 16 Aug 2019 20:44:09 -0700 Subject: [PATCH 5/6] add changelog --- CHANGELOG.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8d0e26ad..155c52be 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,10 @@ +# 4.1.0 +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. From a1b09fa87b52822bd457d4547c57ea6abdc75321 Mon Sep 17 00:00:00 2001 From: Patrick Weygand Date: Fri, 16 Aug 2019 20:53:45 -0700 Subject: [PATCH 6/6] change header of changelog so we dont mislead those reading as to the status of the release --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 155c52be..b3706f43 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,4 @@ -# 4.1.0 +# master Add a pretty print option to `toString(false)` pass true pretty print