summaryrefslogtreecommitdiffstats
path: root/packages/gitbook-plugin-highlight/src
diff options
context:
space:
mode:
authorSamy Pesse <samypesse@gmail.com>2016-10-05 01:47:16 +0200
committerSamy Pesse <samypesse@gmail.com>2016-10-05 01:47:16 +0200
commit45752fc79c9e3a5b7e84ed8572a8f0c12d8176b1 (patch)
tree2ecfc9d8bdff9c44dbb9e0682f070000dc194dba /packages/gitbook-plugin-highlight/src
parenta863b55c798b4dc0a177fe1ada9cd428f1a5444d (diff)
downloadgitbook-45752fc79c9e3a5b7e84ed8572a8f0c12d8176b1.zip
gitbook-45752fc79c9e3a5b7e84ed8572a8f0c12d8176b1.tar.gz
gitbook-45752fc79c9e3a5b7e84ed8572a8f0c12d8176b1.tar.bz2
Complete highlight plugin
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.js46
-rw-r--r--packages/gitbook-plugin-highlight/src/getLanguage.js10
3 files changed, 60 insertions, 6 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
index f55524f..89f6793 100644
--- a/packages/gitbook-plugin-highlight/src/CodeBlock.js
+++ b/packages/gitbook-plugin-highlight/src/CodeBlock.js
@@ -1,14 +1,54 @@
+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.children
+ children: React.PropTypes.node,
+ className: React.PropTypes.string
},
render() {
- const { children } = this.props;
- return <span />;
+ 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>
+ );
+ }
}
});
diff --git a/packages/gitbook-plugin-highlight/src/getLanguage.js b/packages/gitbook-plugin-highlight/src/getLanguage.js
index 47b68cf..7a1bf8e 100644
--- a/packages/gitbook-plugin-highlight/src/getLanguage.js
+++ b/packages/gitbook-plugin-highlight/src/getLanguage.js
@@ -1,14 +1,16 @@
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 {Array<String>}
+ * @param {String} className
* @return {String}
*/
-function getLanguage(classNames) {
- return List(classNames)
+function getLanguage(className) {
+ const lang = List(className.split(' '))
.map(function(cl) {
// Markdown
if (cl.search('lang-') === 0) {
@@ -25,6 +27,8 @@ function getLanguage(classNames) {
.find(function(cl) {
return Boolean(cl);
});
+
+ return ALIASES[lang] || lang;
}
module.exports = getLanguage;