sitemap styling

Hi,

a customer wants to have a “nice” sitemap of his page.

My idea is to grab the sitemap.xml from HubSpot(Settings->Domains&URLs->Sitemap)and display it as a list like

<ul>
<li>Home</li>
<li>Page2</li>
...
</ul>

and style it with css.

Is this a good idea or are there other ways to display the sitemap.xml in a “nice” layout?

regards,

Anton

Hey Anton, you can use JavaScript or jQuery to pull from an XML feed. For example, the code below takes my sitemap (you can change the URL to your own) and adds it to a #sitemap-list ul I’ve created in a Rich Text module (

<ul id=“sitemap-list”></ul>

).

$.get("http://www.stephanieogaygarcia.com/sitemap.xml", function(data) {
 var $XML = $(data);
 $XML.find("url").each(function() {
 var $this = $(this),
 item = {
 loc: $this.find("loc").text(),
 lastmod: $this.find("lastmod").text()
 };
 $('#sitemap-list').append($('<li/>').text(item.loc));
 });
});

(Note that this will only work on a published page since it gets a little fussy about pulling from your domain to app.hubspot.com on the Design Manager).

You could also add more complex HTML wrapper using jQuery but, since the sitemap.xml file only has the URL and last modified information, I don’t think you’ll be able to pull the page names from it.


|300HubSpot Design / Development / Automation

Website | Contact

If this helped, please mark it as the solution to your question, thanks!

Hi @Stephanie-OG,

thanks for the code.

It works fine, but since it doesn’t pull the names from the listing I’ve decided to write a “hard” sitemap listing with listing elements.

regards

Anton

Hello Stephanie

I have been trying to implement this on our own sitemap page but without much success don’t really know what i am doing.

I have added the code to the head of the page and then the html code on the pages itself.

Does the jquery reference file need to be above the code or can it be in the footer?

Where should each piece of your suggested code below be in the scheme of things and what is best practice.
Thanks

Gill

I am also trying to rebuild a html sitemap from the hubspot sitemap.xml page and used a js function to try and fetch all the relevant data such as url and title to a more or less acceptable standard. It is only the first step and will probably need to turn it into a jQuery function to have it be fully intergratable.

it only works within the console while on the sitemap.xml page since I am still only testing everything out. hopefully jQuery will allow me to make something much simpler that wont rely as much on XMLHttpRequests… any input or help is of course highly appreciated!

var rawWebMap = document.querySelectorAll(‘.collapsible-content .text’);
var nbrRawPages= rawWebMap.length;
var webMap = {};

for(i=0;i<nbrRawPages;i++){
if(rawWebMap[i].textContent.includes(“http”))
{
var xmlHttp = new XMLHttpRequest();
xmlHttp.open(“GET”,rawWebMap[i].textContent,false);
xmlHttp.send(null);
var patt=/(?<=(<title>))(.*?)+?(?=<\s*\/\s*title>)/g;
var hyperTxt =xmlHttp.responseText;
webMap[rawWebMap[i].textContent]=hyperTxt.match(patt);
}
}

var nbrPages=webMap.length;
var uList=document.createElement(“ul”);

for(var key in webMap){
var item=document.createElement(“li”);
var link=document.createElement(“a”);
var theValue=[key]
link.setAttribute(‘href’,theValue);
var title=document.createTextNode(webMap[key]);
link.appendChild(title);
item.appendChild(link);
uList.appendChild(item);

}

var placeIn=document.getElementsByTagName(‘body’);
var firstChild=document.getElementById(“webkit-xml-viewer-source-xml”);
placeIn[0].insertBefore(uList,firstChild);