nickFalcone / test-parse-sitemap

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

test-parse-sitemap

Simple demo using the parse-sitemap package NPM info.

Live demo page: http://test-parse-sitemap.surge.sh/

Building the demo page

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/

Using parseXmlSitemap

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);
  });
})();

About


Languages

Language:JavaScript 58.7%Language:HTML 41.3%