├── .gitattributes ├── docs ├── examples.md ├── options.md └── quickstart.md ├── .gitignore ├── .jshintrc ├── .editorconfig ├── .verb.md ├── LICENSE ├── package.json ├── index.js └── README.md /.gitattributes: -------------------------------------------------------------------------------- 1 | # Enforce Unix newlines 2 | * text eol=lf 3 | 4 | # binaries 5 | *.ai binary 6 | *.psd binary 7 | *.jpg binary 8 | *.gif binary 9 | *.png binary 10 | *.jpeg binary -------------------------------------------------------------------------------- /docs/examples.md: -------------------------------------------------------------------------------- 1 | ```js 2 | assemble: { 3 | options: { 4 | plugins: ['{%= name %}'], 5 | toc: { 6 | id: 'toc', 7 | modifier: '' 8 | } 9 | } 10 | } 11 | ``` 12 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.DS_Store 2 | lib-cov 3 | *.seed 4 | *.log 5 | *.csv 6 | *.dat 7 | *.out 8 | *.pid 9 | *.gz 10 | *.yo-rc.json 11 | 12 | pids 13 | logs 14 | results 15 | 16 | 17 | npm-debug.log 18 | node_modules 19 | tmp 20 | 21 | *.sublime-* 22 | -------------------------------------------------------------------------------- /docs/options.md: -------------------------------------------------------------------------------- 1 | ## toc 2 | Type: `String` 3 | Default: `'toc'` 4 | 5 | Tag to determine where the table of contents is located 6 | 7 | ## modifier 8 | Type: `String` 9 | Default: `''` 10 | 11 | CSS class used in the wrapper `ul` for the table of contents. 12 | -------------------------------------------------------------------------------- /.jshintrc: -------------------------------------------------------------------------------- 1 | { 2 | "asi": false, 3 | "boss": true, 4 | "curly": true, 5 | "eqeqeq": true, 6 | "eqnull": true, 7 | "esnext": true, 8 | "immed": true, 9 | "latedef": false, 10 | "laxcomma": false, 11 | "mocha": true, 12 | "newcap": true, 13 | "noarg": true, 14 | "node": true, 15 | "sub": true, 16 | "undef": true, 17 | "unused": true 18 | } -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | # http://editorconfig.org 2 | root = true 3 | 4 | [*] 5 | indent_style = space 6 | indent_size = 2 7 | end_of_line = lf 8 | charset = utf-8 9 | trim_trailing_whitespace = true 10 | insert_final_newline = true 11 | 12 | [*.md] 13 | trim_trailing_whitespace = false 14 | 15 | [test/fixtures/*] 16 | insert_final_newline = false 17 | trim_trailing_whitespace = false -------------------------------------------------------------------------------- /.verb.md: -------------------------------------------------------------------------------- 1 | # {%= name %} {%= badge("fury") %} 2 | 3 | > {%= description %} 4 | 5 | ## Quickstart 6 | {%= docs("quickstart") %} 7 | 8 | ## Options 9 | {%= docs("options") %} 10 | 11 | ## Usage Examples 12 | {%= docs("examples") %} 13 | 14 | ## Assemble plugins 15 | {%= related(verb.related.list, {remove: name}) %} 16 | 17 | ## Contributing 18 | {%= include("contributing") %} 19 | 20 | ## Author 21 | {%= include("author") %} 22 | 23 | ## License 24 | {%= copyright() %} 25 | {%= license() %} 26 | 27 | *** 28 | 29 | {%= include("footer") %} 30 | -------------------------------------------------------------------------------- /docs/quickstart.md: -------------------------------------------------------------------------------- 1 | In the command line, run: 2 | 3 | ```sh 4 | npm install {%= name %} --save-dev 5 | ``` 6 | 7 | Next, to register the plugin with Assemble in your project's Gruntfile you can either specify the direct path to the plugin(s) (e.g. `./path/to/plugins/*.js`), or if installed via npm, make sure the plugin is in the `devDependencies` of your project.js package.json, and simply add the module's name to the `plugins` option: 8 | 9 | ```js 10 | assemble: { 11 | options: { 12 | plugins: ['{%= name %}', 'other/plugins/*.js'] 13 | } 14 | } 15 | ``` 16 | Visit the [plugins docs](http://assemble.io/plugins/) for more info or for help getting started. 17 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2014-2015, Brian Woodward. 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 13 | all 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 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "grunt-assemble-toc", 3 | "description": "Assemble middleware for adding a Table of Contents (TOC) to any HTML page.", 4 | "version": "0.1.0", 5 | "homepage": "http://assemble.io", 6 | "author": "Brian Woodward (http://github.com/assemble)", 7 | "repository": { 8 | "type": "git", 9 | "url": "git://github.com/assemble/grunt-assemble-toc.git" 10 | }, 11 | "bugs": { 12 | "url": "https://github.com/assemble/grunt-assemble-toc/issues" 13 | }, 14 | "license": { 15 | "type": "MIT", 16 | "url": "https://github.com/assemble/grunt-assemble-toc/blob/master/LICENSE" 17 | }, 18 | "files": [ 19 | "index.js" 20 | ], 21 | "main": "index.js", 22 | "engines": { 23 | "node": ">=0.8" 24 | }, 25 | "scripts": { 26 | "test": "grunt test" 27 | }, 28 | "dependencies": { 29 | "cheerio": "~0.15.0" 30 | }, 31 | "keywords": [ 32 | "assemble", 33 | "assemblemiddleware", 34 | "assembleplugin", 35 | "middleware", 36 | "plugin", 37 | "table of contents", 38 | "toc" 39 | ], 40 | "verb": { 41 | "related": { 42 | "list": [ 43 | "grunt-assemble", 44 | "grunt-assemble-anchors", 45 | "grunt-assemble-contextual", 46 | "grunt-assemble-decompress", 47 | "grunt-assemble-download", 48 | "grunt-assemble-i18n", 49 | "grunt-assemble-lunr", 50 | "grunt-assemble-navigation", 51 | "grunt-assemble-permalinks", 52 | "grunt-assemble-sitemap", 53 | "grunt-assemble-toc", 54 | "grunt-assemble-wordcount" 55 | ] 56 | } 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | /*! 2 | * grunt-assemble-toc 3 | * 4 | * Copyright (c) 2013-2015, Brian Woodward. 5 | * Licensed under the MIT License. 6 | */ 7 | 8 | var options = { 9 | stage: 'render:post:page' 10 | }; 11 | 12 | var cheerio = require('cheerio'); 13 | 14 | /** 15 | * Anchor Plugin 16 | * @param {Object} params 17 | * @param {Function} callback 18 | */ 19 | module.exports = function(params, callback) { 20 | 'use strict'; 21 | 22 | var opts = params.assemble.options; 23 | opts.toc = opts.toc || {}; 24 | 25 | // id to use to append TOC 26 | var id = '#' + (opts.toc.id || 'toc'); 27 | var modifier = opts.toc.modifier || ''; 28 | var li = opts.toc.li ? (' class="' + opts.toc.li + '"') : ''; 29 | 30 | // load current page content 31 | var $ = cheerio.load(params.content); 32 | var toc = cheerio.load(''); 33 | 34 | // get all the anchor tags from inside the headers 35 | var anchors = $('h1 a[name],h2 a[name],h3 a[name],h4 a[name]'); 36 | anchors.map(function(i, e) { 37 | var text = $(e.parent).text().trim(); 38 | var link = e.attribs.name 39 | var depth = parseInt(e.parent.name.replace(/h/gi, ''), 10); 40 | 41 | var arr = new Array(depth); 42 | var level = arr.join('
  • '); 43 | toc('#toc-list').append(level); 44 | }); 45 | $(id).append(toc.html() 46 | .replace(/(
  • \s*