diff options
author | Samy Pesse <samypesse@gmail.com> | 2016-10-06 20:08:47 +0200 |
---|---|---|
committer | Samy Pesse <samypesse@gmail.com> | 2016-10-06 20:08:47 +0200 |
commit | 58ae96b7f6a5cd3b7c1fd1d650fcaae4686f578d (patch) | |
tree | c1555e51d2d399062d52a8b1df8eda1852a2bf7c | |
parent | 1354582c4e0d61c7608965f36b3c27ee3e39dc78 (diff) | |
download | gitbook-58ae96b7f6a5cd3b7c1fd1d650fcaae4686f578d.zip gitbook-58ae96b7f6a5cd3b7c1fd1d650fcaae4686f578d.tar.gz gitbook-58ae96b7f6a5cd3b7c1fd1d650fcaae4686f578d.tar.bz2 |
Add "url" to summary articles and files in json representation
33 files changed, 268 insertions, 411 deletions
diff --git a/packages/gitbook-core/src/components/Link.js b/packages/gitbook-core/src/components/Link.js index f5b9753..145486e 100644 --- a/packages/gitbook-core/src/components/Link.js +++ b/packages/gitbook-core/src/components/Link.js @@ -19,19 +19,16 @@ const Link = React.createClass({ ]) }, - getHref() { - let { currentFile, to } = this.props; + render() { + const { currentFile, to, children, ...props } = this.props; + let href = to; if (SummaryArticle.is(to) || File.is(to)) { - to = to.url; + href = to.url; } - return currentFile.relative(to); - }, + href = currentFile.relative(href); - render() { - const { children, ...props } = this.props; - const href = this.getHref(); return <a href={href} {...props}>{children}</a>; } }); diff --git a/packages/gitbook-core/src/models/SummaryArticle.js b/packages/gitbook-core/src/models/SummaryArticle.js index 0f8ca0d..3651c8a 100644 --- a/packages/gitbook-core/src/models/SummaryArticle.js +++ b/packages/gitbook-core/src/models/SummaryArticle.js @@ -4,6 +4,7 @@ const DEFAULTS = { title: '', depth: 0, path: '', + url: '', ref: '', level: '', articles: List() diff --git a/packages/gitbook-core/src/shapes/SummaryArticle.js b/packages/gitbook-core/src/shapes/SummaryArticle.js index c5aa717..c93bdd9 100644 --- a/packages/gitbook-core/src/shapes/SummaryArticle.js +++ b/packages/gitbook-core/src/shapes/SummaryArticle.js @@ -13,6 +13,7 @@ const Article = shape({ title: string.isRequired, depth: number.isRequired, path: string, + url: string, ref: string, level: string, articles: list diff --git a/packages/gitbook/src/api/encodeConfig.js b/packages/gitbook/src/api/encodeConfig.js index 096f136..cdfc0b7 100644 --- a/packages/gitbook/src/api/encodeConfig.js +++ b/packages/gitbook/src/api/encodeConfig.js @@ -2,12 +2,12 @@ const objectPath = require('object-path'); const deprecate = require('./deprecate'); /** - Encode a config object into a JS config api - - @param {Output} output - @param {Config} config - @return {Object} -*/ + * Encode a config object into a JS config api + * + * @param {Output} output + * @param {Config} config + * @return {Object} + */ function encodeConfig(output, config) { const result = { values: config.getValues().toJS(), diff --git a/packages/gitbook/src/api/encodeGlobal.js b/packages/gitbook/src/api/encodeGlobal.js index 21a1e73..18ebdf0 100644 --- a/packages/gitbook/src/api/encodeGlobal.js +++ b/packages/gitbook/src/api/encodeGlobal.js @@ -5,7 +5,6 @@ const fs = require('../utils/fs'); const Plugins = require('../plugins'); const deprecate = require('./deprecate'); -const fileToURL = require('../output/helper/fileToURL'); const defaultBlocks = require('../constants/defaultBlocks'); const gitbook = require('../gitbook'); const parsers = require('../parsers'); @@ -16,12 +15,12 @@ const encodeNavigation = require('./encodeNavigation'); const encodePage = require('./encodePage'); /** - Encode a global context into a JS object - It's the context for page's hook, etc - - @param {Output} output - @return {Object} -*/ + * Encode a global context into a JS object + * It's the context for page's hook, etc + * + * @param {Output} output + * @return {Object} + */ function encodeGlobal(output) { const book = output.getBook(); const bookFS = book.getContentFS(); @@ -29,6 +28,7 @@ function encodeGlobal(output) { const outputFolder = output.getRoot(); const plugins = output.getPlugins(); const blocks = Plugins.listBlocks(plugins); + const urls = output.getURLIndex(); const result = { log: logger, @@ -36,59 +36,53 @@ function encodeGlobal(output) { summary: encodeSummary(output, book.getSummary()), /** - Check if the book is a multilingual book - - @return {Boolean} - */ + * Check if the book is a multilingual book. + * @return {Boolean} + */ isMultilingual() { return book.isMultilingual(); }, /** - Check if the book is a language book for a multilingual book - - @return {Boolean} - */ + * Check if the book is a language book for a multilingual book. + * @return {Boolean} + */ isLanguageBook() { return book.isLanguageBook(); }, /** - Read a file from the book - - @param {String} fileName - @return {Promise<Buffer>} - */ + * Read a file from the book. + * @param {String} fileName + * @return {Promise<Buffer>} + */ readFile(fileName) { return bookFS.read(fileName); }, /** - Read a file from the book as a string - - @param {String} fileName - @return {Promise<String>} - */ + * Read a file from the book as a string. + * @param {String} fileName + * @return {Promise<String>} + */ readFileAsString(fileName) { return bookFS.readAsString(fileName); }, /** - Resolve a file from the book root - - @param {String} fileName - @return {String} + * Resolve a file from the book root. + * @param {String} fileName + * @return {String} */ resolve(fileName) { return path.resolve(book.getContentRoot(), fileName); }, /** - Resolve a page by it path - - @param {String} filePath - @return {String} - */ + * Resolve a page by it path. + * @param {String} filePath + * @return {String} + */ getPageByPath(filePath) { const page = output.getPage(filePath); if (!page) return undefined; @@ -97,12 +91,11 @@ function encodeGlobal(output) { }, /** - Render a block of text (markdown/asciidoc) - - @param {String} type - @param {String} text - @return {Promise<String>} - */ + * Render a block of text (markdown/asciidoc). + * @param {String} type + * @param {String} text + * @return {Promise<String>} + */ renderBlock(type, text) { const parser = parsers.get(type); @@ -111,12 +104,11 @@ function encodeGlobal(output) { }, /** - Render an inline text (markdown/asciidoc) - - @param {String} type - @param {String} text - @return {Promise<String>} - */ + * Render an inline text (markdown/asciidoc). + * @param {String} type + * @param {String} text + * @return {Promise<String>} + */ renderInline(type, text) { const parser = parsers.get(type); @@ -127,12 +119,11 @@ function encodeGlobal(output) { template: { /** - Apply a templating block and returns its result - - @param {String} name - @param {Object} blockData - @return {Promise|Object} - */ + * Apply a templating block and returns its result. + * @param {String} name + * @param {Object} blockData + * @return {Promise|Object} + */ applyBlock(name, blockData) { const block = blocks.get(name) || defaultBlocks.get(name); return Promise(block.applyBlock(blockData, result)); @@ -142,43 +133,41 @@ function encodeGlobal(output) { output: { /** - Name of the generator being used - {String} - */ + * Name of the generator being used + * {String} + */ name: output.getGenerator(), /** - Return absolute path to the root folder of output - @return {String} - */ + * Return absolute path to the root folder of output + * @return {String} + */ root() { return outputFolder; }, /** - Resolve a file from the output root - - @param {String} fileName - @return {String} - */ + * Resolve a file from the output root. + * @param {String} fileName + * @return {String} + */ resolve(fileName) { return path.resolve(outputFolder, fileName); }, /** - Convert a filepath into an url - @return {String} - */ + * Convert a filepath into an url + * @return {String} + */ toURL(filePath) { - return fileToURL(output, filePath); + return urls.resolveToURL(filePath); }, /** - Check that a file exists. - - @param {String} fileName - @return {Promise} - */ + * Check that a file exists. + * @param {String} fileName + * @return {Promise} + */ hasFile(fileName, content) { return Promise() .then(function() { @@ -189,13 +178,13 @@ function encodeGlobal(output) { }, /** - Write a file to the output folder, - It creates the required folder - - @param {String} fileName - @param {Buffer} content - @return {Promise} - */ + * Write a file to the output folder, + * It creates the required folder + * + * @param {String} fileName + * @param {Buffer} content + * @return {Promise} + */ writeFile(fileName, content) { return Promise() .then(function() { @@ -209,14 +198,14 @@ function encodeGlobal(output) { }, /** - Copy a file to the output folder - It creates the required folder. - - @param {String} inputFile - @param {String} outputFile - @param {Buffer} content - @return {Promise} - */ + * Copy a file to the output folder + * It creates the required folder. + * + * @param {String} inputFile + * @param {String} outputFile + * @param {Buffer} content + * @return {Promise} + */ copyFile(inputFile, outputFile, content) { return Promise() .then(function() { diff --git a/packages/gitbook/src/api/encodeNavigation.js b/packages/gitbook/src/api/encodeNavigation.js index d54239d..95ab8e3 100644 --- a/packages/gitbook/src/api/encodeNavigation.js +++ b/packages/gitbook/src/api/encodeNavigation.js @@ -1,12 +1,12 @@ const Immutable = require('immutable'); /** - Encode an article for next/prev - - @param {Map<String:Page>} - @param {Article} - @return {Object} -*/ + * Encode an article for next/prev + * + * @param {Map<String:Page>} + * @param {Article} + * @return {Object} + */ function encodeArticle(pages, article) { const articlePath = article.getPath(); @@ -20,11 +20,11 @@ function encodeArticle(pages, article) { } /** - this.navigation is a deprecated property from GitBook v2 - - @param {Output} - @return {Object} -*/ + * this.navigation is a deprecated property from GitBook v2 + * + * @param {Output} + * @return {Object} + */ function encodeNavigation(output) { const book = output.getBook(); const pages = output.getPages(); diff --git a/packages/gitbook/src/api/encodePage.js b/packages/gitbook/src/api/encodePage.js index fb77fcd..48119f7 100644 --- a/packages/gitbook/src/api/encodePage.js +++ b/packages/gitbook/src/api/encodePage.js @@ -3,20 +3,21 @@ const deprecate = require('./deprecate'); const encodeProgress = require('./encodeProgress'); /** - Encode a page in a context to a JS API - - @param {Output} output - @param {Page} page - @return {Object} -*/ + * Encode a page in a context to a JS API + * + * @param {Output} output + * @param {Page} page + * @return {Object} + */ function encodePage(output, page) { const book = output.getBook(); + const urls = output.getURLIndex(); const summary = book.getSummary(); const fs = book.getContentFS(); const file = page.getFile(); // JS Page is based on the JSON output - const result = JSONUtils.encodePage(page, summary); + const result = JSONUtils.encodePage(page, summary, urls); result.type = file.getType(); result.path = file.getPath(); diff --git a/packages/gitbook/src/json/encodeBook.js b/packages/gitbook/src/json/encodeBook.js deleted file mode 100644 index c5632d2..0000000 --- a/packages/gitbook/src/json/encodeBook.js +++ /dev/null @@ -1,39 +0,0 @@ -const extend = require('extend'); - -const gitbook = require('../gitbook'); -const encodeSummary = require('./encodeSummary'); -const encodeGlossary = require('./encodeGlossary'); -const encodeReadme = require('./encodeReadme'); -const encodeLanguages = require('./encodeLanguages'); - -/** - * Encode a book to JSON - * - * @param {Book} - * @return {Object} - */ -function encodeBookToJson(book) { - const config = book.getConfig(); - const language = book.getLanguage(); - - const variables = config.getValue('variables', {}); - - return { - summary: encodeSummary(book.getSummary()), - glossary: encodeGlossary(book.getGlossary()), - readme: encodeReadme(book.getReadme()), - config: book.getConfig().getValues().toJS(), - - languages: book.isMultilingual() ? encodeLanguages(book.getLanguages()) : undefined, - - gitbook: { - version: gitbook.version, - time: gitbook.START_TIME - }, - book: extend({ - language: language ? language : undefined - }, variables.toJS()) - }; -} - -module.exports = encodeBookToJson; diff --git a/packages/gitbook/src/json/encodeBookWithPage.js b/packages/gitbook/src/json/encodeBookWithPage.js deleted file mode 100644 index f593af2..0000000 --- a/packages/gitbook/src/json/encodeBookWithPage.js +++ /dev/null @@ -1,22 +0,0 @@ -const encodeBook = require('./encodeBook'); -const encodePage = require('./encodePage'); -const encodeFile = require('./encodeFile'); - -/** - * Return a JSON representation of a book with a specific file - * - * @param {Book} output - * @param {Page} page - * @return {Object} - */ -function encodeBookWithPage(book, page) { - const file = page.getFile(); - - const result = encodeBook(book); - result.page = encodePage(page, book.getSummary()); - result.file = encodeFile(file); - - return result; -} - -module.exports = encodeBookWithPage; diff --git a/packages/gitbook/src/json/encodeFile.js b/packages/gitbook/src/json/encodeFile.js index 1f41228..2295ac1 100644 --- a/packages/gitbook/src/json/encodeFile.js +++ b/packages/gitbook/src/json/encodeFile.js @@ -2,11 +2,11 @@ /** * Return a JSON representation of a file * - * @param {File} file - * @param {String} url? - * @return {Object} json + * @param {File} file + * @param {URIIndex} urls + * @return {JSON} json */ -function encodeFileToJson(file, url) { +function encodeFileToJson(file, urls) { const filePath = file.getPath(); if (!filePath) { return undefined; @@ -16,7 +16,7 @@ function encodeFileToJson(file, url) { path: filePath, mtime: file.getMTime(), type: file.getType(), - url + url: urls.resolveToURL(filePath) }; } diff --git a/packages/gitbook/src/json/encodeGlossary.js b/packages/gitbook/src/json/encodeGlossary.js index 6bdd683..d82bb62 100644 --- a/packages/gitbook/src/json/encodeGlossary.js +++ b/packages/gitbook/src/json/encodeGlossary.js @@ -2,17 +2,18 @@ const encodeFile = require('./encodeFile'); const encodeGlossaryEntry = require('./encodeGlossaryEntry'); /** - Encode a glossary to JSON - - @param {Glossary} - @return {Object} -*/ -function encodeGlossary(glossary) { + * Encode a glossary to JSON + * + * @param {Glossary} glossary + * @param {URIIndex} urls + * @return {JSON} json + */ +function encodeGlossary(glossary, urls) { const file = glossary.getFile(); const entries = glossary.getEntries(); return { - file: encodeFile(file), + file: encodeFile(file, urls), entries: entries .map(encodeGlossaryEntry).toJS() }; diff --git a/packages/gitbook/src/json/encodeGlossaryEntry.js b/packages/gitbook/src/json/encodeGlossaryEntry.js index d163f45..52e13c3 100644 --- a/packages/gitbook/src/json/encodeGlossaryEntry.js +++ b/packages/gitbook/src/json/encodeGlossaryEntry.js @@ -1,10 +1,10 @@ /** - Encode a SummaryArticle to JSON - - @param {GlossaryEntry} - @return {Object} -*/ + * Encode a SummaryArticle to JSON + * + * @param {GlossaryEntry} entry + * @return {JSON} json + */ function encodeGlossaryEntry(entry) { return { id: entry.getID(), diff --git a/packages/gitbook/src/json/encodeLanguages.js b/packages/gitbook/src/json/encodeLanguages.js index fc7487b..e1ef2ef 100644 --- a/packages/gitbook/src/json/encodeLanguages.js +++ b/packages/gitbook/src/json/encodeLanguages.js @@ -1,17 +1,18 @@ const encodeFile = require('./encodeFile'); /** - Encode a languages listing to JSON - - @param {Languages} - @return {Object} + * Encode a languages listing to JSON + * + * @param {Languages} languages + * @param {URIIndex} urls + * @return {JSON} json */ -function encodeLanguages(languages) { +function encodeLanguages(languages, urls) { const file = languages.getFile(); const list = languages.getList(); return { - file: encodeFile(file), + file: encodeFile(file, urls), list: list .valueSeq() .map(function(lang) { diff --git a/packages/gitbook/src/json/encodeOutput.js b/packages/gitbook/src/json/encodeOutput.js deleted file mode 100644 index 31e5757..0000000 --- a/packages/gitbook/src/json/encodeOutput.js +++ /dev/null @@ -1,25 +0,0 @@ -const encodeBook = require('./encodeBook'); - -/** - * Encode an output to JSON - * - * @param {Output} - * @return {Object} - */ -function encodeOutputToJson(output) { - const book = output.getBook(); - const generator = output.getGenerator(); - const options = output.getOptions(); - - const result = encodeBook(book); - - result.output = { - name: generator - }; - - result.options = options.toJS(); - - return result; -} - -module.exports = encodeOutputToJson; diff --git a/packages/gitbook/src/json/encodeOutputWithPage.js b/packages/gitbook/src/json/encodeOutputWithPage.js deleted file mode 100644 index e6cb0c5..0000000 --- a/packages/gitbook/src/json/encodeOutputWithPage.js +++ /dev/null @@ -1,25 +0,0 @@ -const resolveFileToURL = require('../output/helper/resolveFileToURL'); - -const encodeOutput = require('./encodeOutput'); -const encodePage = require('./encodePage'); -const encodeFile = require('./encodeFile'); - -/** - * Return a JSON representation of a book with a specific file - * - * @param {Book} output - * @param {Page} page - * @return {Object} - */ -function encodeOutputWithPage(output, page) { - const file = page.getFile(); - const book = output.getBook(); - - const result = encodeOutput(output); - result.page = encodePage(page, book.getSummary()); - result.file = encodeFile(file, resolveFileToURL(output, file.getPath())); - - return result; -} - -module.exports = encodeOutputWithPage; diff --git a/packages/gitbook/src/json/encodePage.js b/packages/gitbook/src/json/encodePage.js index 0d31d7e..6610308 100644 --- a/packages/gitbook/src/json/encodePage.js +++ b/packages/gitbook/src/json/encodePage.js @@ -1,13 +1,14 @@ const encodeSummaryArticle = require('./encodeSummaryArticle'); /** - * Return a JSON representation of a page + * Return a JSON representation of a page. * - * @param {Page} page - * @param {Summary} summary - * @return {Object} json + * @param {Page} page + * @param {Summary} summary + * @param {URIIndex} urls + * @return {JSON} json */ -function encodePage(page, summary) { +function encodePage(page, summary, urls) { const file = page.getFile(); const attributes = page.getAttributes(); const article = summary.getByPath(file.getPath()); @@ -21,12 +22,12 @@ function encodePage(page, summary) { const nextArticle = summary.getNextArticle(article); if (nextArticle) { - result.next = encodeSummaryArticle(nextArticle); + result.next = encodeSummaryArticle(nextArticle, urls, false); } const prevArticle = summary.getPrevArticle(article); if (prevArticle) { - result.previous = encodeSummaryArticle(prevArticle); + result.previous = encodeSummaryArticle(prevArticle, urls, false); } } diff --git a/packages/gitbook/src/json/encodeReadme.js b/packages/gitbook/src/json/encodeReadme.js index 5fe5a63..dff81cf 100644 --- a/packages/gitbook/src/json/encodeReadme.js +++ b/packages/gitbook/src/json/encodeReadme.js @@ -1,16 +1,17 @@ const encodeFile = require('./encodeFile'); /** - * Encode a readme to JSON + * Encode a readme to JSON. * - * @param {Readme} - * @return {Object} + * @param {Readme} readme + * @param {URIIndex} urls + * @return {JSON} json */ -function encodeReadme(readme) { +function encodeReadme(readme, urls) { const file = readme.getFile(); return { - file: encodeFile(file) + file: encodeFile(file, urls) }; } diff --git a/packages/gitbook/src/json/encodeState.js b/packages/gitbook/src/json/encodeState.js new file mode 100644 index 0000000..d264835 --- /dev/null +++ b/packages/gitbook/src/json/encodeState.js @@ -0,0 +1,42 @@ +const gitbook = require('../gitbook'); +const encodeSummary = require('./encodeSummary'); +const encodeGlossary = require('./encodeGlossary'); +const encodeReadme = require('./encodeReadme'); +const encodeLanguages = require('./encodeLanguages'); +const encodePage = require('./encodePage'); +const encodeFile = require('./encodeFile'); + +/** + * Encode context to JSON from an output instance. + * This JSON representation is used as initial state for the redux store. + * + * @param {Output} output + * @param {Page} page + * @return {JSON} + */ +function encodeStateToJSON(output, page) { + const book = output.getBook(); + const urls = output.getURLIndex(); + const file = page.getFile(); + + return { + output: { + name: output.getGenerator() + }, + gitbook: { + version: gitbook.version, + time: gitbook.START_TIME + }, + + summary: encodeSummary(book.getSummary(), urls), + glossary: encodeGlossary(book.getGlossary(), urls), + readme: encodeReadme(book.getReadme(), urls), + config: book.getConfig().getValues().toJS(), + languages: book.isMultilingual() ? encodeLanguages(book.getLanguages()) : undefined, + + page: encodePage(page, book.getSummary(), urls), + file: encodeFile(file, urls) + }; +} + +module.exports = encodeStateToJSON; diff --git a/packages/gitbook/src/json/encodeSummary.js b/packages/gitbook/src/json/encodeSummary.js index 0a9dbe7..8380379 100644 --- a/packages/gitbook/src/json/encodeSummary.js +++ b/packages/gitbook/src/json/encodeSummary.js @@ -4,16 +4,19 @@ const encodeSummaryPart = require('./encodeSummaryPart'); /** * Encode a summary to JSON * - * @param {Summary} + * @param {Summary} summary + * @param {URIIndex} urls * @return {Object} */ -function encodeSummary(summary) { +function encodeSummary(summary, urls) { const file = summary.getFile(); const parts = summary.getParts(); return { - file: encodeFile(file), - parts: parts.map(encodeSummaryPart).toJS() + file: encodeFile(file, urls), + parts: parts + .map(part => encodeSummaryPart(part, urls)) + .toJS() }; } diff --git a/packages/gitbook/src/json/encodeSummaryArticle.js b/packages/gitbook/src/json/encodeSummaryArticle.js index b6d7d9f..0fb6368 100644 --- a/packages/gitbook/src/json/encodeSummaryArticle.js +++ b/packages/gitbook/src/json/encodeSummaryArticle.js @@ -2,14 +2,16 @@ /** * Encode a SummaryArticle to JSON * - * @param {SummaryArticle} + * @param {SummaryArticle} article + * @param {URIIndex} urls + * @param {Boolean} recursive * @return {Object} */ -function encodeSummaryArticle(article, recursive) { +function encodeSummaryArticle(article, urls, recursive) { let articles = undefined; if (recursive !== false) { articles = article.getArticles() - .map(encodeSummaryArticle) + .map(innerArticle => encodeSummaryArticle(innerArticle, urls, recursive)) .toJS(); } @@ -18,7 +20,7 @@ function encodeSummaryArticle(article, recursive) { level: article.getLevel(), depth: article.getDepth(), anchor: article.getAnchor(), - url: article.getUrl(), + url: urls.resolveToURL(article.getPath() || article.getUrl()), path: article.getPath(), ref: article.getRef(), articles diff --git a/packages/gitbook/src/json/encodeSummaryPart.js b/packages/gitbook/src/json/encodeSummaryPart.js index 9ff3583..fbcdc4c 100644 --- a/packages/gitbook/src/json/encodeSummaryPart.js +++ b/packages/gitbook/src/json/encodeSummaryPart.js @@ -1,16 +1,18 @@ const encodeSummaryArticle = require('./encodeSummaryArticle'); /** - * Encode a SummaryPart to JSON + * Encode a SummaryPart to JSON. * - * @param {SummaryPart} - * @return {Object} + * @param {SummaryPart} part + * @param {URIIndex} urls + * @return {JSON} json */ -function encodeSummaryPart(part) { +function encodeSummaryPart(part, urls) { return { title: part.getTitle(), articles: part.getArticles() - .map(encodeSummaryArticle).toJS() + .map(article => encodeSummaryArticle(article, urls)) + .toJS() }; } diff --git a/packages/gitbook/src/json/index.js b/packages/gitbook/src/json/index.js index 3b68f5e..49ab195 100644 --- a/packages/gitbook/src/json/index.js +++ b/packages/gitbook/src/json/index.js @@ -1,13 +1,10 @@ module.exports = { - encodeOutput: require('./encodeOutput'), - encodeBookWithPage: require('./encodeBookWithPage'), - encodeOutputWithPage: require('./encodeOutputWithPage'), - encodeBook: require('./encodeBook'), - encodeFile: require('./encodeFile'), - encodePage: require('./encodePage'), - encodeSummary: require('./encodeSummary'), - encodeSummaryArticle: require('./encodeSummaryArticle'), - encodeReadme: require('./encodeReadme'), - encodeLanguages: require('./encodeLanguages') + encodeState: require('./encodeState'), + encodeFile: require('./encodeFile'), + encodePage: require('./encodePage'), + encodeSummary: require('./encodeSummary'), + encodeSummaryArticle: require('./encodeSummaryArticle'), + encodeReadme: require('./encodeReadme'), + encodeLanguages: require('./encodeLanguages') }; diff --git a/packages/gitbook/src/models/output.js b/packages/gitbook/src/models/output.js index e4870ba..ab37c67 100644 --- a/packages/gitbook/src/models/output.js +++ b/packages/gitbook/src/models/output.js @@ -51,6 +51,10 @@ class Output extends Record(DEFAULTS) { return this.get('state'); } + getURLIndex() { + return this.get('urls'); + } + /** * Return a page byt its file path * diff --git a/packages/gitbook/src/models/uriIndex.js b/packages/gitbook/src/models/uriIndex.js index 32de3cf..5e1f138 100644 --- a/packages/gitbook/src/models/uriIndex.js +++ b/packages/gitbook/src/models/uriIndex.js @@ -9,7 +9,8 @@ const LocationUtils = require('../utils/location'); */ const DEFAULTS = { - uris: Map() + uris: Map(), + directoryIndex: Boolean(true) }; /** @@ -74,6 +75,28 @@ 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". * diff --git a/packages/gitbook/src/output/generatePage.js b/packages/gitbook/src/output/generatePage.js index 0fc99a3..671ac54 100644 --- a/packages/gitbook/src/output/generatePage.js +++ b/packages/gitbook/src/output/generatePage.js @@ -27,7 +27,7 @@ function generatePage(output, page) { const file = resultPage.getFile(); const filePath = file.getPath(); const parser = file.getParser(); - const context = JSONUtils.encodeOutputWithPage(output, resultPage); + const context = JSONUtils.encodeState(output, resultPage); if (!parser) { return Promise.reject(error.FileNotParsableError({ diff --git a/packages/gitbook/src/output/getModifiers.js b/packages/gitbook/src/output/getModifiers.js index c2ac71f..797afb9 100644 --- a/packages/gitbook/src/output/getModifiers.js +++ b/packages/gitbook/src/output/getModifiers.js @@ -1,7 +1,4 @@ const Modifiers = require('./modifiers'); -const resolveFileToURL = require('./helper/resolveFileToURL'); -const fileToOutput = require('./helper/fileToOutput'); - /** * Return default modifier to prepare a page for @@ -14,10 +11,13 @@ function getModifiers(output, page) { const glossary = book.getGlossary(); const file = page.getFile(); + // Map of urls + const urls = output.getURLIndex(); + // Glossary entries const entries = glossary.getEntries(); const glossaryFile = glossary.getFile(); - const glossaryFilename = fileToOutput(output, glossaryFile.getPath()); + const glossaryFilename = urls.resolveToURL(glossaryFile.getPath()); // Current file path const currentFilePath = file.getPath(); @@ -35,7 +35,7 @@ function getModifiers(output, page) { // Resolve links (.md -> .html) Modifiers.resolveLinks.bind(null, currentFilePath, - resolveFileToURL.bind(null, output) + (filePath => urls.resolveToURL(filePath)) ) ]; } diff --git a/packages/gitbook/src/output/helper/fileToOutput.js b/packages/gitbook/src/output/helper/fileToOutput.js deleted file mode 100644 index a514854..0000000 --- a/packages/gitbook/src/output/helper/fileToOutput.js +++ /dev/null @@ -1,32 +0,0 @@ -const path = require('path'); - -const PathUtils = require('../../utils/path'); -const LocationUtils = require('../../utils/location'); - -const OUTPUT_EXTENSION = '.html'; - -/** - * Convert a filePath (absolute) to a filename for output - * - * @param {Output} output - * @param {String} filePath - * @return {String} - */ -function fileToOutput(output, filePath) { - const book = output.getBook(); - const readme = book.getReadme(); - const fileReadme = readme.getFile(); - - if ( - path.basename(filePath, path.extname(filePath)) == 'README' || - (fileReadme.exists() && filePath == fileReadme.getPath()) - ) { - filePath = path.join(path.dirname(filePath), 'index' + OUTPUT_EXTENSION); - } else { - filePath = PathUtils.setExtension(filePath, OUTPUT_EXTENSION); - } - - return LocationUtils.normalize(filePath); -} - -module.exports = fileToOutput; diff --git a/packages/gitbook/src/output/helper/fileToURL.js b/packages/gitbook/src/output/helper/fileToURL.js deleted file mode 100644 index 089dad5..0000000 --- a/packages/gitbook/src/output/helper/fileToURL.js +++ /dev/null @@ -1,31 +0,0 @@ -const path = require('path'); -const LocationUtils = require('../../utils/location'); - -const fileToOutput = require('./fileToOutput'); - -/** - * Convert a filePath (absolute) to an url (without hostname). - * It returns an absolute path. - * - * "README.md" -> "/" - * "test/hello.md" -> "test/hello.html" - * "test/README.md" -> "test/" - * - * @param {Output} output - * @param {String} filePath - * @return {String} - */ -function fileToURL(output, filePath) { - const options = output.getOptions(); - const directoryIndex = options.get('directoryIndex'); - - filePath = fileToOutput(output, filePath); - - if (directoryIndex && path.basename(filePath) == 'index.html') { - filePath = path.dirname(filePath) + '/'; - } - - return LocationUtils.normalize(filePath); -} - -module.exports = fileToURL; diff --git a/packages/gitbook/src/output/helper/resolveFileToURL.js b/packages/gitbook/src/output/helper/resolveFileToURL.js deleted file mode 100644 index 907cfdd..0000000 --- a/packages/gitbook/src/output/helper/resolveFileToURL.js +++ /dev/null @@ -1,26 +0,0 @@ -const LocationUtils = require('../../utils/location'); - -const fileToURL = require('./fileToURL'); - -/** - * Resolve an absolute path (extracted from a link) - * - * @param {Output} output - * @param {String} filePath - * @return {String} - */ -function resolveFileToURL(output, filePath) { - // Convert /test.png -> test.png - filePath = LocationUtils.toAbsolute(filePath, '', ''); - - const page = output.getPage(filePath); - - // if file is a page, return correct .html url - if (page) { - filePath = fileToURL(output, filePath); - } - - return LocationUtils.normalize(filePath); -} - -module.exports = resolveFileToURL; diff --git a/packages/gitbook/src/output/json/onFinish.js b/packages/gitbook/src/output/json/onFinish.js index b05057a..24f5159 100644 --- a/packages/gitbook/src/output/json/onFinish.js +++ b/packages/gitbook/src/output/json/onFinish.js @@ -5,14 +5,15 @@ const fs = require('../../utils/fs'); const JSONUtils = require('../../json'); /** - Finish the generation - - @param {Output} - @return {Output} -*/ + * Finish the generation + * + * @param {Output} + * @return {Output} + */ function onFinish(output) { const book = output.getBook(); const outputRoot = output.getRoot(); + const urls = output.getURLIndex(); if (!book.isMultilingual()) { return Promise(output); @@ -29,7 +30,7 @@ function onFinish(output) { .then(function(content) { const json = JSON.parse(content); - json.languages = JSONUtils.encodeLanguages(languages); + json.languages = JSONUtils.encodeLanguages(languages, urls); return json; }) diff --git a/packages/gitbook/src/output/json/onPage.js b/packages/gitbook/src/output/json/onPage.js index 8bee711..f31fadc 100644 --- a/packages/gitbook/src/output/json/onPage.js +++ b/packages/gitbook/src/output/json/onPage.js @@ -19,7 +19,7 @@ function onPage(output, page) { return Modifiers.modifyHTML(page, getModifiers(output, page)) .then(function(resultPage) { // Generate the JSON - const json = JSONUtils.encodeBookWithPage(output.getBook(), resultPage); + const json = JSONUtils.encodeState(output, resultPage); // Delete some private properties delete json.config; diff --git a/packages/gitbook/src/output/website/onPage.js b/packages/gitbook/src/output/website/onPage.js index c20752d..7dd35f3 100644 --- a/packages/gitbook/src/output/website/onPage.js +++ b/packages/gitbook/src/output/website/onPage.js @@ -1,7 +1,6 @@ const JSONUtils = require('../../json'); const Modifiers = require('../modifiers'); const writeFile = require('../helper/writeFile'); -const fileToOutput = require('../helper/fileToOutput'); const getModifiers = require('../getModifiers'); const render = require('../../browser/render'); @@ -12,16 +11,17 @@ const render = require('../../browser/render'); * @param {Page} page */ function onPage(output, page) { - const file = page.getFile(); - const plugins = output.getPlugins(); + const file = page.getFile(); + const plugins = output.getPlugins(); + const urls = output.getURLIndex(); // Output file path - const filePath = fileToOutput(output, file.getPath()); + const filePath = urls.resolve(file.getPath()); return Modifiers.modifyHTML(page, getModifiers(output, page)) .then(function(resultPage) { // Generate the context - const initialState = JSONUtils.encodeOutputWithPage(output, resultPage); + const initialState = JSONUtils.encodeState(output, resultPage); // Render the theme const html = render(plugins, initialState); diff --git a/packages/gitbook/src/parse/parseURIIndexFromPages.js b/packages/gitbook/src/parse/parseURIIndexFromPages.js index d5f745e..645d083 100644 --- a/packages/gitbook/src/parse/parseURIIndexFromPages.js +++ b/packages/gitbook/src/parse/parseURIIndexFromPages.js @@ -9,15 +9,15 @@ const OUTPUT_EXTENSION = '.html'; * Convert a filePath (absolute) to an url (without hostname). * It returns an absolute path. * - * "README.md" -> "/" + * "README.md" -> "/index.html" * "test/hello.md" -> "test/hello.html" - * "test/README.md" -> "test/" + * "test/README.md" -> "test/index.html" * * @param {Output} output * @param {String} filePath * @return {String} */ -function fileToURL(filePath, directoryIndex) { +function fileToURL(filePath) { if ( path.basename(filePath, path.extname(filePath)) == 'README' ) { @@ -26,10 +26,6 @@ function fileToURL(filePath, directoryIndex) { filePath = PathUtils.setExtension(filePath, OUTPUT_EXTENSION); } - if (directoryIndex && path.basename(filePath) == 'index.html') { - filePath = path.dirname(filePath) + '/'; - } - return LocationUtils.normalize(filePath); } @@ -38,16 +34,10 @@ function fileToURL(filePath, directoryIndex) { * Each pages is added as an entry in the index. * * @param {OrderedMap<Page>} pages - * @param {Boolean} options.directoryIndex: should we use "index.html" or "/" * @return {URIIndex} index */ -function parseURIIndexFromPages(pages, options) { - options = options || {}; - if (typeof options.directoryIndex === 'undefined') { - options.directoryIndex = true; - } - - const urls = pages.map((page, filePath) => fileToURL(filePath, options.directoryIndex)); +function parseURIIndexFromPages(pages) { + const urls = pages.map((page, filePath) => fileToURL(filePath)); return new URIIndex(urls); } |