Skip to content

Commit 5a4726c

Browse files
committed
fix: better meta handling
1 parent 19976ee commit 5a4726c

File tree

5 files changed

+1236
-1190
lines changed

5 files changed

+1236
-1190
lines changed

__tests__/fixtures/stripped.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<!doctype html>
2+
<p>Hello world</p>

__tests__/multi-run.test.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/* eslint-env jest */
2+
const toFile = require('../lib');
3+
const fileToBin = require('file-to-bin');
4+
const fs = require('fs');
5+
const path = require('path');
6+
7+
describe('stripped and repeatedly converted', function() {
8+
var html = '';
9+
10+
beforeEach(function() {
11+
html = fs.readFileSync(
12+
path.join(__dirname, 'fixtures', 'stripped.html'),
13+
'utf8'
14+
);
15+
});
16+
17+
it('should have fixtures', function() {
18+
expect(html.length).toBeGreaterThan(0);
19+
});
20+
21+
it('does nothing to plain html', function() {
22+
const bin = fileToBin(html);
23+
bin.url = 'abc-def-ghi';
24+
// bin.meta = `<!-- source: https://jsbin.com/${bin.url}/edit -->
25+
// <meta http-equiv="CACHE-CONTROL" content="NO-CACHE">`;
26+
const file = toFile(bin);
27+
const parsed = fileToBin(file);
28+
29+
expect(parsed.html).toEqual(bin.html);
30+
expect(parsed.url).toEqual(bin.url);
31+
32+
expect(toFile(parsed)).toEqual(toFile(bin));
33+
});
34+
});

lib/index.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,9 @@ function binToFile(bin, options = {}) {
8080
const binId = [bin.url, bin.revision].filter(Boolean).join('/');
8181
let meta =
8282
bin.meta ||
83-
(bin.url ? `<!-- source: https://jsbin.com/${binId}/edit -->\n` : '');
83+
(bin.url
84+
? `<meta name="source" content="https://jsbin.com/${binId}/edit">`
85+
: '');
8486

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

134136
// try to insert above the head
135137
if (meta) {
138+
meta = `<!-- jsbin -->\n${meta.trim()}\n<!-- /jsbin -->\n`;
136139
const afterHTML = afterLine(file, '<html', meta);
137140
if (afterHTML) {
138141
file = afterHTML;
@@ -161,7 +164,7 @@ function binToFile(bin, options = {}) {
161164
} else {
162165
// is there head tag?
163166
const hasModule = javascript.includes('import ');
164-
javascript = `<!--boot js-->\n<script id="jsbin-javascript" ${
167+
javascript = `<script id="jsbin-javascript" ${
165168
hasModule ? 'type="module" ' : ''
166169
}defer>\n${javascript}\n</script>`;
167170

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

203206
return '';
204207
})
205-
.join('\n');
208+
.join('');
206209

207210
const bodyTag = insert(file, '</body>', sourceScripts);
208211
if (bodyTag) {

0 commit comments

Comments
 (0)