Skip to content

Commit

Permalink
fix: realtive background image url don't show
Browse files Browse the repository at this point in the history
  • Loading branch information
Kei Son committed Sep 6, 2018
1 parent 7f83050 commit 3dd169e
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 14 deletions.
37 changes: 24 additions & 13 deletions src/plugin.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as cheerio from 'cheerio';
import * as _ from 'lodash';

import { Section, convertImageSrcSet, host, sanitizeImageSrc, parseParams } from './util';

Expand Down Expand Up @@ -35,19 +36,29 @@ export function attached(req) {
};
}

export function backgroundImage(section: Section): Section {
const $ = cheerio.load(section.body);
const imgs = $('img');
imgs.map((_i, el) => {
const img = $(el);
const originalSize = !img.attr('height') && !img.attr('width');
const isEmoticon = img.hasClass('emoticon');
if (!originalSize || isEmoticon) return;
section.background = img.data('image-src');
img.remove();
});
section.body = $.html();
return section;
function hostToAbsolute(req) {
if (req.baseUrl) return req.baseUrl;
const hostFromHeaders: string = _.get(req, 'headers.host');
if (hostFromHeaders.startsWith('http://')) return hostFromHeaders;
return 'http://' + hostFromHeaders;
}

export function backgroundImage(req) {
return (section: Section): Section => {
const $ = cheerio.load(section.body);
const imgs = $('img');
imgs.map((_i, el) => {
const img = $(el);
const originalSize = !img.attr('height') && !img.attr('width');
const isEmoticon = img.hasClass('emoticon');
if (!originalSize || isEmoticon) return;
section.background = hostToAbsolute(req) + '/image' + sanitizeImageSrc(img.data('image-src'));
//section['background-image'] = req.baseUrl + '/image' + sanitizeImageSrc(img.data('image-src'));
img.remove();
});
section.body = $.html();
return section;
};
}

export function emoticon(req) {
Expand Down
2 changes: 1 addition & 1 deletion src/routes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ router.get('/page/:id', (req, res, next) => {
function map(section: Section) {
const middlewares: ((s: Section) => Section)[] = [
attached(req),
backgroundImage,
backgroundImage(req),
emoticon(req),
gliffy(req),
mermaid,
Expand Down

0 comments on commit 3dd169e

Please sign in to comment.