Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
quantum-kittens
GitHub Repository: quantum-kittens/platypus
Path: blob/main/converter/sitemap-to-json.ts
3338 views
1
import * as fs from 'fs'
2
import { parseString } from 'xml2js'
3
4
/**
5
* Generates a sitemap.json file from a sitemap.xml file.
6
* @param {string} sitemapXmlPath - The path to the sitemap.xml file.
7
* @param {string} sitemapJsonPath - The path to the output sitemap.json file.
8
*/
9
function generateJsonSitemap (
10
sitemapXmlPath: string,
11
sitemapJsonPath: string
12
) : Promise<void> {
13
return new Promise((resolve, reject) => {
14
fs.readFile(sitemapXmlPath, 'utf8', (err, sitemapXml) => {
15
if (err) {
16
reject(err)
17
} else {
18
parseString(sitemapXml, (err, sitemapJson) => {
19
if (err) {
20
reject(err)
21
}
22
23
fs.writeFile(
24
sitemapJsonPath,
25
JSON.stringify(sitemapJson, null, 2),
26
(err) => {
27
if (err) {
28
reject(err)
29
} else {
30
resolve()
31
}
32
})
33
})
34
}
35
})
36
})
37
}
38
39
export default generateJsonSitemap
40
41