diff --git a/README.md b/README.md index ba82441..7b935dc 100644 --- a/README.md +++ b/README.md @@ -55,6 +55,29 @@ It will produce this

Follow me on twitter!

``` +## Options + +```js +var md = require('markdown-it')() + .use(require('markdown-it-include'), [, options]); +``` + +* Type: `String|Object` + +If it's a string, it's the same as `options.root`. + +### root +* Type: `String` +* Default: `.` + +`root` is the base directory of all the markdown files. + +### includeRe +* Type: `RegExp` +* Default: `/\!{3}\s*include\s*\(\s*(.+?)\s*\)\s*\!{3}/i` + +By default the `!!!include( )!!!` statement is used to include markdown fragment files. This option allows to change the regular expression and then customize this statement. + ## Disclaimer This purposefully doesn't conform to any spec or discussion related to CommonMark. diff --git a/index.js b/index.js index a1a1680..1b7b3ff 100644 --- a/index.js +++ b/index.js @@ -5,7 +5,19 @@ var path = require('path'), var INCLUDE_RE = /\!{3}\s*include\s*\(\s*(.+?)\s*\)\s*\!{3}/i; -module.exports = function include_plugin(md, basedir) { +module.exports = function include_plugin(md, options) { + var root = '.', + includeRe = INCLUDE_RE; + + if (options) { + if (typeof options === 'string') { + root = options; + } else { + root = options.root || root; + includeRe = options.includeRe || includeRe; + } + } + function _replaceIncludeByContent(src, rootdir, parentFilePath, filesProcessed) { filesProcessed = filesProcessed ? filesProcessed.slice() : []; // making a copy var cap, filePath, mdSrc, indexOfCircularRef; @@ -14,8 +26,8 @@ module.exports = function include_plugin(md, basedir) { if (parentFilePath) { filesProcessed.push(parentFilePath); } - while ((cap = INCLUDE_RE.exec(src))) { - filePath = path.resolve(rootdir, cap[1]); + while ((cap = includeRe.exec(src))) { + filePath = path.resolve(rootdir, cap[1].trim()); // check if circular reference indexOfCircularRef = filesProcessed.indexOf(filePath); @@ -32,8 +44,7 @@ module.exports = function include_plugin(md, basedir) { } function _includeFileParts(state) { - var rootdir = basedir || '.'; - state.src = _replaceIncludeByContent(state.src, rootdir); + state.src = _replaceIncludeByContent(state.src, root); } md.core.ruler.before('normalize', 'include', _includeFileParts); diff --git a/package.json b/package.json index f14c84a..c1ec8da 100644 --- a/package.json +++ b/package.json @@ -1,12 +1,12 @@ { "name": "markdown-it-include", - "version": "1.0.1", + "version": "1.1.0", "description": "Markdown-it plugin which adds the ability to include markdown fragment files.", "keywords": [ "markdown-it-plugin", "markdown-it", "markdown", - "inlcude" + "include" ], "repository": { "type": "git", diff --git a/test/default.js b/test/default.js index fdf0913..4c3ceaf 100644 --- a/test/default.js +++ b/test/default.js @@ -5,28 +5,59 @@ var assert = require('chai').assert, generate = require('markdown-it-testgen'); /*eslint-env mocha*/ +var fixturesPath = path.join(__dirname, 'fixtures'); describe('plugin', function () { describe('right workflows', function () { it ('default.txt', function () { var md = require('markdown-it')() - .use(require('../'), path.join(__dirname, 'fixtures')); + .use(require('../'), fixturesPath); generate(path.join(__dirname, 'fixtures/default.txt'), md); }); it ('including same field twice', function () { var md = require('markdown-it')() - .use(require('../'), path.join(__dirname, 'fixtures')); + .use(require('../'), fixturesPath); assert.equal(md.render('!!! include( a.md ) !!!\n!!! include( a.md ) !!!'), '

a content\na content

\n'); }); + + it ('default options', function () { + var md = require('markdown-it')() + .use(require('../')); + + assert.equal(md.render('!!! include( test/fixtures/a.md ) !!!\n'), + '

a content

\n'); + + md = require('markdown-it')() + .use(require('../'), {}); + + assert.equal(md.render('!!! include( test/fixtures/a.md ) !!!\n'), + '

a content

\n'); + }); + + it ('root option', function () { + var md = require('markdown-it')() + .use(require('../'), { root: fixturesPath }); + + assert.equal(md.render('!!! include( a.md ) !!!\n'), + '

a content

\n'); + }); + + it ('includeRe option', function () { + var md = require('markdown-it')() + .use(require('../'), { root: fixturesPath, includeRe: /<\[include\]\((.+)\)/i }); + + assert.equal(md.render('<[include]( a.md )\n'), + '

a content

\n'); + }); }); describe('wrong workflows', function () { it ('file not found', function () { var md = require('markdown-it')() - .use(require('../'), path.join(__dirname, 'fixtures')); + .use(require('../'), fixturesPath); assert.throws(function () { md.render('!!! include( xxx.md ) !!!'); @@ -35,7 +66,7 @@ describe('plugin', function () { it ('direct circular reference', function () { var md = require('markdown-it')() - .use(require('../'), path.join(__dirname, 'fixtures')); + .use(require('../'), fixturesPath); assert.throws(function () { md.render('!!! include( c.md ) !!!'); @@ -44,7 +75,7 @@ describe('plugin', function () { it ('indirect circular reference', function () { var md = require('markdown-it')() - .use(require('../'), path.join(__dirname, 'fixtures')); + .use(require('../'), fixturesPath); assert.throws(function () { md.render('!!! include( L1/L2/e2.md ) !!!');