summaryrefslogtreecommitdiffstats
path: root/packages/gitbook-plugin-highlight/src
diff options
context:
space:
mode:
Diffstat (limited to 'packages/gitbook-plugin-highlight/src')
-rw-r--r--packages/gitbook-plugin-highlight/src/ALIASES.js10
-rw-r--r--packages/gitbook-plugin-highlight/src/CodeBlock.js55
-rw-r--r--packages/gitbook-plugin-highlight/src/getLanguage.js34
-rw-r--r--packages/gitbook-plugin-highlight/src/index.js9
4 files changed, 108 insertions, 0 deletions
diff --git a/packages/gitbook-plugin-highlight/src/ALIASES.js b/packages/gitbook-plugin-highlight/src/ALIASES.js
new file mode 100644
index 0000000..799efef
--- /dev/null
+++ b/packages/gitbook-plugin-highlight/src/ALIASES.js
@@ -0,0 +1,10 @@
+
+const ALIASES = {
+ 'py': 'python',
+ 'js': 'javascript',
+ 'json': 'javascript',
+ 'rb': 'ruby',
+ 'csharp': 'cs'
+};
+
+module.exports = ALIASES;
diff --git a/packages/gitbook-plugin-highlight/src/CodeBlock.js b/packages/gitbook-plugin-highlight/src/CodeBlock.js
new file mode 100644
index 0000000..a556d36
--- /dev/null
+++ b/packages/gitbook-plugin-highlight/src/CodeBlock.js
@@ -0,0 +1,55 @@
+const hljs = require('highlight.js');
+const GitBook = require('gitbook-core');
+const { React } = GitBook;
+
+const getLanguage = require('./getLanguage');
+
+/**
+ * Get children as text
+ * @param {React.Children} children
+ * @return {String}
+ */
+function getChildrenToText(children) {
+ return React.Children.map(children, child => {
+ if (typeof child === 'string') {
+ return child;
+ } else {
+ return child.props.children ?
+ getChildrenToText(child.props.children) : '';
+ }
+ }).join('');
+}
+
+const CodeBlock = React.createClass({
+ propTypes: {
+ children: React.PropTypes.node,
+ className: React.PropTypes.string
+ },
+
+ render() {
+ const { children, className } = this.props;
+ const content = getChildrenToText(children);
+ const lang = getLanguage(className || '');
+
+ const includeCSS = <GitBook.ImportCSS href="gitbook/highlight/white.css" />;
+
+ try {
+ const html = hljs.highlight(lang, content).value;
+ return (
+ <code>
+ {includeCSS}
+ <span dangerouslySetInnerHTML={{__html: html}} />
+ </code>
+ );
+ } catch (e) {
+ return (
+ <code>
+ {includeCSS}
+ {content}
+ </code>
+ );
+ }
+ }
+});
+
+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..7a1bf8e
--- /dev/null
+++ b/packages/gitbook-plugin-highlight/src/getLanguage.js
@@ -0,0 +1,34 @@
+const GitBook = require('gitbook-core');
+const { List } = GitBook.Immutable;
+
+const ALIASES = require('./ALIASES');
+
+/**
+ * Return language for a code blocks from a list of class names
+ *
+ * @param {String} className
+ * @return {String}
+ */
+function getLanguage(className) {
+ const lang = List(className.split(' '))
+ .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);
+ });
+
+ return ALIASES[lang] || lang;
+}
+
+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
+});