}
*/
function getModifiers(output, page) {
- var book = output.getBook();
- var plugins = output.getPlugins();
- var glossary = book.getGlossary();
- var file = page.getFile();
+ const book = output.getBook();
+ const plugins = output.getPlugins();
+ const glossary = book.getGlossary();
+ const file = page.getFile();
// Glossary entries
- var entries = glossary.getEntries();
- var glossaryFile = glossary.getFile();
- var glossaryFilename = fileToOutput(output, glossaryFile.getPath());
+ const entries = glossary.getEntries();
+ const glossaryFile = glossary.getFile();
+ const glossaryFilename = fileToOutput(output, glossaryFile.getPath());
// Current file path
- var currentFilePath = file.getPath();
+ const currentFilePath = file.getPath();
// Get TemplateBlock for highlighting
- var blocks = Plugins.listBlocks(plugins);
- var code = blocks.get(CODEBLOCK) || defaultBlocks.get(CODEBLOCK);
+ const blocks = Plugins.listBlocks(plugins);
+ const code = blocks.get(CODEBLOCK) || defaultBlocks.get(CODEBLOCK);
// Current context
- var context = Api.encodeGlobal(output);
+ const context = Api.encodeGlobal(output);
return [
// Normalize IDs on headings
diff --git a/packages/gitbook/lib/output/helper/fileToOutput.js b/packages/gitbook/src/output/helper/fileToOutput.js
similarity index 68%
rename from packages/gitbook/lib/output/helper/fileToOutput.js
rename to packages/gitbook/src/output/helper/fileToOutput.js
index 361c6ebe2..a51485484 100644
--- a/packages/gitbook/lib/output/helper/fileToOutput.js
+++ b/packages/gitbook/src/output/helper/fileToOutput.js
@@ -1,9 +1,9 @@
-var path = require('path');
+const path = require('path');
-var PathUtils = require('../../utils/path');
-var LocationUtils = require('../../utils/location');
+const PathUtils = require('../../utils/path');
+const LocationUtils = require('../../utils/location');
-var OUTPUT_EXTENSION = '.html';
+const OUTPUT_EXTENSION = '.html';
/**
* Convert a filePath (absolute) to a filename for output
@@ -13,9 +13,9 @@ var OUTPUT_EXTENSION = '.html';
* @return {String}
*/
function fileToOutput(output, filePath) {
- var book = output.getBook();
- var readme = book.getReadme();
- var fileReadme = readme.getFile();
+ const book = output.getBook();
+ const readme = book.getReadme();
+ const fileReadme = readme.getFile();
if (
path.basename(filePath, path.extname(filePath)) == 'README' ||
diff --git a/packages/gitbook/lib/output/helper/fileToURL.js b/packages/gitbook/src/output/helper/fileToURL.js
similarity index 71%
rename from packages/gitbook/lib/output/helper/fileToURL.js
rename to packages/gitbook/src/output/helper/fileToURL.js
index 44ad2d836..a42bca6f5 100644
--- a/packages/gitbook/lib/output/helper/fileToURL.js
+++ b/packages/gitbook/src/output/helper/fileToURL.js
@@ -1,7 +1,7 @@
-var path = require('path');
-var LocationUtils = require('../../utils/location');
+const path = require('path');
+const LocationUtils = require('../../utils/location');
-var fileToOutput = require('./fileToOutput');
+const fileToOutput = require('./fileToOutput');
/**
Convert a filePath (absolute) to an url (without hostname).
@@ -16,8 +16,8 @@ var fileToOutput = require('./fileToOutput');
@return {String}
*/
function fileToURL(output, filePath) {
- var options = output.getOptions();
- var directoryIndex = options.get('directoryIndex');
+ const options = output.getOptions();
+ const directoryIndex = options.get('directoryIndex');
filePath = fileToOutput(output, filePath);
diff --git a/packages/gitbook/lib/output/helper/index.js b/packages/gitbook/src/output/helper/index.js
similarity index 100%
rename from packages/gitbook/lib/output/helper/index.js
rename to packages/gitbook/src/output/helper/index.js
diff --git a/packages/gitbook/lib/output/helper/resolveFileToURL.js b/packages/gitbook/src/output/helper/resolveFileToURL.js
similarity index 77%
rename from packages/gitbook/lib/output/helper/resolveFileToURL.js
rename to packages/gitbook/src/output/helper/resolveFileToURL.js
index 3f5271318..907cfdd71 100644
--- a/packages/gitbook/lib/output/helper/resolveFileToURL.js
+++ b/packages/gitbook/src/output/helper/resolveFileToURL.js
@@ -1,6 +1,6 @@
-var LocationUtils = require('../../utils/location');
+const LocationUtils = require('../../utils/location');
-var fileToURL = require('./fileToURL');
+const fileToURL = require('./fileToURL');
/**
* Resolve an absolute path (extracted from a link)
@@ -13,7 +13,7 @@ function resolveFileToURL(output, filePath) {
// Convert /test.png -> test.png
filePath = LocationUtils.toAbsolute(filePath, '', '');
- var page = output.getPage(filePath);
+ const page = output.getPage(filePath);
// if file is a page, return correct .html url
if (page) {
diff --git a/packages/gitbook/lib/output/helper/writeFile.js b/packages/gitbook/src/output/helper/writeFile.js
similarity index 79%
rename from packages/gitbook/lib/output/helper/writeFile.js
rename to packages/gitbook/src/output/helper/writeFile.js
index a6d4645ae..01a8e6886 100644
--- a/packages/gitbook/lib/output/helper/writeFile.js
+++ b/packages/gitbook/src/output/helper/writeFile.js
@@ -1,5 +1,5 @@
-var path = require('path');
-var fs = require('../../utils/fs');
+const path = require('path');
+const fs = require('../../utils/fs');
/**
Write a file to the output folder
@@ -10,7 +10,7 @@ var fs = require('../../utils/fs');
@return {Promise}
*/
function writeFile(output, filePath, content) {
- var rootFolder = output.getRoot();
+ const rootFolder = output.getRoot();
filePath = path.join(rootFolder, filePath);
return fs.ensureFile(filePath)
diff --git a/packages/gitbook/lib/output/index.js b/packages/gitbook/src/output/index.js
similarity index 77%
rename from packages/gitbook/lib/output/index.js
rename to packages/gitbook/src/output/index.js
index 9b8ec1756..574b3df3f 100644
--- a/packages/gitbook/lib/output/index.js
+++ b/packages/gitbook/src/output/index.js
@@ -1,6 +1,6 @@
-var Immutable = require('immutable');
+const Immutable = require('immutable');
-var generators = Immutable.List([
+const generators = Immutable.List([
require('./json'),
require('./website'),
require('./ebook')
@@ -20,5 +20,5 @@ function getGenerator(name) {
module.exports = {
generate: require('./generateBook'),
- getGenerator: getGenerator
+ getGenerator
};
diff --git a/packages/gitbook/lib/output/json/index.js b/packages/gitbook/src/output/json/index.js
similarity index 100%
rename from packages/gitbook/lib/output/json/index.js
rename to packages/gitbook/src/output/json/index.js
diff --git a/packages/gitbook/lib/output/json/onFinish.js b/packages/gitbook/src/output/json/onFinish.js
similarity index 65%
rename from packages/gitbook/lib/output/json/onFinish.js
rename to packages/gitbook/src/output/json/onFinish.js
index d41d778de..b05057a23 100644
--- a/packages/gitbook/lib/output/json/onFinish.js
+++ b/packages/gitbook/src/output/json/onFinish.js
@@ -1,8 +1,8 @@
-var path = require('path');
+const path = require('path');
-var Promise = require('../../utils/promise');
-var fs = require('../../utils/fs');
-var JSONUtils = require('../../json');
+const Promise = require('../../utils/promise');
+const fs = require('../../utils/fs');
+const JSONUtils = require('../../json');
/**
Finish the generation
@@ -11,23 +11,23 @@ var JSONUtils = require('../../json');
@return {Output}
*/
function onFinish(output) {
- var book = output.getBook();
- var outputRoot = output.getRoot();
+ const book = output.getBook();
+ const outputRoot = output.getRoot();
if (!book.isMultilingual()) {
return Promise(output);
}
// Get main language
- var languages = book.getLanguages();
- var mainLanguage = languages.getDefaultLanguage();
+ const languages = book.getLanguages();
+ const mainLanguage = languages.getDefaultLanguage();
// Read the main JSON
return fs.readFile(path.resolve(outputRoot, mainLanguage.getID(), 'README.json'), 'utf8')
// Extend the JSON
.then(function(content) {
- var json = JSON.parse(content);
+ const json = JSON.parse(content);
json.languages = JSONUtils.encodeLanguages(languages);
diff --git a/packages/gitbook/lib/output/json/onPage.js b/packages/gitbook/src/output/json/onPage.js
similarity index 57%
rename from packages/gitbook/lib/output/json/onPage.js
rename to packages/gitbook/src/output/json/onPage.js
index 2315ba08f..8bee71195 100644
--- a/packages/gitbook/lib/output/json/onPage.js
+++ b/packages/gitbook/src/output/json/onPage.js
@@ -1,10 +1,10 @@
-var JSONUtils = require('../../json');
-var PathUtils = require('../../utils/path');
-var Modifiers = require('../modifiers');
-var writeFile = require('../helper/writeFile');
-var getModifiers = require('../getModifiers');
+const JSONUtils = require('../../json');
+const PathUtils = require('../../utils/path');
+const Modifiers = require('../modifiers');
+const writeFile = require('../helper/writeFile');
+const getModifiers = require('../getModifiers');
-var JSON_VERSION = '3';
+const JSON_VERSION = '3';
/**
* Write a page as a json file
@@ -13,13 +13,13 @@ var JSON_VERSION = '3';
* @param {Page} page
*/
function onPage(output, page) {
- var file = page.getFile();
- var readme = output.getBook().getReadme().getFile();
+ const file = page.getFile();
+ const readme = output.getBook().getReadme().getFile();
return Modifiers.modifyHTML(page, getModifiers(output, page))
.then(function(resultPage) {
// Generate the JSON
- var json = JSONUtils.encodeBookWithPage(output.getBook(), resultPage);
+ const json = JSONUtils.encodeBookWithPage(output.getBook(), resultPage);
// Delete some private properties
delete json.config;
@@ -28,7 +28,7 @@ function onPage(output, page) {
json.version = JSON_VERSION;
// File path in the output folder
- var filePath = file.getPath() == readme.getPath()? 'README.json' : file.getPath();
+ let filePath = file.getPath() == readme.getPath() ? 'README.json' : file.getPath();
filePath = PathUtils.setExtension(filePath, '.json');
// Write it to the disk
diff --git a/packages/gitbook/lib/output/json/options.js b/packages/gitbook/src/output/json/options.js
similarity index 54%
rename from packages/gitbook/lib/output/json/options.js
rename to packages/gitbook/src/output/json/options.js
index 79167b180..2a9de0ed7 100644
--- a/packages/gitbook/lib/output/json/options.js
+++ b/packages/gitbook/src/output/json/options.js
@@ -1,6 +1,6 @@
-var Immutable = require('immutable');
+const Immutable = require('immutable');
-var Options = Immutable.Record({
+const Options = Immutable.Record({
// Root folder for the output
root: String()
});
diff --git a/packages/gitbook/lib/output/modifiers/__tests__/addHeadingId.js b/packages/gitbook/src/output/modifiers/__tests__/addHeadingId.js
similarity index 61%
rename from packages/gitbook/lib/output/modifiers/__tests__/addHeadingId.js
rename to packages/gitbook/src/output/modifiers/__tests__/addHeadingId.js
index a3b1d81f7..4d77e757d 100644
--- a/packages/gitbook/lib/output/modifiers/__tests__/addHeadingId.js
+++ b/packages/gitbook/src/output/modifiers/__tests__/addHeadingId.js
@@ -1,26 +1,25 @@
-var cheerio = require('cheerio');
-var addHeadingId = require('../addHeadingId');
+const cheerio = require('cheerio');
+const addHeadingId = require('../addHeadingId');
describe('addHeadingId', function() {
it('should add an ID if none', function() {
- var $ = cheerio.load('Hello World
Cool !!
');
+ const $ = cheerio.load('Hello World
Cool !!
');
return addHeadingId($)
.then(function() {
- var html = $.html();
+ const html = $.html();
expect(html).toBe('Hello World
Cool !!
');
});
});
it('should not change existing IDs', function() {
- var $ = cheerio.load('Hello World
');
+ const $ = cheerio.load('Hello World
');
return addHeadingId($)
.then(function() {
- var html = $.html();
+ const html = $.html();
expect(html).toBe('Hello World
');
});
});
});
-
diff --git a/packages/gitbook/lib/output/modifiers/__tests__/annotateText.js b/packages/gitbook/src/output/modifiers/__tests__/annotateText.js
similarity index 61%
rename from packages/gitbook/lib/output/modifiers/__tests__/annotateText.js
rename to packages/gitbook/src/output/modifiers/__tests__/annotateText.js
index 67e7a1067..28a5cc538 100644
--- a/packages/gitbook/lib/output/modifiers/__tests__/annotateText.js
+++ b/packages/gitbook/src/output/modifiers/__tests__/annotateText.js
@@ -1,46 +1,45 @@
-var Immutable = require('immutable');
-var cheerio = require('cheerio');
-var GlossaryEntry = require('../../../models/glossaryEntry');
-var annotateText = require('../annotateText');
+const Immutable = require('immutable');
+const cheerio = require('cheerio');
+const GlossaryEntry = require('../../../models/glossaryEntry');
+const annotateText = require('../annotateText');
describe('annotateText', function() {
- var entries = Immutable.List([
+ const entries = Immutable.List([
GlossaryEntry({ name: 'Word' }),
GlossaryEntry({ name: 'Multiple Words' })
]);
it('should annotate text', function() {
- var $ = cheerio.load('This is a word, and multiple words
');
+ const $ = cheerio.load('This is a word, and multiple words
');
annotateText(entries, 'GLOSSARY.md', $);
- var links = $('a');
+ const links = $('a');
expect(links.length).toBe(2);
- var word = $(links.get(0));
+ const word = $(links.get(0));
expect(word.attr('href')).toBe('/GLOSSARY.md#word');
expect(word.text()).toBe('word');
expect(word.hasClass('glossary-term')).toBeTruthy();
- var words = $(links.get(1));
+ const words = $(links.get(1));
expect(words.attr('href')).toBe('/GLOSSARY.md#multiple-words');
expect(words.text()).toBe('multiple words');
expect(words.hasClass('glossary-term')).toBeTruthy();
});
it('should not annotate scripts', function() {
- var $ = cheerio.load('');
+ const $ = cheerio.load('');
annotateText(entries, 'GLOSSARY.md', $);
expect($('a').length).toBe(0);
});
it('should not annotate when has class "no-glossary"', function() {
- var $ = cheerio.load('This is a word, and multiple words
');
+ const $ = cheerio.load('This is a word, and multiple words
');
annotateText(entries, 'GLOSSARY.md', $);
expect($('a').length).toBe(0);
});
});
-
diff --git a/packages/gitbook/src/output/modifiers/__tests__/fetchRemoteImages.js b/packages/gitbook/src/output/modifiers/__tests__/fetchRemoteImages.js
new file mode 100644
index 000000000..9145caece
--- /dev/null
+++ b/packages/gitbook/src/output/modifiers/__tests__/fetchRemoteImages.js
@@ -0,0 +1,39 @@
+const cheerio = require('cheerio');
+const tmp = require('tmp');
+const path = require('path');
+
+const URL = 'https://upload.wikimedia.org/wikipedia/commons/thumb/4/47/PNG_transparency_demonstration_1.png/280px-PNG_transparency_demonstration_1.png';
+
+describe('fetchRemoteImages', function() {
+ let dir;
+ const fetchRemoteImages = require('../fetchRemoteImages');
+
+ beforeEach(function() {
+ dir = tmp.dirSync();
+ });
+
+ it('should download image file', function() {
+ const $ = cheerio.load('
');
+
+ return fetchRemoteImages(dir.name, 'index.html', $)
+ .then(function() {
+ const $img = $('img');
+ const src = $img.attr('src');
+
+ expect(dir.name).toHaveFile(src);
+ });
+ });
+
+ it('should download image file and replace with relative path', function() {
+ const $ = cheerio.load('
');
+
+ return fetchRemoteImages(dir.name, 'test/index.html', $)
+ .then(function() {
+ const $img = $('img');
+ const src = $img.attr('src');
+
+ expect(dir.name).toHaveFile(path.join('test', src));
+ });
+ });
+});
+
diff --git a/packages/gitbook/lib/output/modifiers/__tests__/highlightCode.js b/packages/gitbook/src/output/modifiers/__tests__/highlightCode.js
similarity index 65%
rename from packages/gitbook/lib/output/modifiers/__tests__/highlightCode.js
rename to packages/gitbook/src/output/modifiers/__tests__/highlightCode.js
index 75d99022e..d93417b7b 100644
--- a/packages/gitbook/lib/output/modifiers/__tests__/highlightCode.js
+++ b/packages/gitbook/src/output/modifiers/__tests__/highlightCode.js
@@ -1,6 +1,6 @@
-var cheerio = require('cheerio');
-var Promise = require('../../../utils/promise');
-var highlightCode = require('../highlightCode');
+const cheerio = require('cheerio');
+const Promise = require('../../../utils/promise');
+const highlightCode = require('../highlightCode');
describe('highlightCode', function() {
function doHighlight(lang, code) {
@@ -17,44 +17,43 @@ describe('highlightCode', function() {
}
it('should call it for normal code element', function() {
- var $ = cheerio.load('This is a test
');
+ const $ = cheerio.load('This is a test
');
return highlightCode(doHighlight, $)
.then(function() {
- var $code = $('code');
+ const $code = $('code');
expect($code.text()).toBe('$test');
});
});
it('should call it for markdown code block', function() {
- var $ = cheerio.load('test
');
+ const $ = cheerio.load('test
');
return highlightCode(doHighlight, $)
.then(function() {
- var $code = $('code');
+ const $code = $('code');
expect($code.text()).toBe('js$test');
});
});
it('should call it for asciidoc code block', function() {
- var $ = cheerio.load('test
');
+ const $ = cheerio.load('test
');
return highlightCode(doHighlight, $)
.then(function() {
- var $code = $('code');
+ const $code = $('code');
expect($code.text()).toBe('python$test');
});
});
it('should accept async highlighter', function() {
- var $ = cheerio.load('test
');
+ const $ = cheerio.load('test
');
return highlightCode(doHighlightAsync, $)
.then(function() {
- var $code = $('code');
+ const $code = $('code');
expect($code.text()).toBe('python$test');
});
});
});
-
diff --git a/packages/gitbook/src/output/modifiers/__tests__/inlinePng.js b/packages/gitbook/src/output/modifiers/__tests__/inlinePng.js
new file mode 100644
index 000000000..fd031b05e
--- /dev/null
+++ b/packages/gitbook/src/output/modifiers/__tests__/inlinePng.js
@@ -0,0 +1,24 @@
+const cheerio = require('cheerio');
+const tmp = require('tmp');
+const inlinePng = require('../inlinePng');
+
+describe('inlinePng', function() {
+ let dir;
+
+ beforeEach(function() {
+ dir = tmp.dirSync();
+ });
+
+ it('should write an inline PNG using data URI as a file', function() {
+ const $ = cheerio.load('
');
+
+ return inlinePng(dir.name, 'index.html', $)
+ .then(function() {
+ const $img = $('img');
+ const src = $img.attr('src');
+
+ expect(dir.name).toHaveFile(src);
+ });
+ });
+});
+
diff --git a/packages/gitbook/lib/output/modifiers/__tests__/resolveLinks.js b/packages/gitbook/src/output/modifiers/__tests__/resolveLinks.js
similarity index 68%
rename from packages/gitbook/lib/output/modifiers/__tests__/resolveLinks.js
rename to packages/gitbook/src/output/modifiers/__tests__/resolveLinks.js
index 8904c118d..167af5de6 100644
--- a/packages/gitbook/lib/output/modifiers/__tests__/resolveLinks.js
+++ b/packages/gitbook/src/output/modifiers/__tests__/resolveLinks.js
@@ -1,6 +1,6 @@
-var path = require('path');
-var cheerio = require('cheerio');
-var resolveLinks = require('../resolveLinks');
+const path = require('path');
+const cheerio = require('cheerio');
+const resolveLinks = require('../resolveLinks');
describe('resolveLinks', function() {
function resolveFileBasic(href) {
@@ -16,24 +16,24 @@ describe('resolveLinks', function() {
}
describe('Absolute path', function() {
- var TEST = 'This is a
';
+ const TEST = 'This is a
';
it('should resolve path starting by "/" in root directory', function() {
- var $ = cheerio.load(TEST);
+ const $ = cheerio.load(TEST);
return resolveLinks('hello.md', resolveFileBasic, $)
.then(function() {
- var link = $('a');
+ const link = $('a');
expect(link.attr('href')).toBe('fakeDir/test/cool.md');
});
});
it('should resolve path starting by "/" in child directory', function() {
- var $ = cheerio.load(TEST);
+ const $ = cheerio.load(TEST);
return resolveLinks('afolder/hello.md', resolveFileBasic, $)
.then(function() {
- var link = $('a');
+ const link = $('a');
expect(link.attr('href')).toBe('../fakeDir/test/cool.md');
});
});
@@ -41,61 +41,61 @@ describe('resolveLinks', function() {
describe('Anchor', function() {
it('should prevent anchors in resolution', function() {
- var TEST = 'This is a
';
- var $ = cheerio.load(TEST);
+ const TEST = 'This is a
';
+ const $ = cheerio.load(TEST);
return resolveLinks('hello.md', resolveFileCustom, $)
.then(function() {
- var link = $('a');
+ const link = $('a');
expect(link.attr('href')).toBe('test/cool.html#an-anchor');
});
});
it('should ignore pure anchor links', function() {
- var TEST = 'This is a
';
- var $ = cheerio.load(TEST);
+ const TEST = 'This is a
';
+ const $ = cheerio.load(TEST);
return resolveLinks('hello.md', resolveFileCustom, $)
.then(function() {
- var link = $('a');
+ const link = $('a');
expect(link.attr('href')).toBe('#an-anchor');
});
});
});
describe('Custom Resolver', function() {
- var TEST = 'This is a
';
+ const TEST = 'This is a
';
it('should resolve path correctly for absolute path', function() {
- var $ = cheerio.load(TEST);
+ const $ = cheerio.load(TEST);
return resolveLinks('hello.md', resolveFileCustom, $)
.then(function() {
- var link = $('a').first();
+ const link = $('a').first();
expect(link.attr('href')).toBe('test/cool.html');
});
});
it('should resolve path correctly for absolute path (2)', function() {
- var $ = cheerio.load(TEST);
+ const $ = cheerio.load(TEST);
return resolveLinks('afodler/hello.md', resolveFileCustom, $)
.then(function() {
- var link = $('a').first();
+ const link = $('a').first();
expect(link.attr('href')).toBe('../test/cool.html');
});
});
});
describe('External link', function() {
- var TEST = 'This is a external link
';
+ const TEST = 'This is a external link
';
it('should have target="_blank" attribute', function() {
- var $ = cheerio.load(TEST);
+ const $ = cheerio.load(TEST);
return resolveLinks('hello.md', resolveFileBasic, $)
.then(function() {
- var link = $('a');
+ const link = $('a');
expect(link.attr('target')).toBe('_blank');
});
});
diff --git a/packages/gitbook/src/output/modifiers/__tests__/svgToImg.js b/packages/gitbook/src/output/modifiers/__tests__/svgToImg.js
new file mode 100644
index 000000000..4bdab592e
--- /dev/null
+++ b/packages/gitbook/src/output/modifiers/__tests__/svgToImg.js
@@ -0,0 +1,24 @@
+const cheerio = require('cheerio');
+const tmp = require('tmp');
+
+describe('svgToImg', function() {
+ let dir;
+ const svgToImg = require('../svgToImg');
+
+ beforeEach(function() {
+ dir = tmp.dirSync();
+ });
+
+ it('should write svg as a file', function() {
+ const $ = cheerio.load('');
+
+ return svgToImg(dir.name, 'index.html', $)
+ .then(function() {
+ const $img = $('img');
+ const src = $img.attr('src');
+
+ expect(dir.name).toHaveFile(src);
+ });
+ });
+});
+
diff --git a/packages/gitbook/src/output/modifiers/__tests__/svgToPng.js b/packages/gitbook/src/output/modifiers/__tests__/svgToPng.js
new file mode 100644
index 000000000..0a1293850
--- /dev/null
+++ b/packages/gitbook/src/output/modifiers/__tests__/svgToPng.js
@@ -0,0 +1,32 @@
+const cheerio = require('cheerio');
+const tmp = require('tmp');
+const path = require('path');
+
+const svgToImg = require('../svgToImg');
+const svgToPng = require('../svgToPng');
+
+describe('svgToPng', function() {
+ let dir;
+
+ beforeEach(function() {
+ dir = tmp.dirSync();
+ });
+
+ it('should write svg as png file', function() {
+ const $ = cheerio.load('');
+ const fileName = 'index.html';
+
+ return svgToImg(dir.name, fileName, $)
+ .then(function() {
+ return svgToPng(dir.name, fileName, $);
+ })
+ .then(function() {
+ const $img = $('img');
+ const src = $img.attr('src');
+
+ expect(dir.name).toHaveFile(src);
+ expect(path.extname(src)).toBe('.png');
+ });
+ });
+});
+
diff --git a/packages/gitbook/lib/output/modifiers/addHeadingId.js b/packages/gitbook/src/output/modifiers/addHeadingId.js
similarity index 79%
rename from packages/gitbook/lib/output/modifiers/addHeadingId.js
rename to packages/gitbook/src/output/modifiers/addHeadingId.js
index e2e27200d..e5bab3e2e 100644
--- a/packages/gitbook/lib/output/modifiers/addHeadingId.js
+++ b/packages/gitbook/src/output/modifiers/addHeadingId.js
@@ -1,5 +1,5 @@
-var slug = require('github-slugid');
-var editHTMLElement = require('./editHTMLElement');
+const slug = require('github-slugid');
+const editHTMLElement = require('./editHTMLElement');
/**
Add ID to an heading
diff --git a/packages/gitbook/lib/output/modifiers/annotateText.js b/packages/gitbook/src/output/modifiers/annotateText.js
similarity index 63%
rename from packages/gitbook/lib/output/modifiers/annotateText.js
rename to packages/gitbook/src/output/modifiers/annotateText.js
index 490c228fb..36ee4e9fc 100644
--- a/packages/gitbook/lib/output/modifiers/annotateText.js
+++ b/packages/gitbook/src/output/modifiers/annotateText.js
@@ -1,46 +1,43 @@
-var escape = require('escape-html');
+const escape = require('escape-html');
// Selector to ignore
-var ANNOTATION_IGNORE = '.no-glossary,code,pre,a,script,h1,h2,h3,h4,h5,h6';
+const ANNOTATION_IGNORE = '.no-glossary,code,pre,a,script,h1,h2,h3,h4,h5,h6';
-function pregQuote( str ) {
- return (str+'').replace(/([\\\.\+\*\?\[\^\]\$\(\)\{\}\=\!\<\>\|\:])/g, '\\$1');
+function pregQuote(str) {
+ return (str + '').replace(/([\\\.\+\*\?\[\^\]\$\(\)\{\}\=\!\<\>\|\:])/g, '\\$1');
}
-function replaceText($, el, search, replace, text_only ) {
- return $(el).each(function(){
- var node = this.firstChild,
- val,
- new_val,
-
- // Elements to be removed at the end.
- remove = [];
+function replaceText($, el, search, replace, text_only) {
+ return $(el).each(function() {
+ let node = this.firstChild, val, new_val;
+ // Elements to be removed at the end.
+ const remove = [];
// Only continue if firstChild exists.
- if ( node ) {
+ if (node) {
// Loop over all childNodes.
while (node) {
// Only process text nodes.
- if ( node.nodeType === 3 ) {
+ if (node.nodeType === 3) {
// The original node value.
val = node.nodeValue;
// The new value.
- new_val = val.replace( search, replace );
+ new_val = val.replace(search, replace);
// Only replace text if the new value is actually different!
- if ( new_val !== val ) {
+ if (new_val !== val) {
- if ( !text_only && /}
*/
function modifyHTML(page, operations) {
- var html = page.getContent();
- var $ = cheerio.load(html);
+ const html = page.getContent();
+ const $ = cheerio.load(html);
return Promise.forEach(operations, function(op) {
return op($);
})
.then(function() {
- var resultHTML = $.html();
+ const resultHTML = $.html();
return page.set('content', resultHTML);
});
}
diff --git a/packages/gitbook/lib/output/modifiers/resolveImages.js b/packages/gitbook/src/output/modifiers/resolveImages.js
similarity index 73%
rename from packages/gitbook/lib/output/modifiers/resolveImages.js
rename to packages/gitbook/src/output/modifiers/resolveImages.js
index cc25cfa32..339ddeb5a 100644
--- a/packages/gitbook/lib/output/modifiers/resolveImages.js
+++ b/packages/gitbook/src/output/modifiers/resolveImages.js
@@ -1,7 +1,7 @@
-var path = require('path');
+const path = require('path');
-var LocationUtils = require('../../utils/location');
-var editHTMLElement = require('./editHTMLElement');
+const LocationUtils = require('../../utils/location');
+const editHTMLElement = require('./editHTMLElement');
/**
Resolve all HTML images:
@@ -11,10 +11,10 @@ var editHTMLElement = require('./editHTMLElement');
@param {HTMLDom} $
*/
function resolveImages(currentFile, $) {
- var currentDirectory = path.dirname(currentFile);
+ const currentDirectory = path.dirname(currentFile);
return editHTMLElement($, 'img', function($img) {
- var src = $img.attr('src');
+ let src = $img.attr('src');
if (LocationUtils.isExternal(src) || LocationUtils.isDataURI(src)) {
return;
diff --git a/packages/gitbook/lib/output/modifiers/resolveLinks.js b/packages/gitbook/src/output/modifiers/resolveLinks.js
similarity index 77%
rename from packages/gitbook/lib/output/modifiers/resolveLinks.js
rename to packages/gitbook/src/output/modifiers/resolveLinks.js
index 9d15e5ed4..8b15315e1 100644
--- a/packages/gitbook/lib/output/modifiers/resolveLinks.js
+++ b/packages/gitbook/src/output/modifiers/resolveLinks.js
@@ -1,8 +1,8 @@
-var path = require('path');
-var url = require('url');
+const path = require('path');
+const url = require('url');
-var LocationUtils = require('../../utils/location');
-var editHTMLElement = require('./editHTMLElement');
+const LocationUtils = require('../../utils/location');
+const editHTMLElement = require('./editHTMLElement');
/**
Resolve all HTML links:
@@ -13,10 +13,10 @@ var editHTMLElement = require('./editHTMLElement');
@param {HTMLDom} $
*/
function resolveLinks(currentFile, resolveFile, $) {
- var currentDirectory = path.dirname(currentFile);
+ const currentDirectory = path.dirname(currentFile);
return editHTMLElement($, 'a', function($a) {
- var href = $a.attr('href');
+ let href = $a.attr('href');
// Don't change a tag without href
if (!href) {
@@ -29,7 +29,7 @@ function resolveLinks(currentFile, resolveFile, $) {
}
// Split anchor
- var parsed = url.parse(href);
+ const parsed = url.parse(href);
href = parsed.pathname || '';
if (href) {
diff --git a/packages/gitbook/lib/output/modifiers/svgToImg.js b/packages/gitbook/src/output/modifiers/svgToImg.js
similarity index 57%
rename from packages/gitbook/lib/output/modifiers/svgToImg.js
rename to packages/gitbook/src/output/modifiers/svgToImg.js
index f31b06d1a..ac37d0790 100644
--- a/packages/gitbook/lib/output/modifiers/svgToImg.js
+++ b/packages/gitbook/src/output/modifiers/svgToImg.js
@@ -1,10 +1,10 @@
-var path = require('path');
-var crc = require('crc');
-var domSerializer = require('dom-serializer');
+const path = require('path');
+const crc = require('crc');
+const domSerializer = require('dom-serializer');
-var editHTMLElement = require('./editHTMLElement');
-var fs = require('../../utils/fs');
-var LocationUtils = require('../../utils/location');
+const editHTMLElement = require('./editHTMLElement');
+const fs = require('../../utils/fs');
+const LocationUtils = require('../../utils/location');
/**
Render a cheerio DOM as html
@@ -18,7 +18,7 @@ function renderDOM($, dom, options) {
if (!dom && $._root && $._root.children) {
dom = $._root.children;
}
- options = options|| dom.options || $._options;
+ options = options || dom.options || $._options;
return domSerializer(dom, options);
}
@@ -29,16 +29,16 @@ function renderDOM($, dom, options) {
@param {HTMLDom} $
*/
function svgToImg(baseFolder, currentFile, $) {
- var currentDirectory = path.dirname(currentFile);
+ const currentDirectory = path.dirname(currentFile);
return editHTMLElement($, 'svg', function($svg) {
- var content = '' +
+ const content = '' +
renderDOM($, $svg);
// We avoid generating twice the same PNG
- var hash = crc.crc32(content).toString(16);
- var fileName = hash + '.svg';
- var filePath = path.join(baseFolder, fileName);
+ const hash = crc.crc32(content).toString(16);
+ const fileName = hash + '.svg';
+ const filePath = path.join(baseFolder, fileName);
// Write the svg to the file
return fs.assertFile(filePath, function() {
@@ -47,7 +47,7 @@ function svgToImg(baseFolder, currentFile, $) {
// Return as image
.then(function() {
- var src = LocationUtils.relative(currentDirectory, fileName);
+ const src = LocationUtils.relative(currentDirectory, fileName);
$svg.replaceWith('
');
});
});
diff --git a/packages/gitbook/lib/output/modifiers/svgToPng.js b/packages/gitbook/src/output/modifiers/svgToPng.js
similarity index 62%
rename from packages/gitbook/lib/output/modifiers/svgToPng.js
rename to packages/gitbook/src/output/modifiers/svgToPng.js
index 109310639..ad3f31fba 100644
--- a/packages/gitbook/lib/output/modifiers/svgToPng.js
+++ b/packages/gitbook/src/output/modifiers/svgToPng.js
@@ -1,11 +1,11 @@
-var crc = require('crc');
-var path = require('path');
+const crc = require('crc');
+const path = require('path');
-var imagesUtil = require('../../utils/images');
-var fs = require('../../utils/fs');
-var LocationUtils = require('../../utils/location');
+const imagesUtil = require('../../utils/images');
+const fs = require('../../utils/fs');
+const LocationUtils = require('../../utils/location');
-var editHTMLElement = require('./editHTMLElement');
+const editHTMLElement = require('./editHTMLElement');
/**
Convert all SVG images to PNG
@@ -15,10 +15,10 @@ var editHTMLElement = require('./editHTMLElement');
@return {Promise}
*/
function svgToPng(rootFolder, currentFile, $) {
- var currentDirectory = path.dirname(currentFile);
+ const currentDirectory = path.dirname(currentFile);
return editHTMLElement($, 'img', function($img) {
- var src = $img.attr('src');
+ let src = $img.attr('src');
if (path.extname(src) !== '.svg') {
return;
}
@@ -27,14 +27,14 @@ function svgToPng(rootFolder, currentFile, $) {
src = LocationUtils.toAbsolute(src, currentDirectory, '.');
// We avoid generating twice the same PNG
- var hash = crc.crc32(src).toString(16);
- var fileName = hash + '.png';
+ const hash = crc.crc32(src).toString(16);
+ let fileName = hash + '.png';
// Input file path
- var inputPath = path.join(rootFolder, src);
+ const inputPath = path.join(rootFolder, src);
// Result file path
- var filePath = path.join(rootFolder, fileName);
+ const filePath = path.join(rootFolder, fileName);
return fs.assertFile(filePath, function() {
return imagesUtil.convertSVGToPNG(inputPath, filePath);
diff --git a/packages/gitbook/lib/output/prepareAssets.js b/packages/gitbook/src/output/prepareAssets.js
similarity index 70%
rename from packages/gitbook/lib/output/prepareAssets.js
rename to packages/gitbook/src/output/prepareAssets.js
index ae9b55acb..6694a7427 100644
--- a/packages/gitbook/lib/output/prepareAssets.js
+++ b/packages/gitbook/src/output/prepareAssets.js
@@ -1,4 +1,4 @@
-var Parse = require('../parse');
+const Parse = require('../parse');
/**
List all assets in the book
@@ -7,9 +7,9 @@ var Parse = require('../parse');
@return {Promise