hexo/node_modules/hexo-generator-feed/lib/autodiscovery.js

27 lines
925 B
JavaScript
Raw Normal View History

2023-09-25 15:58:56 +08:00
'use strict';
const { url_for } = require('hexo-util');
function autodiscoveryInject(data) {
const { config } = this;
const { feed } = config;
const { path, type } = feed;
let autodiscoveryTag = '';
if (data.match(/type=['|"]?application\/(atom|rss)\+xml['|"]?/i) || feed.autodiscovery === false) return;
if (typeof type === 'string') {
autodiscoveryTag += `<link rel="alternate" href="${url_for.call(this, path)}" `
+ `title="${config.title}" type="application/${type.replace(/2$/, '')}+xml">\n`;
} else {
type.forEach((feedType, i) => {
autodiscoveryTag += `<link rel="alternate" href="${url_for.call(this, path[i])}" `
+ `title="${config.title}" type="application/${feedType.replace(/2$/, '')}+xml">\n`;
});
}
return data.replace(/<head>(?!<\/head>).+?<\/head>/s, str => str.replace('</head>', `${autodiscoveryTag}</head>`));
}
module.exports = autodiscoveryInject;