sitemap-builder.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. "use strict";
  2. var __assign = (this && this.__assign) || function () {
  3. __assign = Object.assign || function(t) {
  4. for (var s, i = 1, n = arguments.length; i < n; i++) {
  5. s = arguments[i];
  6. for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
  7. t[p] = s[p];
  8. }
  9. return t;
  10. };
  11. return __assign.apply(this, arguments);
  12. };
  13. var __rest = (this && this.__rest) || function (s, e) {
  14. var t = {};
  15. for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
  16. t[p] = s[p];
  17. if (s != null && typeof Object.getOwnPropertySymbols === "function")
  18. for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
  19. if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
  20. t[p[i]] = s[p[i]];
  21. }
  22. return t;
  23. };
  24. var __read = (this && this.__read) || function (o, n) {
  25. var m = typeof Symbol === "function" && o[Symbol.iterator];
  26. if (!m) return o;
  27. var i = m.call(o), r, ar = [], e;
  28. try {
  29. while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
  30. }
  31. catch (error) { e = { error: error }; }
  32. finally {
  33. try {
  34. if (r && !r.done && (m = i["return"])) m.call(i);
  35. }
  36. finally { if (e) throw e.error; }
  37. }
  38. return ar;
  39. };
  40. var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
  41. if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
  42. if (ar || !(i in from)) {
  43. if (!ar) ar = Array.prototype.slice.call(from, 0, i);
  44. ar[i] = from[i];
  45. }
  46. }
  47. return to.concat(ar || Array.prototype.slice.call(from));
  48. };
  49. var __values = (this && this.__values) || function(o) {
  50. var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
  51. if (m) return m.call(o);
  52. if (o && typeof o.length === "number") return {
  53. next: function () {
  54. if (o && i >= o.length) o = void 0;
  55. return { value: o && o[i++], done: !o };
  56. }
  57. };
  58. throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
  59. };
  60. exports.__esModule = true;
  61. exports.SitemapBuilder = void 0;
  62. /**
  63. * Builder class to generate xml and robots.txt
  64. * Returns only string values
  65. */
  66. var SitemapBuilder = /** @class */ (function () {
  67. function SitemapBuilder() {
  68. }
  69. /**
  70. * Create XML Template
  71. * @param content
  72. * @returns
  73. */
  74. SitemapBuilder.prototype.withXMLTemplate = function (content) {
  75. return "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\" xmlns:news=\"http://www.google.com/schemas/sitemap-news/0.9\" xmlns:xhtml=\"http://www.w3.org/1999/xhtml\" xmlns:mobile=\"http://www.google.com/schemas/sitemap-mobile/1.0\" xmlns:image=\"http://www.google.com/schemas/sitemap-image/1.1\" xmlns:video=\"http://www.google.com/schemas/sitemap-video/1.1\">\n".concat(content, "</urlset>");
  76. };
  77. /**
  78. * Generates sitemap-index.xml
  79. * @param allSitemaps
  80. * @returns
  81. */
  82. SitemapBuilder.prototype.buildSitemapIndexXml = function (allSitemaps) {
  83. var _a;
  84. return __spreadArray(__spreadArray([
  85. '<?xml version="1.0" encoding="UTF-8"?>',
  86. '<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">'
  87. ], __read(((_a = allSitemaps === null || allSitemaps === void 0 ? void 0 : allSitemaps.map(function (x) { return "<sitemap><loc>".concat(x, "</loc></sitemap>"); })) !== null && _a !== void 0 ? _a : [])), false), [
  88. '</sitemapindex>',
  89. ], false).join('\n');
  90. };
  91. /**
  92. * Normalize sitemap field keys to stay consistent with <xsd:sequence> order
  93. * @link https://www.w3schools.com/xml/el_sequence.asp
  94. * @link https://github.com/iamvishnusankar/next-sitemap/issues/345
  95. * @param x
  96. * @returns
  97. */
  98. SitemapBuilder.prototype.normalizeSitemapField = function (x) {
  99. var loc = x.loc, lastmod = x.lastmod, changefreq = x.changefreq, priority = x.priority, restProps = __rest(x
  100. // Return keys in following order
  101. , ["loc", "lastmod", "changefreq", "priority"]);
  102. // Return keys in following order
  103. return __assign({ loc: loc, lastmod: lastmod, changefreq: changefreq, priority: priority }, restProps);
  104. };
  105. /**
  106. * Generates sitemap.xml
  107. * @param fields
  108. * @returns
  109. */
  110. SitemapBuilder.prototype.buildSitemapXml = function (fields) {
  111. var _this = this;
  112. var content = fields
  113. .map(function (x) {
  114. var e_1, _a;
  115. // Normalize sitemap field keys to stay consistent with <xsd:sequence> order
  116. var field = _this.normalizeSitemapField(x);
  117. // Field array to keep track of properties
  118. var fieldArr = [];
  119. try {
  120. // Iterate all object keys and key value pair to field-set
  121. for (var _b = __values(Object.keys(field)), _c = _b.next(); !_c.done; _c = _b.next()) {
  122. var key = _c.value;
  123. // Skip reserved keys
  124. if (['trailingSlash'].includes(key)) {
  125. continue;
  126. }
  127. if (field[key]) {
  128. if (key !== 'alternateRefs') {
  129. fieldArr.push("<".concat(key, ">").concat(field[key], "</").concat(key, ">"));
  130. }
  131. else {
  132. var altRefField = _this.buildAlternateRefsXml(field.alternateRefs);
  133. fieldArr.push(altRefField);
  134. }
  135. }
  136. }
  137. }
  138. catch (e_1_1) { e_1 = { error: e_1_1 }; }
  139. finally {
  140. try {
  141. if (_c && !_c.done && (_a = _b["return"])) _a.call(_b);
  142. }
  143. finally { if (e_1) throw e_1.error; }
  144. }
  145. // Append previous value and return
  146. return "<url>".concat(fieldArr.join(''), "</url>\n");
  147. })
  148. .join('');
  149. return this.withXMLTemplate(content);
  150. };
  151. /**
  152. * Generate alternate refs.xml
  153. * @param alternateRefs
  154. * @returns
  155. */
  156. SitemapBuilder.prototype.buildAlternateRefsXml = function (alternateRefs) {
  157. if (alternateRefs === void 0) { alternateRefs = []; }
  158. return alternateRefs
  159. .map(function (alternateRef) {
  160. return "<xhtml:link rel=\"alternate\" hreflang=\"".concat(alternateRef.hreflang, "\" href=\"").concat(alternateRef.href, "\"/>");
  161. })
  162. .join('');
  163. };
  164. return SitemapBuilder;
  165. }());
  166. exports.SitemapBuilder = SitemapBuilder;