├── .gitignore ├── .travis.yml ├── LICENSE ├── README.md ├── assets └── embed-codepen.js ├── index.js └── package.json /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | 5 | # Runtime data 6 | pids 7 | *.pid 8 | *.seed 9 | 10 | # Directory for instrumented libs generated by jscoverage/JSCover 11 | lib-cov 12 | 13 | # Coverage directory used by tools like istanbul 14 | coverage 15 | 16 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 17 | .grunt 18 | 19 | # node-waf configuration 20 | .lock-wscript 21 | 22 | # Compiled binary addons (http://nodejs.org/api/addons.html) 23 | build/Release 24 | 25 | # Dependency directory 26 | # https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git 27 | node_modules 28 | .DS_Store 29 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - "stable" 4 | - "0.12" 5 | - "4.0.0" 6 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Lingyu Wang 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # gitbook-plugin-codepen 2 | 3 | [![Build Status](https://travis-ci.org/LingyuCoder/gitbook-plugin-codepen.png)](https://travis-ci.org/LingyuCoder/gitbook-plugin-codepen) 4 | [![Dependency Status](https://david-dm.org/LingyuCoder/gitbook-plugin-codepen.svg)](https://david-dm.org/LingyuCoder/gitbook-plugin-codepen) 5 | [![devDependency Status](https://david-dm.org/LingyuCoder/gitbook-plugin-codepen/dev-status.svg)](https://david-dm.org/LingyuCoder/gitbook-plugin-codepen#info=devDependencies) 6 | [![NPM version](http://img.shields.io/npm/v/gitbook-plugin-codepen.svg?style=flat-square)](http://npmjs.org/package/gitbook-plugin-codepen) 7 | [![node](https://img.shields.io/badge/node.js-%3E=_0.12-green.svg?style=flat-square)](http://nodejs.org/download/) 8 | [![License](http://img.shields.io/npm/l/gitbook-plugin-codepen.svg?style=flat-square)](LICENSE) 9 | [![npm download](https://img.shields.io/npm/dm/gitbook-plugin-codepen.svg?style=flat-square)](https://npmjs.org/package/gitbook-plugin-codepen) 10 | 11 | 12 | **Embed Codepen preview into your Gitbook** 13 | 14 | [DEMO](http://read.lingyu.wang/Codepen.html) 15 | 16 | ## Install 17 | 18 | ```shell 19 | $npm install --save gitbook-plugin-codepen 20 | ``` 21 | 22 | ## Usage 23 | 24 | Add the plugin to your `book.json` like this: 25 | 26 | ```javascript 27 | { 28 | "plugins": ["codepen"] 29 | } 30 | ``` 31 | 32 | then in your markdown file, you can embed codepen panel like this: 33 | 34 | ```markdown 35 | [](codepen://Lingyucoder/AsFJh?height=800&theme=0) 36 | ``` 37 | 38 | ## Options 39 | 40 | You can set the options of your Gitbook in `book.json` like this: 41 | 42 | ```javascript 43 | "pluginsConfig": { 44 | "codepen": { 45 | "height": 400, 46 | "theme": 14038, 47 | "description": "somepen", 48 | "defaultTab": 'html' 49 | } 50 | } 51 | ``` 52 | 53 | You can also set options of a specific Codepen panel use URL querystring like `?height=400&theme=14038&description=somepen&defaultTab=html` 54 | 55 | ### height 56 | 57 | default: `300` 58 | 59 | ### theme 60 | 61 | default: `0` 62 | 63 | ### description 64 | 65 | default: `''` 66 | 67 | ### defaultTab 68 | 69 | default: `'result'` 70 | 71 | ##License 72 | 73 | The MIT License (MIT) 74 | 75 | Copyright (c) 2015 Lingyu Wang 76 | 77 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 78 | 79 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 80 | 81 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 82 | -------------------------------------------------------------------------------- /assets/embed-codepen.js: -------------------------------------------------------------------------------- 1 | require(["gitbook", "jQuery"], function(gitbook, $) { 2 | 3 | var root = (document.getElementsByTagName('head')[0] || 4 | document.getElementsByTagName('body')[0]); 5 | 6 | var initEmbed = function() { 7 | window.__cp_embed_script_ran = false; 8 | window.CodePenEmbed.init(); 9 | }; 10 | 11 | var loadEmbedScript = function() { 12 | var script = document.createElement('script'); 13 | script.onload = initEmbed; 14 | script.type = 'text/javascript'; 15 | script.async = true; 16 | script.charset = 'UTF-8'; 17 | script.src = document.location.protocol + 18 | '//assets.codepen.io/assets/embed/ei.js'; 19 | root.appendChild(script); 20 | }; 21 | 22 | gitbook.events.bind("page.change", function(e) { 23 | if (!window.CodePenEmbed) loadEmbedScript(); 24 | else { 25 | window.__cp_embed_script_ran = false; 26 | window.CodePenEmbed.init(); 27 | } 28 | }); 29 | }); 30 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | var cheerio = require('cheerio'); 3 | var _ = require('underscore'); 4 | var parse = require('url-parse'); 5 | var defaultConfig = { 6 | height: 300, 7 | theme: 0, 8 | defaultTab: 'result', 9 | description: '' 10 | }; 11 | 12 | var template = _.template( 13 | '

See the Pen <%= description %> by <%= user %> (@<%= user %>) on CodePen.

' 14 | ); 15 | 16 | module.exports = { 17 | book: { 18 | assets: './assets', 19 | js: [ 20 | 'embed-codepen.js' 21 | ] 22 | }, 23 | hooks: { 24 | page: function(page) { 25 | var config = _.defaults(this.options.pluginsConfig 26 | .codepen, defaultConfig); 27 | _.each(page.sections, function(section) { 28 | var $ = cheerio.load(section.content); 29 | $('a').filter(function() { 30 | var href = $(this).attr('href'); 31 | return parse(href).protocol === 32 | 'codepen:'; 33 | }).each(function(index, a) { 34 | a = $(a); 35 | var rst = parse(a.attr('href'), true); 36 | a.replaceWith(template(_.extend({}, 37 | config, rst.query, { 38 | user: rst.host, 39 | pen: rst.pathname.slice(1) 40 | }))); 41 | }); 42 | section.content = $.html(); 43 | }); 44 | return page; 45 | } 46 | } 47 | }; 48 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "gitbook-plugin-codepen", 3 | "version": "0.1.2", 4 | "description": "A plugin to embed codepen in your Gitbook", 5 | "main": "index.js", 6 | "keywords": [ 7 | "gitbook", 8 | "plugin", 9 | "codepen" 10 | ], 11 | "engines": { 12 | "gitbook": "*" 13 | }, 14 | "author": "LingyuCoder", 15 | "license": "MIT", 16 | "dependencies": { 17 | "cheerio": "^0.19.0", 18 | "underscore": "^1.8.3", 19 | "url-parse": "^1.0.0" 20 | }, 21 | "devDependencies": {}, 22 | "scripts": { 23 | "test": "echo \"Error: no test specified\" && exit 0" 24 | }, 25 | "repository": { 26 | "type": "git", 27 | "url": "https://github.com/LingyuCoder/gitbook-plugin-codepen.git" 28 | }, 29 | "bugs": { 30 | "url": "https://github.com/LingyuCoder/gitbook-plugin-codepen/issues" 31 | }, 32 | "homepage": "https://github.com/LingyuCoder/gitbook-plugin-codepen", 33 | "_shasum": "124a99efb53a783bc32f05e09a761be0853e1b55", 34 | "_from": "gitbook-plugin-codepen@*" 35 | } 36 | --------------------------------------------------------------------------------