@canalplus/readme.doc
Version:
Readme's an Extremely Accessible Documentation MakEr
62 lines (61 loc) • 1.81 kB
JavaScript
/**
* Class responsible for creating and managing a sitemap.
* It allows adding URLs to the sitemap and generating an XML file of the sitemap.
* This is used by search engines to crawl and index files.
*/
export class SiteMapCreator {
constructor() {
this.siteMapEntry = [];
}
/**
* Adds a new URL to the sitemap.
* The `lastmod` (last modification date) is automatically set to the current date in the format `YYYY-MM-DD`.
*
* @param {string} loc - The URL to be added to the sitemap.
* @example
* const sitemap = new SiteMapCreator();
* sitemap.addToSiteMap('https://mydoc.com');
*/
addToSiteMap(loc) {
this.siteMapEntry.push({
loc,
lastmod: new Date().toISOString().split("T")[0],
});
}
/**
* Generates the XML for the sitemap.
* This method converts the list of entries into a properly formatted XML string.
*
* @returns {string} The XML string representing the sitemap.
*/
generateSiteMapXML() {
return `<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
${this.siteMapEntry
.map((entry) => `<url>
<loc>${escapeXml(entry.loc)}</loc>
<lastmod>${escapeXml(entry.lastmod)}</lastmod>
</url>
`)
.join("")}
</urlset>`;
}
}
function escapeXml(unsafe) {
return unsafe.replace(/[<>&'"]/g, (c) => {
switch (c) {
case "<":
return "<";
case ">":
return ">";
case "&":
return "&";
case "'":
return "'";
case '"':
return """;
default:
return "";
}
});
}