Skip to content

Commit

Permalink
Merge pull request #3882 from seaoak/feature/enable-exclude-and-inclu…
Browse files Browse the repository at this point in the history
…de-options-for-files-in-asset-folder

fix(exclude/include): apply options to files in post asset folder
  • Loading branch information
curbengh authored Dec 1, 2019
2 parents a07cd68 + 4775a2c commit e423773
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 19 deletions.
6 changes: 1 addition & 5 deletions lib/plugins/processor/asset.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,7 @@ module.exports = ctx => {

return {
pattern: new Pattern(path => {
if (common.isTmpFile(path) || common.isMatch(path, ctx.config.exclude)) return;

if (common.isHiddenFile(path) && !common.isMatch(path, ctx.config.include)) {
return;
}
if (common.isExcludedFile(path, ctx.config)) return;

return {
renderable: ctx.render.isRenderable(path) && !common.isMatch(path, ctx.config.skip_render)
Expand Down
20 changes: 15 additions & 5 deletions lib/plugins/processor/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ const micromatch = require('micromatch');

const DURATION_MINUTE = 1000 * 60;

function isMatch(path, patterns) {
if (!patterns) return false;

return micromatch.isMatch(path, patterns);
}

function isTmpFile(path) {
const last = path[path.length - 1];
return last === '%' || last === '~';
Expand All @@ -15,13 +21,21 @@ function isHiddenFile(path) {
return /(^|\/)[_.]/.test(path);
}

function isExcludedFile(path, config) {
if (isTmpFile(path)) return true;
if (isMatch(path, config.exclude)) return true;
if (isHiddenFile(path) && !isMatch(path, config.include)) return true;
return false;
}

exports.ignoreTmpAndHiddenFile = new Pattern(path => {
if (isTmpFile(path) || isHiddenFile(path)) return false;
return true;
});

exports.isTmpFile = isTmpFile;
exports.isHiddenFile = isHiddenFile;
exports.isExcludedFile = isExcludedFile;

exports.toDate = date => {
if (!date || moment.isMoment(date)) return date;
Expand All @@ -46,8 +60,4 @@ exports.timezone = (date, timezone) => {
return new Date(ms - diff);
};

exports.isMatch = (path, patterns) => {
if (!patterns) return false;

return micromatch.isMatch(path, patterns);
};
exports.isMatch = isMatch;
2 changes: 1 addition & 1 deletion lib/plugins/processor/post.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ module.exports = ctx => {
}).catch(err => {
if (err.cause && err.cause.code === 'ENOENT') return [];
throw err;
}).filter(item => !common.isTmpFile(item) && !common.isHiddenFile(item)).map(item => {
}).filter(item => !common.isExcludedFile(item, ctx.config)).map(item => {
const id = join(assetDir, item).substring(baseDirLength).replace(/\\/g, '/');
const asset = PostAsset.findById(id);
if (asset) return undefined;
Expand Down
43 changes: 35 additions & 8 deletions test/scripts/processors/post.js
Original file line number Diff line number Diff line change
Expand Up @@ -791,6 +791,8 @@ describe('post', () => {

it('post - post_asset_folder enabled', () => {
hexo.config.post_asset_folder = true;
hexo.config.exclude = ['**.png'];
hexo.config.include = ['**/_fizz.*'];

const body = [
'title: "Hello world"',
Expand All @@ -804,27 +806,52 @@ describe('post', () => {
renderable: true
});

const assetId = 'source/_posts/foo/bar.jpg';
const assetPath = pathFn.join(hexo.base_dir, assetId);
const assetFiles = [
'bar.jpg',
'baz.png',
'_fizz.jpg',
'_buzz.jpg'
].map(filename => {
const id = `source/_posts/foo/${filename}`;
const path = pathFn.join(hexo.base_dir, id);
const contents = filename.replace(/\.\w+$/, '');
return {
id,
path,
contents
};
});

return Promise.all([
fs.writeFile(file.source, body),
fs.writeFile(assetPath, '')
...assetFiles.map(obj => fs.writeFile(obj.path, obj.contents))
]).then(() => process(file)).then(() => {
const post = Post.findOne({source: file.path});
const asset = PostAsset.findById(assetId);
const assets = assetFiles.map(obj => PostAsset.findById(obj.id));

asset._id.should.eql(assetId);
asset.post.should.eql(post._id);
asset.modified.should.be.true;
[assets[0]].should.not.eql([undefined]);
assets[0]._id.should.eql(assetFiles[0].id);
assets[0].post.should.eql(post._id);
assets[0].modified.should.be.true;

[assets[1]].should.eql([undefined]);

[assets[2]].should.not.eql([undefined]);
assets[2]._id.should.eql(assetFiles[2].id);
assets[2].post.should.eql(post._id);
assets[2].modified.should.be.true;

[assets[3]].should.eql([undefined]);

return post.remove();
}).finally(() => {
hexo.config.post_asset_folder = false;
hexo.config.exclude = undefined;
hexo.config.include = undefined;

return Promise.all([
fs.unlink(file.source),
fs.unlink(assetPath)
...assetFiles.map(obj => fs.unlink(obj.path))
]);
});
});
Expand Down

0 comments on commit e423773

Please sign in to comment.