Skip to content

Commit

Permalink
prettier --write '**/*.{js,md}'
Browse files Browse the repository at this point in the history
  • Loading branch information
papandreou committed Apr 4, 2020
1 parent 3e55f81 commit 857b876
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 77 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@ Output:

```html
<div style="font-family: monospace; white-space: nowrap">
<div><img src="foo.png"></div>
<div><img src="foo.png" /></div>
</div>
```
25 changes: 13 additions & 12 deletions lib/magicPenMedia.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ module.exports = {
name: 'magicpen-media',
version: require('../package.json').version,
installInto(magicPen) {
magicPen.addStyle('media', function(media, options) {
magicPen.addStyle('media', function (media, options) {
if (typeof options === 'string') {
options = { contentType: options };
} else {
Expand Down Expand Up @@ -123,7 +123,7 @@ module.exports = {
html += '</a>';
}
return html;
}
},
},
fallback() {
function writeToDisc(data) {
Expand All @@ -135,9 +135,9 @@ module.exports = {
fileName = fallbackToDisc;
} else {
fileName = getTemporaryFilePath({
suffix: `.${mime.getExtension(contentType) ||
majorContentType ||
'data'}`
suffix: `.${
mime.getExtension(contentType) || majorContentType || 'data'
}`,
});
}
fs.writeFileSync(fileName, data);
Expand Down Expand Up @@ -173,16 +173,17 @@ module.exports = {
this.text(`${writeToDisc(media)} (${contentType})`);
} else {
this.text(
`${media.constructor.name}[${media.length}] (${contentType ||
'media'})`
`${media.constructor.name}[${media.length}] (${
contentType || 'media'
})`
);
}
}
}
},
});
});

