diff options
author | Samy Pesse <samypesse@gmail.com> | 2016-10-13 20:55:41 +0200 |
---|---|---|
committer | Samy Pesse <samypesse@gmail.com> | 2016-10-13 20:55:41 +0200 |
commit | d56019dc18b98a32d7ec6584a9a6d31670864d61 (patch) | |
tree | 5dd1c80f0ba5309e0c4d0adf0bdc8d923db51a3a | |
parent | 0a6216a093b11f402f9ffa6885a03df7006cb557 (diff) | |
download | gitbook-d56019dc18b98a32d7ec6584a9a6d31670864d61.zip gitbook-d56019dc18b98a32d7ec6584a9a6d31670864d61.tar.gz gitbook-d56019dc18b98a32d7ec6584a9a6d31670864d61.tar.bz2 |
Hook "page" should not be able to edit content but attributes
-rw-r--r-- | CHANGES.md | 2 | ||||
-rw-r--r-- | packages/gitbook/src/api/decodePage.js | 34 | ||||
-rw-r--r-- | packages/gitbook/src/api/encodeGlobal.js | 16 | ||||
-rw-r--r-- | packages/gitbook/src/json/encodePage.js | 9 |
4 files changed, 35 insertions, 26 deletions
@@ -10,6 +10,8 @@ This project adheres to [Semantic Versioning](http://semver.org/). - *Website Feature:* new default plugin `copy-code` to copy code blocks content in one click - *Website Feature:* new default plugin `heading-anchors` to have clickable headings - *Website Feature:* `fontsettings` is now only controlling the font size, and can work on multiple themes +- *Plugin API*: hook `page` can no longer modify the content, only the page's attributes +- *Plugin API*: plugins can no longer export resources, instead use the `_assets` folder ## 3.2.0 - Switch markdown parser from `kramed` to `markup-it` diff --git a/packages/gitbook/src/api/decodePage.js b/packages/gitbook/src/api/decodePage.js index 4fd78af..395c8ff 100644 --- a/packages/gitbook/src/api/decodePage.js +++ b/packages/gitbook/src/api/decodePage.js @@ -1,14 +1,14 @@ -const deprecate = require('./deprecate'); +const Immutable = require('immutable'); /** - Decode changes from a JS API to a page object. - Only the content can be edited by plugin's hooks. - - @param {Output} output - @param {Page} page: page instance to edit - @param {Object} result: result from API - @return {Page} -*/ + * Decode changes from a JS API to a page object. + * Only the content can be edited by plugin's hooks. + * + * @param {Output} output + * @param {Page} page: page instance to edit + * @param {Object} result: result from API + * @return {Page} + */ function decodePage(output, page, result) { const originalContent = page.getContent(); @@ -18,7 +18,9 @@ function decodePage(output, page, result) { return page; } - deprecate.disable('page.sections'); + // Update page attributes + const newAttributes = Immutable.fromJS(page.attributes); + page = page.set('attributes', newAttributes); // GitBook 3 // Use returned page.content if different from original content @@ -26,18 +28,6 @@ function decodePage(output, page, result) { page = page.set('content', result.content); } - // GitBook 2 compatibility - // Finally, use page.sections - else if (result.sections) { - page = page.set('content', - result.sections.map(function(section) { - return section.content; - }).join('\n') - ); - } - - deprecate.enable('page.sections'); - return page; } diff --git a/packages/gitbook/src/api/encodeGlobal.js b/packages/gitbook/src/api/encodeGlobal.js index 18ebdf0..89db629 100644 --- a/packages/gitbook/src/api/encodeGlobal.js +++ b/packages/gitbook/src/api/encodeGlobal.js @@ -36,6 +36,22 @@ function encodeGlobal(output) { summary: encodeSummary(output, book.getSummary()), /** + * Return absolute path to the root folder of the book + * @return {String} + */ + root() { + return book.getRoot(); + }, + + /** + * Return absolute path to the root folder of the book (for content) + * @return {String} + */ + contentRoot() { + return book.getContentRoot(); + }, + + /** * Check if the book is a multilingual book. * @return {Boolean} */ diff --git a/packages/gitbook/src/json/encodePage.js b/packages/gitbook/src/json/encodePage.js index 6610308..0671721 100644 --- a/packages/gitbook/src/json/encodePage.js +++ b/packages/gitbook/src/json/encodePage.js @@ -13,7 +13,11 @@ function encodePage(page, summary, urls) { const attributes = page.getAttributes(); const article = summary.getByPath(file.getPath()); - const result = attributes.toJS(); + const result = { + content: page.getContent(), + dir: page.getDir(), + attributes: attributes.toJS() + }; if (article) { result.title = article.getTitle(); @@ -31,9 +35,6 @@ function encodePage(page, summary, urls) { } } - result.content = page.getContent(); - result.dir = page.getDir(); - return result; } |