├── .gitignore ├── lib ├── index.js └── include.js ├── README.md ├── package.json ├── LICENSE └── test └── test.js /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules/ 2 | -------------------------------------------------------------------------------- /lib/index.js: -------------------------------------------------------------------------------- 1 | /** 2 | * hexo-include 3 | * https://github.com/pirtleshell/hexo-include.git 4 | * Copyright (c) 2015, Robert Pirtle 5 | * Licensed under MIT License 6 | * 7 | * Inserts the raw contents of a file into a hexo markdown file. 8 | * 9 | * Syntax: 10 | * {% include path/to/file %} 11 | * Path is relative to your source directory. 12 | */ 13 | 14 | var include = require('./include')(hexo); 15 | 16 | hexo.extend.tag.register('include', include, {asyn: true}); 17 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # hexo-include 2 | 3 | A [Hexo](https://github.com/hexojs/hexo) plugin for including the raw content of a text file into a post directly from its markdown. Easily insert pre-written HMTL, JS, or the contents of _any_ text file into the body of your post or page. 4 | 5 | ## Usage 6 | 7 | Insert the following into the post's markdown where you want the contents of the external file inserted. All file paths are relative to your `source` directory. 8 | ``` 9 | {% include path/to/file.bar %} 10 | ``` 11 | 12 | ## Install 13 | 14 | Install with [npm](https://www.npmjs.com/) from the base directory of your Hexo site: 15 | 16 | ``` 17 | $ npm install --save --only=prod hexo-include 18 | ``` 19 | 20 | The `--only=prod` ensures that the development dependencies are not installed. If you want to run the tests, remove this flag. More info [here](https://docs.npmjs.com/cli/install). 21 | 22 | ## License 23 | 24 | MIT (c) 2016 Robert Pirtle. 25 | -------------------------------------------------------------------------------- /lib/include.js: -------------------------------------------------------------------------------- 1 | /* 2 | * Hexo include tag function 3 | * 4 | * Takes context. Exports function that grabs contents of file 5 | * given a filename relavtive to source directory. 6 | */ 7 | 8 | var pathFn = require('path'); 9 | var fs = require('hexo-fs'); 10 | 11 | module.exports = function(ctx) { 12 | return function includeTag(args) { 13 | var path = pathFn.join(ctx.source_dir, args[0]); 14 | 15 | // exit if path is not defined 16 | if (!path) { 17 | console.warn("Include file path undefined."); 18 | return; 19 | } 20 | 21 | // check existence, if it does, check there is content, return content 22 | return fs.exists(path).then(function(exist) { 23 | if (!exist) { 24 | console.warn('Include file not found.'); 25 | return; 26 | } 27 | return fs.readFile(path).then(function(contents) { 28 | if (!contents) { 29 | console.warn('Include file empty.'); 30 | return; 31 | } 32 | return contents; 33 | }); 34 | }); 35 | }; 36 | }; 37 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "hexo-include", 3 | "version": "1.1.0", 4 | "description": "A Hexo plugin for including raw content of any text file into a post", 5 | "license": "MIT", 6 | "repository": { 7 | "type": "git", 8 | "url": "git+https://github.com/pirtleshell/hexo-include.git" 9 | }, 10 | "author": { 11 | "name": "Robert Pirtle", 12 | "email": "astropirtle@gmail.com", 13 | "url": "pirtle.xyz" 14 | }, 15 | "main": "lib/index.js", 16 | "keywords": [ 17 | "hexo", 18 | "tag", 19 | "include", 20 | "insert" 21 | ], 22 | "bugs": { 23 | "url": "https://github.com/pipirt/hexo-include/issues" 24 | }, 25 | "homepage": "https://github.com/pipirt/hexo-include#readme", 26 | "devDependencies": { 27 | "babel-register": "^6.16.3", 28 | "chai": "^3.5.0", 29 | "eslint-config-hexo": "^1.0.3", 30 | "hexo": "^3.2.2", 31 | "hexo-fs": "^0.1.6", 32 | "mocha": "^3.1.1", 33 | "xo": "^0.17.0" 34 | }, 35 | "scripts": { 36 | "test": "xo && mocha test/test.js" 37 | }, 38 | "xo": { 39 | "extends": "hexo", 40 | "root": true, 41 | "globals": ["hexo"] 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2016 Robert Pirtle 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 | -------------------------------------------------------------------------------- /test/test.js: -------------------------------------------------------------------------------- 1 | /* eslint-env node, mocha */ 2 | 3 | var pathFn = require('path'); 4 | var should = require('chai').should(); 5 | var Hexo = require('hexo'); 6 | var fs = require('hexo-fs'); 7 | 8 | var hexo = new Hexo(pathFn.join(__dirname, 'include_test')); 9 | 10 | var include = require('../lib/include.js')(hexo); 11 | 12 | describe('Include tag', function() { 13 | var filePath = pathFn.join(hexo.source_dir, 'test_dir/test.html'); 14 | var emptyPath = pathFn.join(hexo.source_dir, 'test_dir/empty.html'); 15 | 16 | var fixture = [ 17 | '

go to sleep ya little bae

', 18 | 'if (tired && night){', 19 | ' sleep();', 20 | '}' 21 | ].join('\n'); 22 | 23 | // returns the rendered contents 24 | function renderedContent(file) { 25 | return include([file]); 26 | } 27 | 28 | before(function() { 29 | // create files for testing 30 | fs.writeFileSync(filePath, fixture); 31 | fs.writeFileSync(emptyPath, ''); 32 | return; 33 | }); 34 | 35 | after(function() { 36 | // remove the testing arena 37 | return fs.rmdir(hexo.base_dir); 38 | }); 39 | 40 | it('existing file', function() { 41 | return renderedContent('test_dir/test.html').then(function(result) { 42 | result.should.eql(fixture); 43 | }); 44 | }); 45 | 46 | it('empty file', function() { 47 | 48 | return renderedContent('test_dir/empty.html').then(function(result) { 49 | should.not.exist(result); 50 | }); 51 | }); 52 | 53 | it('nonexistent file', function() { 54 | return renderedContent('this/file/doesnt/exist.magic').then(function(result) { 55 | should.not.exist(result); 56 | }); 57 | }); 58 | }); 59 | --------------------------------------------------------------------------------