From 0ad2ee48f87d66bbf1d543f40177a522d5dca902 Mon Sep 17 00:00:00 2001 From: SIPS1980 Date: Fri, 7 Jun 2019 17:51:35 -0400 Subject: [PATCH 1/2] Proposal for #5, #6 and #7; includes README update --- README.md | 32 +++++++++++++++++++++++++------- index.js | 38 ++++++++++++++++++++++++++++++-------- 2 files changed, 55 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index 7b935dc..a065fc4 100644 --- a/README.md +++ b/README.md @@ -19,25 +19,20 @@ bower install markdown-it-include --save Let's create a markdown which uses a header and a footer from two separate files: -**header.md** +### File: '**header.md**' ```markdown - # This is my header for all my markdowns - ``` -**footer.md** +### File: '**footer.md**' ```markdown - Follow me on twitter! - ``` Let's assume that header.md and footer.md are located in `/in/this/directory`. - Now it's your turn to play markdown-it! ```js @@ -67,17 +62,40 @@ var md = require('markdown-it')() 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. +### throwError + +* Type: `Boolean` +* Default: `true` + +When set to `false`, instead of throwing an error message, the error message will be written into the output. For references to possible error messages as well as how to change it, see options 'notFoundMessage' and 'circularMessage' + +### notFoundMessage + +* Type: `String` +* Default: `File '{{FILE}}' not found` + +With `notFoundMessage` the default error message when the to be included file cannot be found can be changed. The marker `{{FILE}}` in the message string will be replaced with the full file path. + +### circularMessage + +* Type: `String` +* Default: `Circular reference between '{{FILE}}' and '{{PARENT}}'` + +With `circularMessage` the default error message when there is a circular reference between files can be changed. The markers `{{FILE}}` and `{{FILE}}` in the message string will be replaced with the respective full file paths. + ## Disclaimer This purposefully doesn't conform to any spec or discussion related to CommonMark. diff --git a/index.js b/index.js index 1b7b3ff..1771c21 100644 --- a/index.js +++ b/index.js @@ -7,7 +7,10 @@ var INCLUDE_RE = /\!{3}\s*include\s*\(\s*(.+?)\s*\)\s*\!{3}/i; module.exports = function include_plugin(md, options) { var root = '.', - includeRe = INCLUDE_RE; + includeRe = INCLUDE_RE, + throwError = true, + notFoundMessage = 'File \'{{FILE}}\' not found', + circularMessage = 'Circular reference between \'{{FILE}}\' and \'{{PARENT}}\''; if (options) { if (typeof options === 'string') { @@ -15,12 +18,15 @@ 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; } } function _replaceIncludeByContent(src, rootdir, parentFilePath, filesProcessed) { filesProcessed = filesProcessed ? filesProcessed.slice() : []; // making a copy - var cap, filePath, mdSrc, indexOfCircularRef; + var cap, filePath, mdSrc; // store parent file path to check circular references if (parentFilePath) { @@ -28,16 +34,32 @@ module.exports = function include_plugin(md, options) { } while ((cap = includeRe.exec(src))) { filePath = path.resolve(rootdir, cap[1].trim()); + mdSrc = undefined; - // check if circular reference - indexOfCircularRef = filesProcessed.indexOf(filePath); - if (indexOfCircularRef !== -1) { - throw new Error('Circular reference between ' + filePath + ' and ' + filesProcessed[indexOfCircularRef]); + // check if child file exists + 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) + } + } + + // 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 { + // get content of child file + mdSrc = fs.readFileSync(filePath, 'utf8'); + // check if child file also has includes + mdSrc = _replaceIncludeByContent(mdSrc, path.dirname(filePath), filePath, filesProcessed); } // replace include by file content - 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; From 8c5826175d497605a82bdf90ce9f049d64366c0d Mon Sep 17 00:00:00 2001 From: SIPS1980 Date: Sun, 9 Jun 2019 10:01:46 -0400 Subject: [PATCH 2/2] Update Tests --- Makefile | 6 ++++++ index.js | 26 +++++++++++--------------- test/default.js | 2 +- 3 files changed, 18 insertions(+), 16 deletions(-) diff --git a/Makefile b/Makefile index 2f633a2..ca8168a 100644 --- a/Makefile +++ b/Makefile @@ -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 diff --git a/index.js b/index.js index 1771c21..27590f4 100644 --- a/index.js +++ b/index.js @@ -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; } } @@ -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 diff --git a/test/default.js b/test/default.js index 4c3ceaf..75b4d47 100644 --- a/test/default.js +++ b/test/default.js @@ -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 () {