diff options
-rw-r--r-- | lib/utils/page.js | 18 | ||||
-rw-r--r-- | package.json | 3 | ||||
-rw-r--r-- | test/books/headings/README.md | 3 | ||||
-rw-r--r-- | test/books/headings/SUMMARY.md | 0 | ||||
-rw-r--r-- | test/heading.js | 37 |
5 files changed, 55 insertions, 6 deletions
diff --git a/lib/utils/page.js b/lib/utils/page.js index c79f237..739b59c 100644 --- a/lib/utils/page.js +++ b/lib/utils/page.js @@ -6,6 +6,7 @@ var cheerio = require('cheerio'); var domSerializer = require('dom-serializer'); var request = require('request'); var crc = require('crc'); +var slug = require('github-slugid'); var links = require('./links'); var imgUtils = require('./images'); @@ -14,18 +15,18 @@ var batch = require('./batch'); var parsableExtensions = require('gitbook-parsers').extensions; +// Map of images that have been converted +var imgConversionCache = {}; + // Render a cheerio dom as html -var renderDom = function($, dom, options) { +function renderDom($, dom, options) { if (!dom && $._root && $._root.children) { dom = $._root.children; } options = options|| dom.options || $._options; return domSerializer(dom, options); -}; - -// Map of images that have been converted -var imgConversionCache = {}; +} function replaceText($, el, search, replace, text_only ) { return $(el).each(function(){ @@ -114,6 +115,13 @@ function normalizeHtml(src, options) { }); } + // Generate ID for headings + $('h1,h2,h3,h4,h5,h6').each(function() { + if ($(this).attr('id')) return; + + $(this).attr('id', slug($(this).text())); + }); + // Find images to normalize $('img').each(function() { var origin; diff --git a/package.json b/package.json index 5797c65..5eff3b0 100644 --- a/package.json +++ b/package.json @@ -39,7 +39,8 @@ "juice": "1.5.0", "jsonschema": "1.0.2", "json-schema-defaults": "0.1.1", - "merge-defaults": "0.2.1" + "merge-defaults": "0.2.1", + "github-slugid": "1.0.0" }, "devDependencies": { "eslint": "1.5.0", diff --git a/test/books/headings/README.md b/test/books/headings/README.md new file mode 100644 index 0000000..b08c485 --- /dev/null +++ b/test/books/headings/README.md @@ -0,0 +1,3 @@ +# Hello World + +## Hello {#hello-custom} diff --git a/test/books/headings/SUMMARY.md b/test/books/headings/SUMMARY.md new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/test/books/headings/SUMMARY.md diff --git a/test/heading.js b/test/heading.js new file mode 100644 index 0000000..f6d65c3 --- /dev/null +++ b/test/heading.js @@ -0,0 +1,37 @@ +var path = require('path'); +var fs = require('fs'); + +describe('Headings', function () { + var book, PAGE; + + before(function() { + return books.generate('headings', 'website') + .then(function(_book) { + book = _book; + + PAGE = fs.readFileSync( + path.join(book.options.output, 'index.html'), + { encoding: 'utf-8' } + ); + }); + }); + + describe('IDs', function() { + it('should correctly generate an ID', function() { + PAGE.should.be.html({ + 'h1#hello-world': { + count: 1 + } + }); + }); + + it('should correctly accept custom ID', function() { + PAGE.should.be.html({ + 'h2#hello-custom': { + count: 1 + } + }); + }); + }); +}); + |