Skip to content
This repository has been archived by the owner on Jan 13, 2021. It is now read-only.

Commit

Permalink
More details, fix impl
Browse files Browse the repository at this point in the history
  • Loading branch information
RDIL committed Oct 20, 2020
1 parent a00b534 commit b4eb804
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 21 deletions.
31 changes: 31 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,34 @@
# merge-sitemaps

A simple CLI and npm package to merge sitemaps together.

## Usage

### With CLI

```bash
$ npx merge-sitemaps sitemap.xml subdir/other-sitemap.xml build/sitemap.xml
```

(With the CLI, argument 1 is the base sitemap, argument 2 is the secondary sitemap, and argument 3 is the destination for the output.)

### With API

```js
var mergeSitemaps = require("merge-sitemaps");

// note: the API doesn't actually do any work with files, it just does the string manipulation and related stuff
console.log(
mergeSitemaps("base-xml-sitemap-as-string", "secondary-xml-sitemap-as-string")
);
```

## License

MIT. See the LICENSE file.

## Credits

- [Reece Dunham](https://rdil.rocks) - Author
- [Dowland Aiello](https://github.com/dowlandaiello) - Making it work
- [This StackOverflow post](https://stackoverflow.com/questions/27936772/how-to-deep-merge-instead-of-shallow-merge) - Very helpful
3 changes: 3 additions & 0 deletions dev/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Dev files

These are example sitemaps used to test the program in development.
2 changes: 1 addition & 1 deletion dev/outmap.xml
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<?xml version="1.0" encoding="UTF-8"?><urlset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd" xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"><url><loc>https://rdil.rocks/thing/</loc><changefreq>weekly</changefreq><priority>0.5</priority></url></urlset>
<?xml version="1.0" encoding="UTF-8"?><urlset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd" xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:news="http://www.google.com/schemas/sitemap-news/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns:mobile="http://www.google.com/schemas/sitemap-mobile/1.0" xmlns:image="http://www.google.com/schemas/sitemap-image/1.1" xmlns:video="http://www.google.com/schemas/sitemap-video/1.1"><url><loc>https://rdil.rocks/pages/thing.html</loc><lastmod>2020-09-15T11:03:04-04:00</lastmod></url><url><loc>https://rdil.rocks/</loc><lastmod>2020-09-15T11:03:04-04:00</lastmod></url></urlset>
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "A simple CLI and npm package to merge sitemaps together.",
"homepage": "https://github.com/rdilweb/merge-sitemaps",
"bin": {
"merge-sitemaps": "./src/index.js"
"merge-sitemaps": "./src/cli.js"
},
"keywords": [
"sitemap",
Expand Down
44 changes: 25 additions & 19 deletions source/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,25 @@
import xml from "xml-js"
import { merge } from "merge-anything"

const isObject = (target) =>
typeof target === "object" && !Array.isArray(target)

const mergeDeep = (target, ...sources) => {
if (!sources.length) return target
const source = sources.shift()

if (isObject(target) && isObject(source)) {
for (const key in source) {
if (isObject(source[key])) {
if (!target[key]) Object.assign(target, { [key]: {} })
mergeDeep(target[key], source[key])
} else {
Object.assign(target, { [key]: source[key] })
}
}
}

return mergeDeep(target, ...sources)
}

/**
* Merge sitemaps together.
Expand All @@ -9,24 +29,10 @@ import { merge } from "merge-anything"
* @returns {string} The generated XML.
*/
export default function mergeSitemaps(map1, map2) {
let mapObj = xml.xml2js(map1)
const secondMap = xml.xml2js(map2)
let mapObj = xml.xml2js(map1, { compact: true })
const secondMap = xml.xml2js(map2, { compact: true })

let getXmlValByName = (name, xml) => {
let item
xml.forEach((thing) => {
if (thing.name === name) {
item = thing
}
})
return item
}
mergeDeep(mapObj, secondMap)

mapObj.elements[
mapObj.elements.indexOf(getXmlValByName("urlset", mapObj.elements))
].elements = merge(
getXmlValByName("urlset", mapObj.elements).elements,
getXmlValByName("urlset", secondMap.elements).elements
)
return xml.js2xml(mapObj)
return xml.js2xml(mapObj, { compact: true })
}

0 comments on commit b4eb804

Please sign in to comment.