Repository: bubkoo/hexo-toc Branch: master Commit: ee914c61fd69 Files: 10 Total size: 8.4 KB Directory structure: gitextract_l8wzuhw_/ ├── .editorconfig ├── .gitignore ├── .jshintrc ├── .npmignore ├── LICENSE ├── README.md ├── index.js ├── lib/ │ ├── filter.js │ └── slugify.js └── package.json ================================================ FILE CONTENTS ================================================ ================================================ FILE: .editorconfig ================================================ # EditorConfig is awesome: http://EditorConfig.org # top-most EditorConfig file root = true # Unix-style newlines with a newline ending every file [*] end_of_line = lf charset = utf-8 insert_final_newline = true trim_trailing_whitespace = true # Set default charset [*.js] indent_style = space indent_size = 2 [Makefile] indent_style = tab indent_size = 4 [*.md] trim_trailing_whitespace = false ================================================ FILE: .gitignore ================================================ .idea .DS_Store node_modules npm-debug.log tmp ================================================ FILE: .jshintrc ================================================ { "curly": true, "eqeqeq": true, "immed": true, "newcap": true, "noarg": true, "sub": true, "undef": true, "boss": true, "eqnull": true, "node": true, "latedef": "nofunc" } ================================================ FILE: .npmignore ================================================ .idea .DS_Store .editorconfig .npmignore .gitignore .jshintrc .jshintignore Gruntfile.js npm-debug.log test mock ================================================ FILE: LICENSE ================================================ The MIT License (MIT) Copyright (c) 2015 bubkoo 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: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 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. ================================================ FILE: README.md ================================================ # hexo-toc [![MIT License](https://img.shields.io/badge/license-MIT_License-green.svg?style=flat-square)](https://github.com/bubkoo/hexo-toc/blob/master/LICENSE) [![npm:](https://img.shields.io/npm/v/hexo-toc.svg?style=flat-square)](https://www.npmjs.com/packages/hexo-toc) [![Package Quality](http://npm.packagequality.com/shield/hexo-toc.svg)](http://packagequality.com/#?package=hexo-toc) > Insert a markdown TOC(Table Of Content) before posts be rendered. Unlike the native [`toc`](http://hexo.io/docs/helpers.html#toc) helper, this plugin will inject a TOC only when a placeholder(``) found in the raw markdown files. And the TOC will be injected after the placeholder. All you need to do is placing a placeholder(``) in your post when and where needed. **Note:** this plugin will not mangle your posts(markdown files), so you can use it bold. ## Install ```node npm install hexo-toc --save ``` ## Options All the options of [markdown-toc](https://github.com/jonschlinkert/markdown-toc), slugify function, and heading anchor options can be specified as follow in your `_config.yml`: ```yaml toc: maxdepth: 3 class: toc slugify: transliteration decodeEntities: false anchor: position: after symbol: '#' style: header-anchor ``` - `maxdepth`: Use headings whose depth is at most maxdepth. - `class`: The CSS Class for the toc. (*Default is `false`*) - `slugify`: Choose which slugify function you want to use. Currently support [uslug](https://github.com/jeremys/uslug) (*Default*) and [transliteration](https://github.com/andyhu/node-transliteration). - `decodeEntities`: Select whether to enable decode entities. ( *Default is `false`* and please see [#15](https://github.com/bubkoo/hexo-toc/pull/15)). - `anchor`: Whether should have an anchor for each headings. (*Default is `false`*) - `position`: Where should the anchor be, `before` the title, or `after` the title. (*Default is `after`*); - `symbol`: Which symbol you want the anchor be. (*Default is `#`*); - `style`: The CSS class for the anchor, (*Default is `header-anchor`*); ## Known issues ### [#8](https://github.com/bubkoo/hexo-toc/issues/8) Working with [hexo-renderer-markdown-it](https://github.com/celsomiranda/hexo-renderer-markdown-it). ```yaml # Markdown-it config ## Docs: https://github.com/celsomiranda/hexo-renderer-markdown-it/wiki markdown: render: html: true ``` ## Related - [hexo-filter-fenced-code](https://github.com/bubkoo/hexo-filter-fenced-code) Extend syntax for the native fenced code block. - [hexo-filter-flowchart](https://github.com/bubkoo/hexo-filter-flowchart) Generate flowchart diagrams for Hexo. - [hexo-filter-sequence](https://github.com/bubkoo/hexo-filter-sequence) Generate UML sequence diagrams for Hexo. - [hexo-filter-sub](https://github.com/bubkoo/hexo-filter-sub) Generate subscript (``) tag for Hexo. - [hexo-filter-sup](https://github.com/bubkoo/hexo-filter-sup) Generate superscript (``) tag for Hexo. - [hexo-theme-formula](https://github.com/bubkoo/hexo-theme-formula) Hexo theme base on jade and less. ## Contributing Pull requests and stars are highly welcome. For bugs and feature requests, please [create an issue](https://github.com/bubkoo/hexo-toc/issues/new). ================================================ FILE: index.js ================================================ /* globals hexo: true */ var assign = require('object-assign'); var filter = require('./lib/filter'); var slugify = require('./lib/slugify'); var config = hexo.config.toc || {}; hexo.config.toc = assign({}, config, { slugify: slugify.load(config.slugify) }); hexo.extend.filter.register('before_post_render', filter.insert); hexo.extend.filter.register('after_post_render', filter.heading); ================================================ FILE: lib/filter.js ================================================ var toc = require('markdown-toc'); var assign = require('object-assign'); var cheerio = require('cheerio'); exports.insert = function (data) { var options = assign({}, this.config.toc); // add class option if (options.class) { data.content = data.content.replace("", '
'); } data.content = toc.insert(data.content, options); return data; }; exports.heading = function (data) { var options = assign({}, this.config.toc); var $ = cheerio.load(data.content, { decodeEntities: ( options.decodeEntities !== undefined ? options.decodeEntities : false ) }); var headings = $('h1, h2, h3, h4, h5, h6'); headings.each(function () { var $title = $(this); var title = $title.text(); var id = toc.slugify(title, options); // $title.attr('id', id); $title.children('a').remove(); $title.html( '' + $title.html() + '' ); $title.removeAttr('id'); if (options.anchor) { var anchorOpts = assign( { position: 'after', symbol: '#', style: 'header-anchor' }, options.anchor); // Put the anchor after the title by default, unless says otherwise var link = '' + anchorOpts.symbol + ''; if (anchorOpts.position === 'before') { $title.prepend(link); } else { $title.append(link); } } }); data.content = $.html(); // add class option if (options.class) { data.content = data.content.replace('
', '
').replace('
', '
'); } return data; }; ================================================ FILE: lib/slugify.js ================================================ exports.load = function (name) { var slugify; if (name && typeof name === 'string') { switch (name) { case 'transliteration': slugify = require('transliteration').slugify; break; default: slugify = require('uslug'); break; } } else { // default is 'uslug' slugify = require('uslug'); } return slugify; }; ================================================ FILE: package.json ================================================ { "name": "hexo-toc", "version": "1.1.0", "description": "Insert a markdown TOC(Table Of Content) before posts be rendered. ", "main": "index.js", "scripts": { "lint": "jshint index.js", "pretest": "npm run lint", "test": "echo ", "prepublish": "npm test" }, "repository": { "type": "git", "url": "https://github.com/bubkoo/hexo-toc.git" }, "keywords": [ "hexo", "toc", "filter", "markdown" ], "author": { "name": "bubkoo.wy", "email": "bubkoo.wy@gmail.com" }, "license": "MIT", "bugs": { "url": "https://github.com/bubkoo/hexo-toc/issues" }, "homepage": "https://github.com/bubkoo/hexo-toc", "dependencies": { "cheerio": "^0.19.0", "markdown-toc": "^0.12.12", "object-assign": "^4.0.1", "transliteration": "^1.1.7", "uslug": "^1.0.3" }, "devDependencies": { "jshint": "^2.9.1" } }