Skip to content

Commit

Permalink
fix: better meta handling
Browse files Browse the repository at this point in the history
  • Loading branch information
remy committed Dec 12, 2018
1 parent 19976ee commit 5a4726c
Show file tree
Hide file tree
Showing 5 changed files with 1,236 additions and 1,190 deletions.
2 changes: 2 additions & 0 deletions __tests__/fixtures/stripped.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<!doctype html>
<p>Hello world</p>
34 changes: 34 additions & 0 deletions __tests__/multi-run.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/* eslint-env jest */
const toFile = require('../lib');
const fileToBin = require('file-to-bin');
const fs = require('fs');
const path = require('path');

describe('stripped and repeatedly converted', function() {
var html = '';

beforeEach(function() {
html = fs.readFileSync(
path.join(__dirname, 'fixtures', 'stripped.html'),
'utf8'
);
});

it('should have fixtures', function() {
expect(html.length).toBeGreaterThan(0);
});

it('does nothing to plain html', function() {
const bin = fileToBin(html);
bin.url = 'abc-def-ghi';
// bin.meta = `<!-- source: https://jsbin.com/${bin.url}/edit -->
// <meta http-equiv="CACHE-CONTROL" content="NO-CACHE">`;
const file = toFile(bin);
const parsed = fileToBin(file);

expect(parsed.html).toEqual(bin.html);
expect(parsed.url).toEqual(bin.url);

expect(toFile(parsed)).toEqual(toFile(bin));
});
});
9 changes: 6 additions & 3 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,9 @@ function binToFile(bin, options = {}) {
const binId = [bin.url, bin.revision].filter(Boolean).join('/');
let meta =
bin.meta ||
(bin.url ? `<!-- source: https://jsbin.com/${binId}/edit -->\n` : '');
(bin.url
? `<meta name="source" content="https://jsbin.com/${binId}/edit">`
: '');

// insert protocol if missing
html = html.replace(/(src|href)=('|")\/\//g, '$1=$2' + proto + '//');
Expand Down Expand Up @@ -133,6 +135,7 @@ function binToFile(bin, options = {}) {

// try to insert above the head
if (meta) {
meta = `<!-- jsbin -->\n${meta.trim()}\n<!-- /jsbin -->\n`;
const afterHTML = afterLine(file, '<html', meta);
if (afterHTML) {
file = afterHTML;
Expand Down Expand Up @@ -161,7 +164,7 @@ function binToFile(bin, options = {}) {
} else {
// is there head tag?
const hasModule = javascript.includes('import ');
javascript = `<!--boot js-->\n<script id="jsbin-javascript" ${
javascript = `<script id="jsbin-javascript" ${
hasModule ? 'type="module" ' : ''
}defer>\n${javascript}\n</script>`;

Expand Down Expand Up @@ -202,7 +205,7 @@ function binToFile(bin, options = {}) {

return '';
})
.join('\n');
.join('');

const bodyTag = insert(file, '</body>', sourceScripts);
if (bodyTag) {
Expand Down
Loading

0 comments on commit 5a4726c

Please sign in to comment.