-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Sitemap rewrite first commit (experimental)
- Loading branch information
Showing
6 changed files
with
384 additions
and
137 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
const fs = require('fs-extra'); | ||
const cheerio = require('cheerio'); | ||
const log = require('fancy-log'); | ||
const { URL } = require('url'); | ||
|
||
let extraPaths = []; | ||
|
||
function rewritePath(baseurl, link) { | ||
try { | ||
const url = new URL(link); | ||
const newPath = `${baseurl}/${url.pathname}`.replace('//', '/'); | ||
return `${url.origin}/${newPath}`; | ||
} catch (urlError) { | ||
return link; | ||
} | ||
} | ||
|
||
function rewriteXML(xml, baseurl) { | ||
const $ = cheerio.load(xml, { | ||
xmlMode: true | ||
}); | ||
|
||
const rootNode = $.root().children()[0]; | ||
const isIndex = rootNode && rootNode.name === 'sitemapindex'; | ||
|
||
$('loc').each(function () { | ||
const $el = $(this); | ||
const originalValue = $el.text(); | ||
const updated = rewritePath(baseurl, originalValue); | ||
$el.text(updated); | ||
|
||
if (isIndex && $el.parent()[0].name === 'sitemap') { | ||
extraPaths.push(originalValue); | ||
} | ||
}); | ||
|
||
$('xhtml\\:link').each(function () { | ||
const $el = $(this); | ||
const originalValue = $el.attr('href'); | ||
const updated = rewritePath(baseurl, originalValue); | ||
$el.attr('href', updated); | ||
}); | ||
return $.xml(); | ||
} | ||
|
||
module.exports = { | ||
rewrite: rewriteXML, | ||
|
||
/** Handles rewriting urls in sitemap(s) | ||
* | ||
* @param {string} file the absolute path to the sitemap file. | ||
* @param {string} destination the absolute path to the destination directory. | ||
* @param {string} baseurl the baseurl to prepend to the source files. | ||
*/ | ||
plugin: function (file, destination, baseurl) { | ||
extraPaths = []; | ||
if (!file) { | ||
log.error('Error rewriting XML: Invalid file specified.'); | ||
return 1; | ||
} | ||
if (!destination || !baseurl) { | ||
log.error('Error rewriting XML: No destination specified.'); | ||
return 1; | ||
} | ||
const contents = fs.readFileSync(file); | ||
const xml = contents.toString('utf-8'); | ||
|
||
if (!xml) { | ||
return 0; | ||
} | ||
const rewritten = rewriteXML(xml, baseurl); | ||
|
||
fs.writeFileSync(file, rewritten); | ||
return extraPaths.length ? extraPaths : 0; | ||
} | ||
}; |
Oops, something went wrong.