Skip to content

Commit

Permalink
Merge 01e2de1 into eb20d48
Browse files Browse the repository at this point in the history
  • Loading branch information
BuptStEve committed Feb 18, 2019
2 parents eb20d48 + 01e2de1 commit 2e8afb4
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 12 deletions.
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,29 @@ It will produce this
<p>Follow me on twitter!</p>
```

## 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.
Expand Down
21 changes: 16 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand All @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
41 changes: 36 additions & 5 deletions test/default.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) !!!'),
'<p><em>a content</em>\n<em>a content</em></p>\n');
});

it ('default options', function () {
var md = require('markdown-it')()
.use(require('../'));

assert.equal(md.render('!!! include( test/fixtures/a.md ) !!!\n'),
'<p><em>a content</em></p>\n');

md = require('markdown-it')()
.use(require('../'), {});

assert.equal(md.render('!!! include( test/fixtures/a.md ) !!!\n'),
'<p><em>a content</em></p>\n');
});

it ('root option', function () {
var md = require('markdown-it')()
.use(require('../'), { root: fixturesPath });

assert.equal(md.render('!!! include( a.md ) !!!\n'),
'<p><em>a content</em></p>\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'),
'<p><em>a content</em></p>\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 ) !!!');
Expand All @@ -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 ) !!!');
Expand All @@ -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 ) !!!');
Expand Down

0 comments on commit 2e8afb4

Please sign in to comment.