diff options
author | Samy Pesse <samypesse@gmail.com> | 2016-10-09 18:25:54 +0200 |
---|---|---|
committer | Samy Pesse <samypesse@gmail.com> | 2016-10-09 18:25:54 +0200 |
commit | 7037ac55bd444441fcacefa1224a6c36e3ecd7a6 (patch) | |
tree | 9ac36462ab0f359c52af06e93a8981b53def3ebb | |
parent | cf35fef3b380bf59edae0bd05ffb8366fd3bf4dd (diff) | |
download | gitbook-7037ac55bd444441fcacefa1224a6c36e3ecd7a6.zip gitbook-7037ac55bd444441fcacefa1224a6c36e3ecd7a6.tar.gz gitbook-7037ac55bd444441fcacefa1224a6c36e3ecd7a6.tar.bz2 |
Use URIIndex.resolve in resolveLinks modifiers
10 files changed, 101 insertions, 176 deletions
diff --git a/packages/gitbook/src/models/uriIndex.js b/packages/gitbook/src/models/uriIndex.js index 5e1f138..eecdc54 100644 --- a/packages/gitbook/src/models/uriIndex.js +++ b/packages/gitbook/src/models/uriIndex.js @@ -75,35 +75,14 @@ class URIIndex extends Record(DEFAULTS) { } /** - * Resolve an entry to an url - * @param {String} filePath - * @return {String} - */ - resolveToURL(filePath) { - const { directoryIndex } = this; - const uri = this.resolve(filePath); - - if (!directoryIndex || LocationUtils.isExternal(uri)) { - return uri; - } - - return transformURLPath(uri, (pathname) => { - if (path.basename(pathname) == 'index.html') { - pathname = path.dirname(pathname) + '/'; - } - - return pathname; - }); - } - - /** * Resolve a filename to an url, considering that the link to "filePath" * in the file "originPath". * * For example if we are generating doc/README.md and we have a link "/READNE.md": * index.resolveFrom('doc/README.md', '/README.md') === '../index.html' * - * @param {String} filePath + * @param {String} originPath + * @param {String} filePath * @return {String} url */ resolveFrom(originPath, filePath) { @@ -132,6 +111,49 @@ class URIIndex extends Record(DEFAULTS) { }); } + /** + * Normalize an url + * @param {String} uri + * @return {String} uri + */ + normalizeURL(uri) { + const { directoryIndex } = this; + + if (!directoryIndex || LocationUtils.isExternal(uri)) { + return uri; + } + + return transformURLPath(uri, (pathname) => { + if (path.basename(pathname) == 'index.html') { + pathname = path.dirname(pathname) + '/'; + } + + return pathname; + }); + } + + /** + * Resolve an entry to an url + * @param {String} filePath + * @return {String} + */ + resolveToURL(filePath) { + const uri = this.resolve(filePath); + return this.normalizeURL(uri); + } + + /** + * Resolve an entry to an url + * + * @param {String} originPath + * @param {String} filePath + * @return {String} url + */ + resolveToURLFrom(originPath, filePath) { + const uri = this.resolveFrom(originPath, filePath); + return this.normalizeURL(uri); + } + } module.exports = URIIndex; diff --git a/packages/gitbook/src/output/getModifiers.js b/packages/gitbook/src/output/getModifiers.js index 797afb9..3007b02 100644 --- a/packages/gitbook/src/output/getModifiers.js +++ b/packages/gitbook/src/output/getModifiers.js @@ -34,8 +34,7 @@ function getModifiers(output, page) { // Resolve links (.md -> .html) Modifiers.resolveLinks.bind(null, - currentFilePath, - (filePath => urls.resolveToURL(filePath)) + (filePath => urls.resolveToURLFrom(currentFilePath, filePath)) ) ]; } diff --git a/packages/gitbook/src/output/modifiers/__tests__/resolveLinks.js b/packages/gitbook/src/output/modifiers/__tests__/resolveLinks.js index 167af5d..d11a31f 100644 --- a/packages/gitbook/src/output/modifiers/__tests__/resolveLinks.js +++ b/packages/gitbook/src/output/modifiers/__tests__/resolveLinks.js @@ -1,99 +1,29 @@ -const path = require('path'); const cheerio = require('cheerio'); const resolveLinks = require('../resolveLinks'); -describe('resolveLinks', function() { +describe('resolveLinks', () => { function resolveFileBasic(href) { return 'fakeDir/' + href; } - function resolveFileCustom(href) { - if (path.extname(href) == '.md') { - return href.slice(0, -3) + '.html'; - } + it('should resolve path using resolver', () => { + const TEST = '<p>This is a <a href="test/cool.md"></a></p>'; + const $ = cheerio.load(TEST); - return href; - } - - describe('Absolute path', function() { - const TEST = '<p>This is a <a href="/test/cool.md"></a></p>'; - - it('should resolve path starting by "/" in root directory', function() { - const $ = cheerio.load(TEST); - - return resolveLinks('hello.md', resolveFileBasic, $) - .then(function() { - const link = $('a'); - expect(link.attr('href')).toBe('fakeDir/test/cool.md'); - }); - }); - - it('should resolve path starting by "/" in child directory', function() { - const $ = cheerio.load(TEST); - - return resolveLinks('afolder/hello.md', resolveFileBasic, $) - .then(function() { - const link = $('a'); - expect(link.attr('href')).toBe('../fakeDir/test/cool.md'); - }); - }); - }); - - describe('Anchor', function() { - it('should prevent anchors in resolution', function() { - const TEST = '<p>This is a <a href="test/cool.md#an-anchor"></a></p>'; - const $ = cheerio.load(TEST); - - return resolveLinks('hello.md', resolveFileCustom, $) - .then(function() { - const link = $('a'); - expect(link.attr('href')).toBe('test/cool.html#an-anchor'); - }); - }); - - it('should ignore pure anchor links', function() { - const TEST = '<p>This is a <a href="#an-anchor"></a></p>'; - const $ = cheerio.load(TEST); - - return resolveLinks('hello.md', resolveFileCustom, $) - .then(function() { - const link = $('a'); - expect(link.attr('href')).toBe('#an-anchor'); - }); - }); - }); - - describe('Custom Resolver', function() { - const TEST = '<p>This is a <a href="/test/cool.md"></a> <a href="afile.png"></a></p>'; - - it('should resolve path correctly for absolute path', function() { - const $ = cheerio.load(TEST); - - return resolveLinks('hello.md', resolveFileCustom, $) - .then(function() { - const link = $('a').first(); - expect(link.attr('href')).toBe('test/cool.html'); - }); - }); - - it('should resolve path correctly for absolute path (2)', function() { - const $ = cheerio.load(TEST); - - return resolveLinks('afodler/hello.md', resolveFileCustom, $) - .then(function() { - const link = $('a').first(); - expect(link.attr('href')).toBe('../test/cool.html'); - }); + return resolveLinks(resolveFileBasic, $) + .then(function() { + const link = $('a'); + expect(link.attr('href')).toBe('fakeDir/test/cool.md'); }); }); - describe('External link', function() { + describe('External link', () => { const TEST = '<p>This is a <a href="http://www.github.com">external link</a></p>'; - it('should have target="_blank" attribute', function() { + it('should have target="_blank" attribute', () => { const $ = cheerio.load(TEST); - return resolveLinks('hello.md', resolveFileBasic, $) + return resolveLinks(resolveFileBasic, $) .then(function() { const link = $('a'); expect(link.attr('target')).toBe('_blank'); diff --git a/packages/gitbook/src/output/modifiers/addHeadingId.js b/packages/gitbook/src/output/modifiers/addHeadingId.js index e5bab3e..e528b9d 100644 --- a/packages/gitbook/src/output/modifiers/addHeadingId.js +++ b/packages/gitbook/src/output/modifiers/addHeadingId.js @@ -2,20 +2,18 @@ const slug = require('github-slugid'); const editHTMLElement = require('./editHTMLElement'); /** - Add ID to an heading - - @param {HTMLElement} heading -*/ + * Add ID to an heading. + * @param {HTMLElement} heading + */ function addId(heading) { if (heading.attr('id')) return; heading.attr('id', slug(heading.text())); } /** - Add ID to all headings - - @param {HTMLDom} $ -*/ + * Add ID to all headings. + * @param {HTMLDom} $ + */ function addHeadingId($) { return editHTMLElement($, 'h1,h2,h3,h4,h5,h6', addId); } diff --git a/packages/gitbook/src/output/modifiers/fetchRemoteImages.js b/packages/gitbook/src/output/modifiers/fetchRemoteImages.js index 1732247..f022093 100644 --- a/packages/gitbook/src/output/modifiers/fetchRemoteImages.js +++ b/packages/gitbook/src/output/modifiers/fetchRemoteImages.js @@ -6,13 +6,13 @@ const fs = require('../../utils/fs'); const LocationUtils = require('../../utils/location'); /** - Fetch all remote images - - @param {String} rootFolder - @param {String} currentFile - @param {HTMLDom} $ - @return {Promise} -*/ + * Fetch all remote images + * + * @param {String} rootFolder + * @param {String} currentFile + * @param {HTMLDom} $ + * @return {Promise} + */ function fetchRemoteImages(rootFolder, currentFile, $) { const currentDirectory = path.dirname(currentFile); diff --git a/packages/gitbook/src/output/modifiers/inlineAssets.js b/packages/gitbook/src/output/modifiers/inlineAssets.js index 1ed4344..4541fcc 100644 --- a/packages/gitbook/src/output/modifiers/inlineAssets.js +++ b/packages/gitbook/src/output/modifiers/inlineAssets.js @@ -7,10 +7,10 @@ const fetchRemoteImages = require('./fetchRemoteImages'); const Promise = require('../../utils/promise'); /** - Inline all assets in a page - - @param {String} rootFolder -*/ + * Inline all assets in a page + * + * @param {String} rootFolder + */ function inlineAssets(rootFolder, currentFile) { return function($) { return Promise() diff --git a/packages/gitbook/src/output/modifiers/inlinePng.js b/packages/gitbook/src/output/modifiers/inlinePng.js index 218aaa2..bf14e4f 100644 --- a/packages/gitbook/src/output/modifiers/inlinePng.js +++ b/packages/gitbook/src/output/modifiers/inlinePng.js @@ -8,12 +8,12 @@ const LocationUtils = require('../../utils/location'); const editHTMLElement = require('./editHTMLElement'); /** - Convert all inline PNG images to PNG file - - @param {String} rootFolder - @param {HTMLDom} $ - @return {Promise} -*/ + * Convert all inline PNG images to PNG file + * + * @param {String} rootFolder + * @param {HTMLDom} $ + * @return {Promise} + */ function inlinePng(rootFolder, currentFile, $) { const currentDirectory = path.dirname(currentFile); @@ -43,5 +43,4 @@ function inlinePng(rootFolder, currentFile, $) { }); } - module.exports = inlinePng; diff --git a/packages/gitbook/src/output/modifiers/modifyHTML.js b/packages/gitbook/src/output/modifiers/modifyHTML.js index 00177fc..64abd07 100644 --- a/packages/gitbook/src/output/modifiers/modifyHTML.js +++ b/packages/gitbook/src/output/modifiers/modifyHTML.js @@ -2,13 +2,13 @@ const cheerio = require('cheerio'); const Promise = require('../../utils/promise'); /** - Apply a list of operations to a page and - output the new page. - - @param {Page} - @param {List|Array<Transformation>} - @return {Promise<Page>} -*/ + * Apply a list of operations to a page and + * output the new page. + * + * @param {Page} page + * @param {List|Array<Transformation>} operations + * @return {Promise<Page>} page + */ function modifyHTML(page, operations) { const html = page.getContent(); const $ = cheerio.load(html); diff --git a/packages/gitbook/src/output/modifiers/resolveImages.js b/packages/gitbook/src/output/modifiers/resolveImages.js index 339ddeb..c647fde 100644 --- a/packages/gitbook/src/output/modifiers/resolveImages.js +++ b/packages/gitbook/src/output/modifiers/resolveImages.js @@ -4,12 +4,12 @@ const LocationUtils = require('../../utils/location'); const editHTMLElement = require('./editHTMLElement'); /** - Resolve all HTML images: - - /test.png in hello -> ../test.html - - @param {String} currentFile - @param {HTMLDom} $ -*/ + * Resolve all HTML images: + * - /test.png in hello -> ../test.html + * + * @param {String} currentFile + * @param {HTMLDom} $ + */ function resolveImages(currentFile, $) { const currentDirectory = path.dirname(currentFile); diff --git a/packages/gitbook/src/output/modifiers/resolveLinks.js b/packages/gitbook/src/output/modifiers/resolveLinks.js index 8b15315..ca81ccb 100644 --- a/packages/gitbook/src/output/modifiers/resolveLinks.js +++ b/packages/gitbook/src/output/modifiers/resolveLinks.js @@ -1,20 +1,14 @@ -const path = require('path'); -const url = require('url'); - const LocationUtils = require('../../utils/location'); const editHTMLElement = require('./editHTMLElement'); /** - Resolve all HTML links: - - /test.md in hello -> ../test.html - - @param {String} currentFile - @param {Function(String) -> String} resolveFile - @param {HTMLDom} $ -*/ -function resolveLinks(currentFile, resolveFile, $) { - const currentDirectory = path.dirname(currentFile); - + * Resolve all HTML links: + * - /test.md in hello -> ../test.html + * + * @param {Function(String) -> String} resolveURL + * @param {HTMLDom} $ + */ +function resolveLinks(resolveURL, $) { return editHTMLElement($, 'a', function($a) { let href = $a.attr('href'); @@ -28,24 +22,7 @@ function resolveLinks(currentFile, resolveFile, $) { return; } - // Split anchor - const parsed = url.parse(href); - href = parsed.pathname || ''; - - if (href) { - // Calcul absolute path for this - href = LocationUtils.toAbsolute(href, currentDirectory, '.'); - - // Resolve file - href = resolveFile(href); - - // Convert back to relative - href = LocationUtils.relative(currentDirectory, href); - } - - // Add back anchor - href = href + (parsed.hash || ''); - + href = resolveURL(href); $a.attr('href', href); }); } |