diff options
author | Samy Pesse <samypesse@gmail.com> | 2016-10-04 18:53:58 +0200 |
---|---|---|
committer | Samy Pesse <samypesse@gmail.com> | 2016-10-04 18:53:58 +0200 |
commit | a863b55c798b4dc0a177fe1ada9cd428f1a5444d (patch) | |
tree | 0a3a41543bfc31527d4f4c19f5473df7afbff0ea /packages | |
parent | 16ca48b2c05dad0af4cec600cee69adcafcf255f (diff) | |
download | gitbook-a863b55c798b4dc0a177fe1ada9cd428f1a5444d.zip gitbook-a863b55c798b4dc0a177fe1ada9cd428f1a5444d.tar.gz gitbook-a863b55c798b4dc0a177fe1ada9cd428f1a5444d.tar.bz2 |
Start new highlight.js plugin
Diffstat (limited to 'packages')
-rw-r--r-- | packages/gitbook-plugin-highlight/.gitignore | 31 | ||||
-rw-r--r-- | packages/gitbook-plugin-highlight/.npmignore | 2 | ||||
-rw-r--r-- | packages/gitbook-plugin-highlight/index.js | 10 | ||||
-rw-r--r-- | packages/gitbook-plugin-highlight/package.json | 28 | ||||
-rw-r--r-- | packages/gitbook-plugin-highlight/src/CodeBlock.js | 15 | ||||
-rw-r--r-- | packages/gitbook-plugin-highlight/src/getLanguage.js | 30 | ||||
-rw-r--r-- | packages/gitbook-plugin-highlight/src/index.js | 9 | ||||
-rw-r--r-- | packages/gitbook/src/output/getModifiers.js | 32 | ||||
-rw-r--r-- | packages/gitbook/src/output/modifiers/__tests__/highlightCode.js | 59 | ||||
-rw-r--r-- | packages/gitbook/src/output/modifiers/highlightCode.js | 58 | ||||
-rw-r--r-- | packages/gitbook/src/output/modifiers/index.js | 3 |
11 files changed, 127 insertions, 150 deletions
diff --git a/packages/gitbook-plugin-highlight/.gitignore b/packages/gitbook-plugin-highlight/.gitignore new file mode 100644 index 0000000..ef47881 --- /dev/null +++ b/packages/gitbook-plugin-highlight/.gitignore @@ -0,0 +1,31 @@ +# Logs +logs +*.log + +# Runtime data +pids +*.pid +*.seed + +# Directory for instrumented libs generated by jscoverage/JSCover +lib-cov + +# Coverage directory used by tools like istanbul +coverage + +# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) +.grunt + +# Compiled binary addons (http://nodejs.org/api/addons.html) +build/Release + +# Dependency directory +# Deployed apps should consider commenting this line out: +# see https://npmjs.org/doc/faq.html#Should-I-check-my-node_modules-folder-into-git +node_modules + +# vim swapfile +*.swp + +# Plugin assets +_assets/plugin.js diff --git a/packages/gitbook-plugin-highlight/.npmignore b/packages/gitbook-plugin-highlight/.npmignore new file mode 100644 index 0000000..a0e53cf --- /dev/null +++ b/packages/gitbook-plugin-highlight/.npmignore @@ -0,0 +1,2 @@ +# Publish assets on NPM +!_assets/plugin.js diff --git a/packages/gitbook-plugin-highlight/index.js b/packages/gitbook-plugin-highlight/index.js new file mode 100644 index 0000000..e542ae8 --- /dev/null +++ b/packages/gitbook-plugin-highlight/index.js @@ -0,0 +1,10 @@ + +module.exports = { + blocks: { + + }, + + hooks: { + + } +}; diff --git a/packages/gitbook-plugin-highlight/package.json b/packages/gitbook-plugin-highlight/package.json new file mode 100644 index 0000000..c366194 --- /dev/null +++ b/packages/gitbook-plugin-highlight/package.json @@ -0,0 +1,28 @@ +{ + "name": "gitbook-plugin-highlight", + "description": "Syntax highlighter for Gitbook", + "main": "index.js", + "browser": "./_assets/plugin.js", + "version": "2.0.2", + "dependencies": { + "gitbook-core": "^0.0.0" + }, + "devDependencies": { + "gitbook-plugin": "*" + }, + "engines": { + "gitbook": ">=3.0.0" + }, + "scripts": { + "build-js": "gitbook-plugin build ./src/index.js ./_assets/plugin.js", + "prepublish": "npm run build-js" + }, + "homepage": "https://github.com/GitbookIO/gitbook", + "repository": { + "type": "git", + "url": "https://github.com/GitbookIO/gitbook.git" + }, + "bugs": { + "url": "https://github.com/GitbookIO/gitbook/issues" + } +} diff --git a/packages/gitbook-plugin-highlight/src/CodeBlock.js b/packages/gitbook-plugin-highlight/src/CodeBlock.js new file mode 100644 index 0000000..f55524f --- /dev/null +++ b/packages/gitbook-plugin-highlight/src/CodeBlock.js @@ -0,0 +1,15 @@ +const GitBook = require('gitbook-core'); +const { React } = GitBook; + +const CodeBlock = React.createClass({ + propTypes: { + children: React.PropTypes.children + }, + + render() { + const { children } = this.props; + return <span />; + } +}); + +module.exports = CodeBlock; diff --git a/packages/gitbook-plugin-highlight/src/getLanguage.js b/packages/gitbook-plugin-highlight/src/getLanguage.js new file mode 100644 index 0000000..47b68cf --- /dev/null +++ b/packages/gitbook-plugin-highlight/src/getLanguage.js @@ -0,0 +1,30 @@ +const GitBook = require('gitbook-core'); +const { List } = GitBook.Immutable; + +/** + * Return language for a code blocks from a list of class names + * + * @param {Array<String>} + * @return {String} + */ +function getLanguage(classNames) { + return List(classNames) + .map(function(cl) { + // Markdown + if (cl.search('lang-') === 0) { + return cl.slice('lang-'.length); + } + + // Asciidoc + if (cl.search('language-') === 0) { + return cl.slice('language-'.length); + } + + return null; + }) + .find(function(cl) { + return Boolean(cl); + }); +} + +module.exports = getLanguage; diff --git a/packages/gitbook-plugin-highlight/src/index.js b/packages/gitbook-plugin-highlight/src/index.js new file mode 100644 index 0000000..3f17c42 --- /dev/null +++ b/packages/gitbook-plugin-highlight/src/index.js @@ -0,0 +1,9 @@ +const GitBook = require('gitbook-core'); +const CodeBlock = require('./CodeBlock'); + +module.exports = GitBook.createPlugin({ + activate: (dispatch, getState, { Components }) => { + dispatch(Components.registerComponent(CodeBlock, { role: 'html:code' })); + }, + reduce: (state, action) => state +}); diff --git a/packages/gitbook/src/output/getModifiers.js b/packages/gitbook/src/output/getModifiers.js index df32fcb..c2ac71f 100644 --- a/packages/gitbook/src/output/getModifiers.js +++ b/packages/gitbook/src/output/getModifiers.js @@ -1,12 +1,7 @@ const Modifiers = require('./modifiers'); const resolveFileToURL = require('./helper/resolveFileToURL'); -const Api = require('../api'); -const Plugins = require('../plugins'); -const Promise = require('../utils/promise'); -const defaultBlocks = require('../constants/defaultBlocks'); const fileToOutput = require('./helper/fileToOutput'); -const CODEBLOCK = 'code'; /** * Return default modifier to prepare a page for @@ -16,7 +11,6 @@ const CODEBLOCK = 'code'; */ function getModifiers(output, page) { const book = output.getBook(); - const plugins = output.getPlugins(); const glossary = book.getGlossary(); const file = page.getFile(); @@ -28,13 +22,6 @@ function getModifiers(output, page) { // Current file path const currentFilePath = file.getPath(); - // Get TemplateBlock for highlighting - const blocks = Plugins.listBlocks(plugins); - const code = blocks.get(CODEBLOCK) || defaultBlocks.get(CODEBLOCK); - - // Current context - const context = Api.encodeGlobal(output); - return [ // Normalize IDs on headings Modifiers.addHeadingId, @@ -49,24 +36,7 @@ function getModifiers(output, page) { Modifiers.resolveLinks.bind(null, currentFilePath, resolveFileToURL.bind(null, output) - ), - - // Highlight code blocks using "code" block - Modifiers.highlightCode.bind(null, function(lang, source) { - return Promise(code.applyBlock({ - body: source, - kwargs: { - language: lang - } - }, context)) - .then(function(result) { - if (result.html === false) { - return { text: result.body }; - } else { - return { html: result.body }; - } - }); - }) + ) ]; } diff --git a/packages/gitbook/src/output/modifiers/__tests__/highlightCode.js b/packages/gitbook/src/output/modifiers/__tests__/highlightCode.js deleted file mode 100644 index d93417b..0000000 --- a/packages/gitbook/src/output/modifiers/__tests__/highlightCode.js +++ /dev/null @@ -1,59 +0,0 @@ -const cheerio = require('cheerio'); -const Promise = require('../../../utils/promise'); -const highlightCode = require('../highlightCode'); - -describe('highlightCode', function() { - function doHighlight(lang, code) { - return { - text: '' + (lang || '') + '$' + code - }; - } - - function doHighlightAsync(lang, code) { - return Promise() - .then(function() { - return doHighlight(lang, code); - }); - } - - it('should call it for normal code element', function() { - const $ = cheerio.load('<p>This is a <code>test</code></p>'); - - return highlightCode(doHighlight, $) - .then(function() { - const $code = $('code'); - expect($code.text()).toBe('$test'); - }); - }); - - it('should call it for markdown code block', function() { - const $ = cheerio.load('<pre><code class="lang-js">test</code></pre>'); - - return highlightCode(doHighlight, $) - .then(function() { - const $code = $('code'); - expect($code.text()).toBe('js$test'); - }); - }); - - it('should call it for asciidoc code block', function() { - const $ = cheerio.load('<pre><code class="language-python">test</code></pre>'); - - return highlightCode(doHighlight, $) - .then(function() { - const $code = $('code'); - expect($code.text()).toBe('python$test'); - }); - }); - - it('should accept async highlighter', function() { - const $ = cheerio.load('<pre><code class="language-python">test</code></pre>'); - - return highlightCode(doHighlightAsync, $) - .then(function() { - const $code = $('code'); - expect($code.text()).toBe('python$test'); - }); - }); -}); - diff --git a/packages/gitbook/src/output/modifiers/highlightCode.js b/packages/gitbook/src/output/modifiers/highlightCode.js deleted file mode 100644 index 622432d..0000000 --- a/packages/gitbook/src/output/modifiers/highlightCode.js +++ /dev/null @@ -1,58 +0,0 @@ -const is = require('is'); -const Immutable = require('immutable'); - -const Promise = require('../../utils/promise'); -const editHTMLElement = require('./editHTMLElement'); - -/** - Return language for a code blocks from a list of class names - - @param {Array<String>} - @return {String} -*/ -function getLanguageForClass(classNames) { - return Immutable.List(classNames) - .map(function(cl) { - // Markdown - if (cl.search('lang-') === 0) { - return cl.slice('lang-'.length); - } - - // Asciidoc - if (cl.search('language-') === 0) { - return cl.slice('language-'.length); - } - - return null; - }) - .find(function(cl) { - return Boolean(cl); - }); -} - - -/** - Highlight all code elements - - @param {Function(lang, body) -> String} highlight - @param {HTMLDom} $ - @return {Promise} -*/ -function highlightCode(highlight, $) { - return editHTMLElement($, 'code', function($code) { - const classNames = ($code.attr('class') || '').split(' '); - const lang = getLanguageForClass(classNames); - const source = $code.text(); - - return Promise(highlight(lang, source)) - .then(function(r) { - if (is.string(r.html)) { - $code.html(r.html); - } else { - $code.text(r.text); - } - }); - }); -} - -module.exports = highlightCode; diff --git a/packages/gitbook/src/output/modifiers/index.js b/packages/gitbook/src/output/modifiers/index.js index f1daa2b..5f290f6 100644 --- a/packages/gitbook/src/output/modifiers/index.js +++ b/packages/gitbook/src/output/modifiers/index.js @@ -10,6 +10,5 @@ module.exports = { svgToPng: require('./svgToPng'), resolveLinks: require('./resolveLinks'), resolveImages: require('./resolveImages'), - annotateText: require('./annotateText'), - highlightCode: require('./highlightCode') + annotateText: require('./annotateText') }; |