magicPen.addStyle('image', function(image, options) {
magicPen.addStyle('image', function (image, options) {
if (typeof options === 'string') {
options = { contentType: options };
} else {
Expand All @@ -191,7 +192,7 @@ module.exports = {
this.media(image, _.extend({ contentType: 'image' }, options));
});

magicPen.addStyle('audio', function(audio, options) {
magicPen.addStyle('audio', function (audio, options) {
if (typeof options === 'string') {
options = { contentType: options };
} else {
Expand All @@ -200,13 +201,13 @@ module.exports = {
this.media(audio, _.extend({ contentType: 'audio' }, options));
});

magicPen.addStyle('video', function(video, options) {
magicPen.addStyle('video', function (video, options) {
if (typeof options === 'string') {
options = { contentType: options };
} else {
options = options || {};
}
this.media(video, _.extend({ contentType: 'video' }, options));
});
}
},
};
87 changes: 23 additions & 64 deletions test/magicPenMedia.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,21 @@ const magicPen = new MagicPen().installPlugin(require('../lib/magicPenMedia'));
const getTemporaryFilePath = require('gettemporaryfilepath');
const sinon = require('sinon');
const fs = require('fs');
const expect = require('unexpected')
.clone()
.use(require('unexpected-sinon'));
const expect = require('unexpected').clone().use(require('unexpected-sinon'));

describe('magicpen-media', () => {
describe('in HTML mode', () => {
it('should render an image given as a path', () => {
expect(
magicPen
.clone('html')
.media('foo/bar.jpg')
.toString(),
magicPen.clone('html').media('foo/bar.jpg').toString(),
'to equal',
'<div style="font-family: monospace; white-space: nowrap"><div><img src="foo/bar.jpg" title="foo/bar.jpg"></div></div>'
);
});

it('should link to the image url if link:true is provided in the options object', () => {
expect(
magicPen
.clone('html')
.media('foo/bar.jpg', { link: true })
.toString(),
magicPen.clone('html').media('foo/bar.jpg', { link: true }).toString(),
'to equal',
'<div style="font-family: monospace; white-space: nowrap"><div><a href="foo/bar.jpg"><img src="foo/bar.jpg" title="foo/bar.jpg"></a></div></div>'
);
Expand All @@ -44,10 +36,7 @@ describe('magicpen-media', () => {

it('should entitify the src attribute properly', () => {
expect(
magicPen
.clone('html')
.media('foo&bar".jpg')
.toString(),
magicPen.clone('html').media('foo&bar".jpg').toString(),
'to equal',
'<div style="font-family: monospace; white-space: nowrap"><div><img src="foo&amp;bar&quot;.jpg" title="foo&amp;bar&quot;.jpg"></div></div>'
);
Expand Down Expand Up @@ -77,10 +66,7 @@ describe('magicpen-media', () => {

it('should render an image given as a data: url', () => {
expect(
magicPen
.clone('html')
.media('data:image/jpg,base64;Zm9v')
.toString(),
magicPen.clone('html').media('data:image/jpg,base64;Zm9v').toString(),
'to equal',
'<div style="font-family: monospace; white-space: nowrap"><div><img src="data:image/jpg,base64;Zm9v"></div></div>'
);
Expand Down Expand Up @@ -124,13 +110,13 @@ describe('magicpen-media', () => {

const originalURL = global.URL;
beforeEach(() => {
global.Blob = sinon.spy(function(data, contentType) {
global.Blob = sinon.spy(function (data, contentType) {
this.data = data;
this.contentType = contentType;
});

global.URL = {
createObjectURL: sinon.spy(() => 'blob:foobarquux')
createObjectURL: sinon.spy(() => 'blob:foobarquux'),
};
});

Expand Down Expand Up @@ -217,7 +203,7 @@ describe('magicpen-media', () => {
describe('and btoa available', () => {
const originalBtoa = global.Blob;
beforeEach(() => {
global.btoa = sinon.spy(obj => Buffer.from(obj).toString('base64'));
global.btoa = sinon.spy((obj) => Buffer.from(obj).toString('base64'));
});

afterEach(() => {
Expand All @@ -243,21 +229,15 @@ describe('magicpen-media', () => {
describe('#media', () => {
it('should render an image given as a path and no Content-Type as-is with "media" in parentheses', () => {
expect(
magicPen
.clone('text')
.media('foo/bar.jpg')
.toString(),
magicPen.clone('text').media('foo/bar.jpg').toString(),
'to equal',
'foo/bar.jpg (image/jpeg)'
);
});

it('should render an image given as a path and a Content-Type of "image" as-is with "image" in parentheses', () => {
expect(
magicPen
.clone('text')
.media('foo/bar.jpg', 'image')
.toString(),
magicPen.clone('text').media('foo/bar.jpg', 'image').toString(),
'to equal',
'foo/bar.jpg (image/jpeg)'
);
Expand Down Expand Up @@ -289,21 +269,15 @@ describe('magicpen-media', () => {
describe('#image', () => {
it('should render an image given as a path and no Content-Type as-is with a looked up Content-Type', () => {
expect(
magicPen
.clone('text')
.image('foo/bar.svg')
.toString(),
magicPen.clone('text').image('foo/bar.svg').toString(),
'to equal',
'foo/bar.svg (image/svg+xml)'
);
});

it('should accept the content type as the second parameter', () => {
expect(
magicPen
.clone('text')
.image('foo/bar.jpg', 'image/jpeg')
.toString(),
magicPen.clone('text').image('foo/bar.jpg', 'image/jpeg').toString(),
'to equal',
'foo/bar.jpg (image/jpeg)'
);
Expand All @@ -322,10 +296,7 @@ describe('magicpen-media', () => {

it('should render a data url', () => {
expect(
magicPen
.clone('text')
.image('data:image/png,base64;AQID')
.toString(),
magicPen.clone('text').image('data:image/png,base64;AQID').toString(),
'to equal',
'data url (image/png)'
);
Expand All @@ -346,21 +317,15 @@ describe('magicpen-media', () => {
describe('#audio', () => {
it('should render an audio file given as a path and no Content-Type with the looked up Content-Type in parentheses', () => {
expect(
magicPen
.clone('text')
.audio('foo/bar.aiff')
.toString(),
magicPen.clone('text').audio('foo/bar.aiff').toString(),
'to equal',
'foo/bar.aiff (audio/x-aiff)'
);
});

it('should accept the content type as the second parameter', () => {
expect(
magicPen
.clone('text')
.audio('foo/bar.aiff', 'audio/aiff')
.toString(),
magicPen.clone('text').audio('foo/bar.aiff', 'audio/aiff').toString(),
'to equal',
'foo/bar.aiff (audio/aiff)'
);
Expand All @@ -381,21 +346,15 @@ describe('magicpen-media', () => {
describe('#video', () => {
it('should render an video file given as a path and no Content-Type as-is with the looked up Content-Type in parentheses', () => {
expect(
magicPen
.clone('text')
.video('foo/bar.mkv')
.toString(),
magicPen.clone('text').video('foo/bar.mkv').toString(),
'to equal',
'foo/bar.mkv (video/x-matroska)'
);
});

it('should render an video file given as a path and no Content-Type as-is with "video" in parentheses when the video style is used and the extension is unknown', () => {
expect(
magicPen
.clone('text')
.video('foo/bar.foo')
.toString(),
magicPen.clone('text').video('foo/bar.foo').toString(),
'to equal',
'foo/bar.foo (video)'
);
Expand Down Expand Up @@ -435,7 +394,7 @@ describe('magicpen-media', () => {
const fileName = text.split(' ')[0];

expect(fs.statSync(fileName), 'to satisfy', {
isFile: expect.it('when called to be true')
isFile: expect.it('when called to be true'),
});
expect(fs.readFileSync(fileName), 'to equal', Buffer.from([1, 2, 3]));
}, 'not to error'));
Expand All @@ -450,7 +409,7 @@ describe('magicpen-media', () => {
const fileName = text.split(' ')[0];

expect(fs.statSync(fileName), 'to satisfy', {
isFile: expect.it('when called to be true')
isFile: expect.it('when called to be true'),
});
expect(fs.readFileSync(fileName), 'to equal', Buffer.from([1, 2, 3]));
}, 'not to error'));
Expand All @@ -465,7 +424,7 @@ describe('magicpen-media', () => {
const fileName = text.split(' ')[0];

expect(fs.statSync(fileName), 'to satisfy', {
isFile: expect.it('when called to be true')
isFile: expect.it('when called to be true'),
});
expect(fs.readFileSync(fileName), 'to equal', Buffer.from([1, 2, 3]));
}, 'not to error'));
Expand All @@ -477,15 +436,15 @@ describe('magicpen-media', () => {
magicPen
.clone('text')
.image('data:image/png;base64,MTIz', {
fallbackToDisc: fileName
fallbackToDisc: fileName,
})
.toString(),
'to equal',
`${fileName} (image/png)`
);

expect(fs.statSync(fileName), 'to satisfy', {
isFile: expect.it('when called to be true')
isFile: expect.it('when called to be true'),
});
expect(
fs.readFileSync(fileName),
Expand All @@ -507,7 +466,7 @@ describe('magicpen-media', () => {
);

expect(fs.statSync(fileName), 'to satisfy', {
isFile: expect.it('when called to be true')
isFile: expect.it('when called to be true'),
});
expect(fs.readFileSync(fileName), 'to equal', Buffer.from([1, 2, 3]));
}, 'not to error'));
Expand Down

0 comments on commit 857b876

Please sign in to comment.