Skip to content

Commit

Permalink
Update Tests
Browse files Browse the repository at this point in the history
  • Loading branch information
SIPS1980 committed Jun 9, 2019
1 parent 0ad2ee4 commit 8c58261
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 16 deletions.
6 changes: 6 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,15 @@ GITHUB_PROJ := https://github.com//camelaissani//markdown-it-include
lint:
./node_modules/.bin/eslint .

lintWin10PS:
.\node_modules\.bin\eslint .

test: lint
./node_modules/.bin/mocha -R spec

testWin10PS: lintWin10PS
.\node_modules\.bin\mocha -R spec

coverage:
rm -rf coverage
./node_modules/.bin/istanbul cover node_modules/.bin/_mocha
Expand Down
26 changes: 11 additions & 15 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ module.exports = function include_plugin(md, options) {
} else {
root = options.root || root;
includeRe = options.includeRe || includeRe;
throwError = options.throwError === undefined ? throwError : options.throwError;
notFoundMessage = options.notFoundMessage === undefined ? notFoundMessage : options.notFoundMessage;
circularMessage = options.circularMessage === undefined ? circularMessage : options.circularMessage;
throwError = options.throwError || throwError;
notFoundMessage = options.notFoundMessage || notFoundMessage;
circularMessage = options.circularMessage || circularMessage;
}
}

Expand All @@ -34,25 +34,21 @@ module.exports = function include_plugin(md, options) {
}
while ((cap = includeRe.exec(src))) {
filePath = path.resolve(rootdir, cap[1].trim());
mdSrc = undefined;
mdSrc = '';

// check if child file exists
// check if child file exists or if there is a circular reference
if (fs.existsSync(filePath) === false) {
// child file does not exist
mdSrc = notFoundMessage.replace('{{FILE}}', filePath);
} else {
// check if circular reference
if (filesProcessed.indexOf(filePath) !== -1) {
mdSrc = circularMessage.replace('{{FILE}}', filePath).replace('{{PARENT}}', parentFilePath)
}
} else if (filesProcessed.indexOf(filePath) !== -1) {
// reference would be circular
mdSrc = circularMessage.replace('{{FILE}}', filePath).replace('{{PARENT}}', parentFilePath);
}

// check if there were any errors and / or ensure error message is not empty
if (mdSrc !== undefined) {
if (throwError === true && mdSrc !== '') {
throw new Error(mdSrc)
}
} else {
if (mdSrc !== '' && throwError === true) {
throw new Error(mdSrc);
} else if (mdSrc === '') {
// get content of child file
mdSrc = fs.readFileSync(filePath, 'utf8');
// check if child file also has includes
Expand Down
2 changes: 1 addition & 1 deletion test/default.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ describe('plugin', function () {

assert.throws(function () {
md.render('!!! include( xxx.md ) !!!');
}, Error, /ENOENT/);
}, Error, /not found/i);
});

it ('direct circular reference', function () {
Expand Down

0 comments on commit 8c58261

Please sign in to comment.