Skip to content

Commit

Permalink
fix: theme.assets work with post_asset_folder (#170)
Browse files Browse the repository at this point in the history
  • Loading branch information
ikeq authored Nov 6, 2019
1 parent 78b7aca commit e21f8ae
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 30 deletions.
13 changes: 5 additions & 8 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
language: node_js
node_js:
- "10"

cache:
directories:
- node_modules
npm: true

branches:
only:
- master
node_js:
- "10"

before_install:
script:
- git clone https://github.com/hexojs/hexo-theme-unit-test generate_test
- git clone https://github.com/ikeq/hexo-theme-inside.git generate_test/themes/landscape
- cd generate_test
- npm install
- hexo g & hexo g
- cd ..
- npm test
38 changes: 23 additions & 15 deletions lib/filter/post.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
const cheerio = require('cheerio');
const { date } = require('hexo/lib/plugins/helper/date');
const { snippet, parseToc, isObject, isEmptyObject, localeId, pick, trimHtml } = require('../utils');
const bounded = '<div class="article-bounded"></div>';
const table = '<div class="article-table"></div>';
const { snippet, parseToc, isObject, isEmptyObject, localeId, pick, trimHtml } = require('../utils');
const date_formats = [
'll', // Sep 4, 1986
'L', // 09/04/1986
'MM-DD' // 06-17
]
];

// cache
let hasComments, hasReward, hasToc, copyright, dateHelper, uriReplacer;
Expand All @@ -29,17 +29,23 @@ module.exports = function (data) {
copyright = theme.copyright;
if (dateHelper === undefined)
dateHelper = date.bind({ page: { lang: localeId(config.language, true) }, config })
if (uriReplacer === undefined)
uriReplacer = theme.assets ?
(() => {
const { prefix, suffix } = theme.assets;
return s => /^(\/\/|http|data\:image)/.test(s) ?
s :
s.replace(/^\/*(.*[^\/])\/*$/, prefix + '/$1' + suffix)
})() :
config.post_asset_folder ?
(s, p) => /\//.test(s) ? s : `/${p}/${s}` :
s => s;
if (uriReplacer === undefined) {
uriReplacer = (() => {
let assetsFn = src => src;
if (theme.assets) {
const prefix = theme.assets.prefix ? theme.assets.prefix + '/' : ''
const suffix = theme.assets.suffix || ''
assetsFn = src => prefix + `${src}${suffix}`.replace(/\/{2,}/g, '/')
}

return (src, assetPath) => {
assetPath = assetPath ? assetPath + '/' : ''

// skip both external and absolute path
return /^(\/\/?|http|data\:image)/.test(src) ? src : assetsFn(`${assetPath}${src}`);
}
})();
}

// pre format date for i18n
data.date_formatted = date_formats.reduce((ret, format) => {
Expand All @@ -57,8 +63,10 @@ module.exports = function (data) {
// comments
data.comments = hasComments && data.comments !== false;

// asset path
const assetPath = isPage ? trimHtml(data.path, true) : data.link;
// asset path (for post_asset_folder)
const assetPath = config.post_asset_folder
? (isPage ? trimHtml(data.path, true) : data.link)
: undefined;

// Make sure articles without titles are also accessible
if (!data.title) data.title = data.slug
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "hexo-theme-inside",
"version": "2.5.0",
"version": "2.5.1",
"description": "❤️ SPA, flat and clean theme for Hexo.",
"scripts": {
"test": "jasmine --config=test/jasmine.json"
Expand Down
76 changes: 70 additions & 6 deletions test/scripts/filters/post.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
'use strict';

describe('post', function () {
const post = require('../../../lib/filter/post');
const filterPath = require.resolve('../../../lib/filter/post');
const post = {
call(ctx, arg) {
delete require.cache[filterPath]
return require(filterPath).call(ctx, arg)
}
}

beforeEach(function () {
this.ctx = {
Expand Down Expand Up @@ -170,19 +176,77 @@ describe('post', function () {
source: 'test/index.md',
content: '<img src="img/sample.jpg">',
};
const { ctx } = this;

post.call(this.ctx, data);
post.call(ctx, data);
expect(data.thumbnail).toBe('https://sample.com/img/sample.jpg?q=80')
expect(data.content).toBe('<img src="https://sample.com/img/sample.jpg?q=80" class="article-img">')
expect(data.content).toBe('<img src="https://sample.com/img/sample.jpg?q=80" class="article-img">');

// suffix only
data.thumbnail = 'img/sample.jpg'
data.content = '<img src="img/sample.jpg">'
ctx.theme.config.assets = { suffix: '?q=80' }
post.call(ctx, data);
expect(data.thumbnail).toBe('img/sample.jpg?q=80')
expect(data.content).toBe('<img src="img/sample.jpg?q=80" class="article-img">');

// prefix only
data.thumbnail = 'img/sample.jpg'
data.content = '<img src="img/sample.jpg">'
ctx.theme.config.assets = { prefix: 'https://sample.com' }
post.call(ctx, data);
expect(data.thumbnail).toBe('https://sample.com/img/sample.jpg')
expect(data.content).toBe('<img src="https://sample.com/img/sample.jpg" class="article-img">');

data.layout = 'page'
post.call(this.ctx, data);

data.thumbnail = 'img/sample.jpg';
post.call(this.ctx, data);
post.call(ctx, data);
expect(data.thumbnail).toBe('img/sample.jpg')
});

it('post_asset_folder', function () {
const data = {
layout: 'post',
thumbnail: 'sample.jpg',
excerpt: '',
source: 'test/index.md',
content: '<img src="sample.jpg">',
path: 'post/test'
};
const { ctx } = this;

// post_asset_folder off, theme.assets off
delete ctx.theme.config.assets
post.call(ctx, data)
expect(data.thumbnail).toBe('sample.jpg')
expect(data.content).toBe('<img src="sample.jpg" class="article-img">')

// post_asset_folder on, theme.assets off
data.thumbnail = 'sample.jpg'
data.content = '<img src="sample.jpg">'
ctx.config.post_asset_folder = true
post.call(ctx, data)
expect(data.thumbnail).toBe('post/test/sample.jpg')
expect(data.content).toBe('<img src="post/test/sample.jpg" class="article-img">')

// post_asset_folder off, theme.assets on
data.thumbnail = 'img/sample.jpg'
data.content = '<img src="img/sample.jpg">'
ctx.config.post_asset_folder = false
ctx.theme.config.assets = { prefix: 'https://sample.com', suffix: '?q=80' }
post.call(ctx, data)
expect(data.thumbnail).toBe('https://sample.com/img/sample.jpg?q=80')
expect(data.content).toBe('<img src="https://sample.com/img/sample.jpg?q=80" class="article-img">')

// post_asset_folder on, theme.assets on
data.thumbnail = 'img/sample.jpg'
data.content = '<img src="img/sample.jpg">'
ctx.config.post_asset_folder = true
post.call(ctx, data)
expect(data.thumbnail).toBe('https://sample.com/post/test/img/sample.jpg?q=80')
expect(data.content).toBe('<img src="https://sample.com/post/test/img/sample.jpg?q=80" class="article-img">')
})

it('escape with data:image', function () {
const data = {
layout: 'post',
Expand Down

0 comments on commit e21f8ae

Please sign in to comment.