UNPKG

@webdoc/template-library

Version:

Goodies for @webdoc template packages! See @webdoc/legacy-template for an example!

53 lines (51 loc) 2.91 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Sitemap = void 0; var fse = _interopRequireWildcard(require("fs-extra")); var _path = _interopRequireDefault(require("path")); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); } function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; } function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } class Sitemap { constructor(dir, domain, root) { _defineProperty(this, "urls", []); _defineProperty(this, "dir", void 0); _defineProperty(this, "domain", void 0); _defineProperty(this, "root", void 0); this.dir = dir; this.domain = domain; this.root = root; if (this.domain.charAt(this.domain.length - 1) !== "/") { this.domain += "/"; } } attachTo(pipeline) { } run(input, pipelineData = {}) { if (pipelineData.outputFile) { this.urls.push(pipelineData.outputFile); } return input; } close() { const xml = `<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd"> ${this.urls.map(url => ` <url> <loc>${this.domain + _path.default.join(this.root, _path.default.relative(this.dir, url))}</loc> </url>`).join("\n")} </urlset>`; fse.outputFile(_path.default.join(this.dir, "sitemap.xml"), xml, err => { if (err) throw err; }); } clone() { const clone = new Sitemap(this.dir, this.domain, this.root); clone.urls = [...this.urls]; return clone; } } exports.Sitemap = Sitemap;