Skip to content

Commit

Permalink
Allowing to include the same file twice - fix #1
Browse files Browse the repository at this point in the history
  • Loading branch information
Lucas-C committed Dec 12, 2017
1 parent 533c7e5 commit 9ed64d4
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
9 changes: 4 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@ var path = require('path'),
var INCLUDE_RE = /\!{3}\s*include\s*\(\s*(.+?)\s*\)\s*\!{3}/i;

module.exports = function include_plugin(md, basedir) {
var filesProcessed;

function _replaceIncludeByContent(src, rootdir, parentFilePath) {
function _replaceIncludeByContent(src, rootdir, parentFilePath, filesProcessed) {
filesProcessed = filesProcessed ? filesProcessed.slice() : []; // making a copy
var cap, filePath, mdSrc, indexOfCircularRef;

// store parent file path to check circular references
Expand All @@ -25,15 +24,15 @@ module.exports = function include_plugin(md, basedir) {
}

// replace include by file content
mdSrc = _replaceIncludeByContent(fs.readFileSync(filePath, 'utf8'), path.dirname(filePath), filePath);
mdSrc = fs.readFileSync(filePath, 'utf8');
mdSrc = _replaceIncludeByContent(mdSrc, path.dirname(filePath), filePath, filesProcessed);
src = src.slice(0, cap.index) + mdSrc + src.slice(cap.index + cap[0].length, src.length);
}
return src;
}

function _includeFileParts(state) {
var rootdir = basedir || '.';
filesProcessed = [];
state.src = _replaceIncludeByContent(state.src, rootdir);
}

Expand Down
8 changes: 8 additions & 0 deletions test/default.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@ describe('plugin', function () {
.use(require('../'), path.join(__dirname, 'fixtures'));
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'));

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

describe('wrong workflows', function() {
Expand Down

0 comments on commit 9ed64d4

Please sign in to comment.