sitemaps
Advanced tools
Comparing version 0.0.2 to 1.0.0
{ | ||
"name": "sitemaps", | ||
"version": "0.0.2", | ||
"description": "A simple sitemap.xml generator.", | ||
"main": "index.js", | ||
"scripts": { | ||
"clean": "rm -rf node_modules && rm package-lock.json", | ||
"test": "mocha \"**/*.test.js\" --exit" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/diogocapela/sitemaps.git" | ||
}, | ||
"author": "Diogo Capela", | ||
"license": "MIT", | ||
"bugs": { | ||
"url": "https://github.com/diogocapela/sitemaps/issues" | ||
}, | ||
"homepage": "https://github.com/diogocapela/sitemaps#readme", | ||
"devDependencies": { | ||
"eslint": "^5.9.0", | ||
"eslint-config-airbnb-base": "^13.1.0", | ||
"eslint-plugin-import": "^2.14.0", | ||
"lodash": "^4.17.11", | ||
"mocha": "^5.2.0" | ||
} | ||
"name": "sitemaps", | ||
"version": "1.0.0", | ||
"description": "A simple sitemap.xml generator.", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "mocha \"**/*.test.js\" --exit" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/diogocapela/sitemaps.git" | ||
}, | ||
"author": "Diogo Capela", | ||
"license": "MIT", | ||
"bugs": { | ||
"url": "https://github.com/diogocapela/sitemaps/issues" | ||
}, | ||
"homepage": "https://github.com/diogocapela/sitemaps#readme", | ||
"devDependencies": { | ||
"eslint": "7.7.0", | ||
"eslint-config-airbnb-base": "14.2.0", | ||
"eslint-config-prettier": "6.11.0", | ||
"eslint-plugin-import": "2.22.0", | ||
"eslint-plugin-prettier": "3.1.4", | ||
"lodash": "4.17.20", | ||
"mocha": "8.1.1", | ||
"prettier": "2.0.5" | ||
} | ||
} |
@@ -19,19 +19,30 @@ # sitemaps | ||
const highPriority = [ | ||
'https://example.com', | ||
]; | ||
const lowPriority = [ | ||
'https://example.com/about', | ||
'https://example.com/services', | ||
'https://example.com/contact', | ||
]; | ||
const filePath = `${__dirname}/sitemap.xml`; | ||
sitemaps(highPriority, lowPriority, filePath, (xml, error) => { | ||
if (error) { | ||
console.log(error); | ||
return; | ||
} | ||
console.log(`Sitemap.xml generated at: ${filePath}`); | ||
}); | ||
const links = [ | ||
{ | ||
loc: 'https://example.com', | ||
priority: '1.00', | ||
changefreq: 'weekly', | ||
}, | ||
{ | ||
loc: 'https://example.com/about', | ||
priority: '0.80', | ||
changefreq: 'monthly', | ||
}, | ||
{ | ||
loc: 'https://example.com/services', | ||
priority: '0.80', | ||
changefreq: 'monthly', | ||
}, | ||
{ | ||
loc: 'https://example.com/contact', | ||
priority: '0.60', | ||
changefreq: 'monthly', | ||
}, | ||
]; | ||
sitemaps(filePath, links); | ||
console.log(`Sitemap.xml generated at: ${filePath}`); | ||
``` | ||
@@ -38,0 +49,0 @@ |
const fs = require('fs'); | ||
module.exports = (highPriority, lowPriority, filePath, callback) => { | ||
const timestamp = new Date().toISOString(); | ||
let xml = '<?xml version="1.0" encoding="UTF-8"?>\n\n'; | ||
module.exports = (filePath, links = []) => { | ||
const timestamp = new Date().toISOString(); | ||
let xml = '<?xml version="1.0" encoding="UTF-8"?>\n\n'; | ||
xml += '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"\n'; | ||
xml += '\txmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"\n'; | ||
xml += '\txsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9\n'; | ||
xml += '\thttp://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">\n\n'; | ||
xml += '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"\n'; | ||
xml += '\txmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"\n'; | ||
xml += '\txsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9\n'; | ||
xml += '\thttp://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">\n\n'; | ||
highPriority.forEach((link) => { | ||
xml += '\t<url>\n'; | ||
xml += `\t\t<loc>${link}</loc>\n`; | ||
xml += `\t\t<lastmod>${timestamp}</lastmod>\n`; | ||
xml += '\t\t<priority>1.00</priority>\n'; | ||
xml += '\t</url>\n'; | ||
}); | ||
links.forEach((link) => { | ||
xml += '\t<url>\n'; | ||
xml += `\t\t<loc>${link.loc}</loc>\n`; | ||
xml += `\t\t<lastmod>${link.lastmod || timestamp}</lastmod>\n`; | ||
xml += `\t\t<changefreq>${link.changefreq || 'monthly'}</changefreq>\n`; | ||
xml += `\t\t<priority>${link.priority || '1.00'}</priority>\n`; | ||
xml += '\t</url>\n'; | ||
}); | ||
lowPriority.forEach((link) => { | ||
xml += '\t<url>\n'; | ||
xml += `\t\t<loc>${link}</loc>\n`; | ||
xml += `\t\t<lastmod>${timestamp}</lastmod>\n`; | ||
xml += '\t\t<priority>0.80</priority>\n'; | ||
xml += '\t</url>\n'; | ||
}); | ||
xml += '\n</urlset>\n'; | ||
xml += '\n</urlset>\n'; | ||
fs.writeFileSync(filePath, xml); | ||
fs.writeFile(filePath, xml, (error) => { | ||
if (error) { | ||
callback(null, error); | ||
} else { | ||
callback(xml, null); | ||
} | ||
}); | ||
return xml; | ||
}; |
@@ -9,41 +9,38 @@ /* eslint-disable no-undef */ | ||
const highPriority = [ | ||
'https://example.com', | ||
const links = [ | ||
{ | ||
loc: 'https://example.com', | ||
}, | ||
{ | ||
loc: 'https://example.com/about', | ||
}, | ||
{ | ||
loc: 'https://example.com/contact', | ||
}, | ||
]; | ||
const lowPriority = [ | ||
'https://example.com/about', | ||
'https://example.com/services', | ||
'https://example.com/contact', | ||
'https://example.com/legal', | ||
'https://example.com/privacy', | ||
'https://example.com/terms', | ||
]; | ||
const xmlTags = [ | ||
'<?xml version="1.0" encoding="UTF-8"?>', | ||
'<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"', | ||
'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">', | ||
'<priority>1.00</priority>', | ||
'<priority>0.80</priority>', | ||
'</urlset>', | ||
'<?xml version="1.0" encoding="UTF-8"?>', | ||
'<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"', | ||
'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">', | ||
'<priority>1.00</priority>', | ||
'<changefreq>monthly</changefreq>', | ||
'</urlset>', | ||
]; | ||
const expected = merge( | ||
highPriority.map(l => `<loc>${l}</loc>`), | ||
lowPriority.map(l => `<loc>${l}</loc>`), | ||
xmlTags, | ||
links.map((l) => `<loc>${l}</loc>`), | ||
xmlTags | ||
); | ||
describe('sitemap-genesis', () => { | ||
it('should work', () => { | ||
sitemaps(highPriority, lowPriority, filePath, (xml, error) => { | ||
expected.forEach((tag) => { | ||
assert.equal(true, xml.includes(tag)); | ||
}); | ||
assert.equal(null, error); | ||
}); | ||
describe('sitemaps', () => { | ||
it('should work', () => { | ||
const xml = sitemaps(filePath, links); | ||
expected.forEach((tag) => { | ||
assert.equal(true, xml.includes(tag)); | ||
}); | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
13
0
52
5821
8
76