Skip to content

Commit

Permalink
feat(plugin): p and img also can be fragmented
Browse files Browse the repository at this point in the history
fix #20 #21
  • Loading branch information
heycalmdown committed Apr 22, 2017
1 parent b2b1419 commit 546378f
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 18 deletions.
34 changes: 25 additions & 9 deletions src/plugin.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as cheerio from 'cheerio';

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

export function mermaid(section) {
const $ = cheerio.load(section);
Expand Down Expand Up @@ -119,18 +119,34 @@ export function code(section) {
return section;
}

export function fragment(section) {
const $ = cheerio.load(section);
const liList = $('li');
if (liList.length === 0) return section;
liList.each((i, el) => {
function fragmentTags($, tagName) {
const list = $(tagName);
if (list.length === 0) return;
list.each((i, el) => {
el = $(el);
let text = el.text();
if (text.includes('⏎')) {
text = text.replace('⏎', '');
el.text(text);
el.addClass('fragment');
const children = el.children();
if (children.length === 0) {
text = text.replace('⏎', '');
el.text(text);
el.addClass('fragment');
return;
}
children.each((i, child) => {
if (child.next.data && child.next.data.includes('⏎')) {
const text = child.next.data.replace('⏎', '');
child.next.data = text;
$(child).addClass('fragment');
}
});
}
});
}

export function fragment(section) {
const $ = cheerio.load(section);
fragmentTags($, 'li');
fragmentTags($, 'p');
return $.html();
}
4 changes: 0 additions & 4 deletions src/routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,6 @@ router.get('/', (req, res, next) => {
});
});

function convertImageSrcSet(baseUrl, imageSrcSet) {
return imageSrcSet.split(',').map(src => baseUrl + '/image' + src).join(',');
}

router.get('/page/:id', (req, res, next) => {
const theme = THEMES[req.query.theme] || 'black';
const transition = TRANSITIONS[req.query.transition] || 'slide';
Expand Down
31 changes: 26 additions & 5 deletions src/spec/index.spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { sanitizeImageSrc, setHost, splitPinnedPages, parseParams } from '../util';
import * as cheerio from 'cheerio';

import { convertImageSrcSet, sanitizeImageSrc, setHost, splitPinnedPages, parseParams } from '../util';
import { fragment } from '../plugin';

describe('miniseminar', () => {
setHost('https://confluency.atlassian.net');
Expand All @@ -13,9 +16,7 @@ describe('miniseminar', () => {
it('should convert image-srcset', () => {
const input = '/wiki/download/thumbnails/2097156/IMG_7444.jpg?width=550&height=500 2x,/wiki/download/thumbnails/2097156/IMG_7444.jpg?width=275&height=250 1x';
const output = '/image/wiki/download/thumbnails/2097156/IMG_7444.jpg?width=550&height=500 2x,/image/wiki/download/thumbnails/2097156/IMG_7444.jpg?width=275&height=250 1x';
function convertImageSrcSet(baseUrl, imageSrcSet) {
return imageSrcSet.split(',').map(src => baseUrl + '/image' + src).join(',');
}

expect(convertImageSrcSet('', input)).toEqual(output);
});
it('should split PINNED_PAGES', () => {
Expand All @@ -31,5 +32,25 @@ describe('miniseminar', () => {
gutter: 'false',
theme: 'Confluence'
});
})
});
it('should convert ⏎ in the <li>', () => {
const a = '<ul><li>first⏎</li><li>second⏎</li><li>third⏎</li></ul>';
expect(fragment(a))
.toEqual(
'<ul><li class="fragment">first</li><li class="fragment">second</li><li class="fragment">third</li></ul>'
);
});
it('should convert ⏎ in the <p>', () => {
const a = '<p>first⏎</p><p>second⏎</p><p>third⏎</p>';
expect(fragment(a))
.toEqual(
'<p class="fragment">first</p><p class="fragment">second</p><p class="fragment">third</p>'
);
});
it('should convert ⏎ with around an image', () => {
expect(fragment(
`<p><span class="confluence-embedded-file-wrapper confluence-embedded-manual-size"><img class="confluence-embedded-image confluence-external-resource" height="250" src="http://cfile25.uf.tistory.com/image/23028948558D5D6844CB82" data-image-src="http://cfile25.uf.tistory.com/image/23028948558D5D6844CB82"></span>&#x23CE;</p>`))
.toEqual(
`<p><span class="confluence-embedded-file-wrapper confluence-embedded-manual-size fragment"><img class="confluence-embedded-image confluence-external-resource" height="250" src="http://cfile25.uf.tistory.com/image/23028948558D5D6844CB82" data-image-src="http://cfile25.uf.tistory.com/image/23028948558D5D6844CB82"></span></p>`);
});
});
4 changes: 4 additions & 0 deletions src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ export function setHost(h) {
host = h;
}

export function convertImageSrcSet(baseUrl, imageSrcSet) {
return imageSrcSet.split(',').map(src => baseUrl + '/image' + src).join(',');
}

export function splitPinnedPages(PINNED_PAGES) {
if (!PINNED_PAGES) return [];
return PINNED_PAGES.split(',');
Expand Down

0 comments on commit 546378f

Please sign in to comment.