├── .npmrc ├── .gitignore ├── example.js ├── package.json ├── README.md └── index.js /.npmrc: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | *.log 3 | .DS_Store 4 | npm-debug.log.* 5 | -------------------------------------------------------------------------------- /example.js: -------------------------------------------------------------------------------- 1 | const fs = require('fs'); 2 | const path = require('path'); 3 | const markdownMagic = require('markdown-magic'); 4 | 5 | const config = { 6 | transforms: { 7 | DIRTREE: require('./index.js'), 8 | }, 9 | }; 10 | 11 | const markdownPath = path.join(__dirname, 'README.md'); 12 | markdownMagic(markdownPath, config); 13 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "markdown-magic-directory-tree", 3 | "version": "1.2.4", 4 | "description": "Print an archy tree for markdown file", 5 | "license": "MIT", 6 | "repository": { 7 | "type": "git", 8 | "url": "git+https://github.com/camacho/markdown-magic-directory-tree.git" 9 | }, 10 | "bugs": { 11 | "url": "https://github.com/camacho/markdown-magic-directory-tree/issues" 12 | }, 13 | "homepage": "https://github.com/camacho/markdown-magic-directory-tree#readme", 14 | "author": "Patrick Camacho ", 15 | "keywords": [ 16 | "markdown-magic", 17 | "archy", 18 | "tree", 19 | "directory", 20 | "directories", 21 | "directory-tree", 22 | "markdown", 23 | "plugin" 24 | ], 25 | "main": "index.js", 26 | "scripts": { 27 | "docs": "node example.js", 28 | "format": "prettier --single-quote --trailing-comma es5 --write index.js example.js", 29 | "prepublish": "npm run docs" 30 | }, 31 | "dependencies": { 32 | "archy": "^1.0.0", 33 | "directory-tree": "^2.0.0" 34 | }, 35 | "peerDependencies": { 36 | "markdown-magic": ">=0.1 <=2.x" 37 | }, 38 | "devDependencies": { 39 | "markdown-magic": "^2.0.0", 40 | "prettier": "^2.0.0" 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Directory tree plugin 2 | 3 | Add directory tree to markdown files via [markdown-magic](https://github.com/DavidWells/markdown-magic) 4 | 5 | ## Install 6 | 7 | ``` 8 | npm i markdown-magic markdown-magic-directory-tree --save-dev 9 | ``` 10 | 11 | ## Adding the plugin 12 | 13 | See `example.js` for usage. 14 | 15 | 16 | 17 | ```js 18 | const fs = require('fs'); 19 | const path = require('path'); 20 | const markdownMagic = require('markdown-magic'); 21 | 22 | const config = { 23 | transforms: { 24 | DIRTREE: require('./index.js'), 25 | }, 26 | }; 27 | 28 | const markdownPath = path.join(__dirname, 'README.md'); 29 | markdownMagic(markdownPath, config); 30 | ``` 31 | 32 | 33 | 34 | ## Usage in markdown 35 | 36 | 37 | ``` 38 | markdown-magic-directory-tree/ 39 | ├── .npmrc 40 | ├── example.js 41 | ├── index.js 42 | ├── package-lock.json 43 | ├── package.json 44 | └── README.md 45 | ``` 46 | 47 | 48 | ## Options 49 | 50 | * **dir** - `process.cwd()` by default 51 | * **ignore** - `['.git', '.gitkeep', '.gitignore', 'node_modules']` by default 52 | * **depth** - `Infinity` by default (how deep in the tree to traverse) 53 | * **onlyDirs** - `false` by default (how mnuch t) 54 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | const path = require('path'); 2 | const { statSync } = require('fs'); 3 | 4 | const archy = require('archy'); 5 | const dirTree = require('directory-tree'); 6 | 7 | const defaults = { 8 | depth: Infinity, 9 | dir: '.', 10 | onlyDirs: false, 11 | }; 12 | 13 | const sortEntries = (a, b) => { 14 | if (a.type === 'directory' && b.type !== 'directory') return -1; 15 | if (a.type !== 'directory' && b.type === 'directory') return 1; 16 | return a.name.localeCompare(b.name); 17 | }; 18 | 19 | const processNode = (node, ignore, options, depth = 0) => { 20 | if ( 21 | ignore.indexOf(node.name) !== -1 || 22 | depth > options.depth || 23 | (options.onlyDirs && node.type !== 'directory') 24 | ) 25 | return; 26 | 27 | const response = { 28 | label: `${node.name}${node.type === 'directory' ? '/' : ''}`, 29 | }; 30 | 31 | if (node.type === 'directory' && depth < options.depth) { 32 | depth++; 33 | response.nodes = node.children 34 | .sort(sortEntries) 35 | .map((child) => processNode(child, ignore, options, depth)) 36 | .filter((child) => !!child); 37 | } 38 | 39 | return response; 40 | }; 41 | 42 | module.exports = function DIRTREE(content, _options = {}, config) { 43 | const options = Object.assign({}, defaults, _options); 44 | 45 | const dir = path.resolve(path.dirname(config.originalPath), options.dir); 46 | 47 | const ignore = options.ignore || [ 48 | '.git', 49 | '.gitkeep', 50 | '.gitignore', 51 | 'node_modules', 52 | ]; 53 | 54 | const tree = archy(processNode(dirTree(dir), ignore, options)); 55 | 56 | return ['```', tree.trim(), '```'].join('\n'); 57 | }; 58 | --------------------------------------------------------------------------------