Simple demo using the parse-sitemap package .
Live demo page: http://test-parse-sitemap.surge.sh/
git clone git@github.com:nickFalcone/test-parse-sitemap.git
cd test-parse-sitemap
npm install # @nfalcone/parse-xmlsitemap http-server surge
http-server # starts local server
surge --domain test-parse-sitemap.surge.sh
# publishes to http://test-parse-sitemap.surge.sh/
Import the parseXmlSitemap
module directly, or use a module bundler of your choice.
Then, in an async function, await parseXmlSitemap('./path-to/sitemap.xml')
supplying the path to your sitemap file:
import parseXmlSitemap from 'https://unpkg.com/@nfalcone/parse-xmlsitemap@0.2.0/index.min.js';
(async () => {
const ul = document.getElementById('sitemapList');
const urls = await parseXmlSitemap('./path-to/sitemap.xml'); // despite the warning, await is needed here.
})();
In the example above, urls
is an array of URL objects. Each URL object contains properties including href.
Assuming an <ul id="sitemapList"></ul>
, we can add list items for our site's URLs:
import parseXmlSitemap from 'https://unpkg.com/@nfalcone/parse-xmlsitemap@0.2.0/index.min.js';
(async () => {
const ul = document.getElementById('sitemapList');
const urls = await parseXmlSitemap('./sitemap.xml'); // despite the warning, await is needed here.
urls.forEach((url) => {
const li = document.createElement('li');
li.innerText = url.href;
ul.appendChild(li);
});
})();