Skip to content

Commit

Permalink
Merge pull request #232 from derduher/parse-existing-sitemap
Browse files Browse the repository at this point in the history
Introduce parsing xml and outputing a compat conf
  • Loading branch information
derduher authored Aug 19, 2019
2 parents dc65317 + c205b5d commit f374497
Show file tree
Hide file tree
Showing 22 changed files with 852 additions and 3,156 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@
Add a pretty print option to `toString(false)`
pass true pretty print

Add an xmlparser that will output a config that would generate that same file
cli:
use --parser to output the complete config --line-separated to print out line
separated config compatible with the --json input option for cli
lib: import parseSitemap and pass it a stream

# 4.0.2
Fix npx script error - needs the shebang

Expand Down
29 changes: 29 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ Table of Contents
* [Sitemap](#sitemap)
* [buildSitemapIndex](#buildsitemapindex)
* [createSitemapIndex](#createsitemapindex)
* [xmlLint](#xmllint)
* [parseSitemap](#parsesitemap)
* [Sitemap Item Options](#sitemap-item-options)
* [ISitemapImage](#isitemapimage)
* [IVideoItem](#ivideoitem)
Expand Down Expand Up @@ -327,6 +329,33 @@ createSitemapIndex({
})
```

### xmlLint
Resolve or reject depending on whether the passed in xml is a valid sitemap.
This is just a wrapper around the xmllint command line tool and thus requires
xmllint.
```
const { createReadStream } = require('fs')
const { xmlLint } = require('sitemap')
xmlLint(createReadStream('./example.xml')).then(
() => console.log('xml is valid'),
([err, stderr]) => console.error('xml is invalid', stderr)
)
```

### parseSitemap
Read xml and resolve with the configuration that would produce it or reject with
an error
```
const { createReadStream } = require('fs')
const { parseSitemap, createSitemap } = require('sitemap')
parseSitemap(createReadStream('./example.xml')).then(
// produces the same xml
// you can, of course, more practically modify it or store it
(xmlConfig) => console.log(createSitemap(xmlConfig).toString()),
(err) => console.log(err)
)
```

### Sitemap Item Options

|Option|Type|eg|Description|
Expand Down
44 changes: 34 additions & 10 deletions cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import { Readable } from 'stream'
import { createReadStream } from 'fs'
import { xmlLint } from './lib/xmllint'
import { XMLLintUnavailable } from './lib/errors'
console.warn('CLI is in new and likely to change quite a bit. Please send feature/bug requests to https://github.com/ekalinin/sitemap.js/issues')
import { parseSitemap } from './lib/sitemap-parser'
console.warn('CLI is new and likely to change quite a bit. Please send feature/bug requests to https://github.com/ekalinin/sitemap.js/issues')
/* eslint-disable-next-line @typescript-eslint/no-var-requires */
const arg = require('arg')

Expand Down Expand Up @@ -39,27 +40,50 @@ const argSpec = {
'--help': Boolean,
'--version': Boolean,
'--json': Boolean,
'--validate': Boolean
'--validate': Boolean,
'--parse': Boolean,
'--line-separated': Boolean
}
const argv = arg(argSpec)

function getStream (): Readable {
if (argv._ && argv._.length) {
return createReadStream(argv._[0])
} else {
console.warn('Reading from stdin. If you are not piping anything in, this command is not doing anything')
return process.stdin
}
}
if (argv['--version']){
/* eslint-disable-next-line @typescript-eslint/no-var-requires */
const packagejson = require('../package.json')
console.log(packagejson.version)
} else if (argv['--help']) {
// TODO stream a full JSON configuration in
// TODO allow user to append entry to existing xml
console.log(`
Turn a list of urls into a sitemap xml.
Options:
--help Print this text
--version Print the version
--json Parse each line as json and feed to Sitemap
--help Print this text
--version Print the version
--json Parse each line as json and feed to Sitemap
--parse Parse fed xml and spit out config
--line-separated When used with parse, it spits out each entry as json rather
than the whole json. This can be then consumed with --json by
the cli
`)
} else if (argv['--parse']) {
parseSitemap(getStream()).then((items): void => {
if (argv['--line-separated'] && items.urls) {
items.urls.forEach((url): void => {
console.log(JSON.stringify(url))
})
} else {
console.log(JSON.stringify(items))
}
})
} else if (argv['--validate']) {
let xml = process.stdin
if (argv._ && argv._.length) {
xml = argv._[0]
}
xmlLint(xml)
xmlLint(getStream())
.then((): void => console.log('valid'))
.catch(([error, stderr]: [Error|null, Buffer]): void => {
if (error instanceof XMLLintUnavailable) {
Expand Down
1 change: 1 addition & 0 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ export * from './lib/sitemap-index'
export * from './lib/errors'
export * from './lib/types'
export { xmlLint } from './lib/xmllint'
export { parseSitemap } from './lib/sitemap-parser'

export default createSitemap
Loading

0 comments on commit f374497

Please # to comment.