diff options
Diffstat (limited to 'packages/gitbook-plugin-headings')
-rw-r--r-- | packages/gitbook-plugin-headings/.gitignore | 31 | ||||
-rw-r--r-- | packages/gitbook-plugin-headings/.npmignore | 2 | ||||
-rw-r--r-- | packages/gitbook-plugin-headings/_assets/website/headings.css | 41 | ||||
-rw-r--r-- | packages/gitbook-plugin-headings/index.js | 10 | ||||
-rw-r--r-- | packages/gitbook-plugin-headings/package.json | 38 | ||||
-rw-r--r-- | packages/gitbook-plugin-headings/src/index.js | 60 |
6 files changed, 182 insertions, 0 deletions
diff --git a/packages/gitbook-plugin-headings/.gitignore b/packages/gitbook-plugin-headings/.gitignore new file mode 100644 index 0000000..ef47881 --- /dev/null +++ b/packages/gitbook-plugin-headings/.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-headings/.npmignore b/packages/gitbook-plugin-headings/.npmignore new file mode 100644 index 0000000..a0e53cf --- /dev/null +++ b/packages/gitbook-plugin-headings/.npmignore @@ -0,0 +1,2 @@ +# Publish assets on NPM +!_assets/plugin.js diff --git a/packages/gitbook-plugin-headings/_assets/website/headings.css b/packages/gitbook-plugin-headings/_assets/website/headings.css new file mode 100644 index 0000000..1ef0d64 --- /dev/null +++ b/packages/gitbook-plugin-headings/_assets/website/headings.css @@ -0,0 +1,41 @@ + +.Headings-Container { + position: relative; + margin-left: -30px; + padding-left: 30px; +} + +/* Left anchors rules */ +.Headings-Container > .Headings-Anchor-Left { + position: absolute; + left: 5px; + top: 50%; + transform: translateY(-50%); + opacity: 0; + color: inherit; +} + +/* Right anchors rules */ +.Headings-Container > .Headings-Anchor-Right { + padding-left: 5px; + opacity: 0; + color: inherit; +} + +.Headings-Container.Headings-Right > h1, +.Headings-Container.Headings-Right > h2, +.Headings-Container.Headings-Right > h3, +.Headings-Container.Headings-Right > h4, +.Headings-Container.Headings-Right > h5, +.Headings-Container.Headings-Right > h6 { + display: inline-block; + margin-right: 5px; +} + +/* Display on hover */ +.Headings-Container:hover > .Headings-Anchor-Left, +.Headings-Container > .Headings-Anchor-Left:focus, +.Headings-Container:hover > .Headings-Anchor-Right, +.Headings-Container > .Headings-Anchor-Right:focus { + opacity: 1; +} diff --git a/packages/gitbook-plugin-headings/index.js b/packages/gitbook-plugin-headings/index.js new file mode 100644 index 0000000..e542ae8 --- /dev/null +++ b/packages/gitbook-plugin-headings/index.js @@ -0,0 +1,10 @@ + +module.exports = { + blocks: { + + }, + + hooks: { + + } +}; diff --git a/packages/gitbook-plugin-headings/package.json b/packages/gitbook-plugin-headings/package.json new file mode 100644 index 0000000..55a06f6 --- /dev/null +++ b/packages/gitbook-plugin-headings/package.json @@ -0,0 +1,38 @@ +{ + "name": "gitbook-plugin-headings", + "description": "Automatically add anchors to headings", + "main": "index.js", + "browser": "./_assets/plugin.js", + "version": "4.0.0", + "dependencies": { + "classnames": "^2.2.5", + "gitbook-core": "4.0.0" + }, + "devDependencies": { + "gitbook-plugin": "4.0.0" + }, + "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" + }, + "gitbook": { + "properties": { + "position": { + "type": "string", + "title": "Position of anchors", + "default": "left" + } + } + } +} diff --git a/packages/gitbook-plugin-headings/src/index.js b/packages/gitbook-plugin-headings/src/index.js new file mode 100644 index 0000000..b023e2e --- /dev/null +++ b/packages/gitbook-plugin-headings/src/index.js @@ -0,0 +1,60 @@ +const GitBook = require('gitbook-core'); +const { React } = GitBook; +const classNames = require('classnames'); + +function mapStateToProps({ config }) { + return { + position: config.getIn(['pluginsConfig', 'headings', 'position'], 'left') + }; +} + +let Heading = React.createClass({ + propTypes: { + id: React.PropTypes.string.isRequired, + children: React.PropTypes.node.isRequired, + position: React.PropTypes.string.isRequired + }, + + render() { + const { position, children, id } = this.props; + const className = classNames('Headings-Container', { + 'Headings-Right': (position !== 'left') + }); + + return ( + <div className={className}> + <GitBook.ImportCSS href="gitbook/headings/headings.css" /> + + {position == 'left' ? + <a className="Headings-Anchor-Left" href={`#${id}`}> + <i className="fa fa-link" /> + </a> + : null} + + {children} + + {position != 'left' ? + <a className="Headings-Anchor-Right" href={`#${id}`}> + <i className="fa fa-link" /> + </a> + : null} + </div> + ); + } +}); + +Heading = GitBook.connect(Heading, mapStateToProps); + +module.exports = GitBook.createPlugin({ + activate: (dispatch, getState, { Components }) => { + // Attach component to titles + dispatch(Components.registerComponent(Heading, { role: 'html:h1' })); + dispatch(Components.registerComponent(Heading, { role: 'html:h2' })); + dispatch(Components.registerComponent(Heading, { role: 'html:h3' })); + dispatch(Components.registerComponent(Heading, { role: 'html:h4' })); + dispatch(Components.registerComponent(Heading, { role: 'html:h5' })); + dispatch(Components.registerComponent(Heading, { role: 'html:h6' })); + }, + deactivate: (dispatch, getState) => {}, + reduce: (state, action) => state +}); |