Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x | import { PathOrFileDescriptor, readFileSync, writeFileSync } from 'fs';
import gulp from 'gulp';
import gulpDom from 'gulp-dom';
import hexo from 'hexo';
import { full_url_for, gravatar } from 'hexo-util';
import nunjucks from 'nunjucks';
import { join } from 'upath';
import envNunjucks from './utils/nunjucks-env';
import { commonIgnore, getConfig } from './_config';
const env = envNunjucks();
gulp.task('feed', function (done) {
const config = getConfig();
const instance = new hexo(config.cwd);
instance.init().then(() => {
instance.load().then(() => {
env.addFilter('formatUrl', (str) => {
return full_url_for.call(instance, str);
});
function build(tmplSrc: PathOrFileDescriptor, dest: PathOrFileDescriptor) {
const template = nunjucks.compile(readFileSync(tmplSrc, 'utf-8'), env);
let posts = instance.locals.get('posts');
posts = posts.sort('-date');
posts = posts.filter((post) => {
return post.draft !== true;
});
const { email, feed, url: urlCfg } = config;
const { icon: iconCfg } = feed;
let url = urlCfg;
if (url[url.length - 1] !== '/') url += '/';
// remove broken hexo lang shortcode
if (url.includes(':lang/')) url = url.replace('/:lang/', '/');
let icon = '';
if (iconCfg) icon = full_url_for.call(instance, iconCfg);
else if (email) icon = gravatar(email, {});
const feed_url = full_url_for.call(instance, 'rss.xml');
const data = template.render({
config,
url,
icon,
posts,
feed_url
});
writeFileSync(dest, data);
}
const templateRSS = join(__dirname, '_config_template_rss.xml');
const destRSS = join(config.cwd, config.public_dir, 'rss.xml');
build(templateRSS, destRSS);
const templateATOM = join(__dirname, '_config_template_atom.xml');
const destATOM = join(config.cwd, config.public_dir, 'atom.xml');
build(templateATOM, destATOM);
const baseURL = config.url.endsWith('/') ? config.url : config.url + '/';
const publicDir = join(config.cwd, config.public_dir);
gulp
.src('**/*.html', { cwd: publicDir, ignore: commonIgnore })
.pipe(
gulpDom(function () {
// auto discovery rss
if (
this.querySelectorAll(`link[href="${baseURL}rss.xml"]`).length === 0 &&
this.querySelectorAll(`link[href="/rss.xml"]`).length === 0
) {
this.head.innerHTML += `<link id="rss-site-url" type="application/rss+xml" rel="alternate" href="${baseURL}rss.xml" />`;
}
// auto discovery atom
if (
this.querySelectorAll(`link[href="${baseURL}atom.xml"]`).length === 0 &&
this.querySelectorAll(`link[href="/atom.xml"]`).length === 0
) {
this.head.innerHTML += `<link id="atom-site-url" type="application/atom+xml" rel="alternate" href="${baseURL}atom.xml" />`;
}
//this.querySelectorAll('body')[0].setAttribute('data-version', '1.0');
})
)
.pipe(gulp.dest(publicDir))
.once('end', () => done());
});
});
});
|