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/dist/markdown-it-include.js b/dist/markdown-it-include.js index a26daf7..ab5da17 100644 --- a/dist/markdown-it-include.js +++ b/dist/markdown-it-include.js @@ -502,7 +502,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') { @@ -510,12 +513,15 @@ module.exports = function include_plugin(md, options) { } else { root = options.root || root; includeRe = options.includeRe || includeRe; + throwError = options.throwError || throwError; + notFoundMessage = options.notFoundMessage || notFoundMessage; + circularMessage = options.circularMessage || circularMessage; } } function _replaceIncludeByContent(src, rootdir, parentFilePath, filesProcessed) { filesProcessed = filesProcessed ? filesProcessed.slice() : []; // making a copy - var cap, filePath, mdSrc, indexOfCircularRef; + var cap, filePath, mdSrc, errorMessage; // store parent file path to check circular references if (parentFilePath) { @@ -524,15 +530,29 @@ module.exports = function include_plugin(md, options) { while ((cap = includeRe.exec(src))) { filePath = path.resolve(rootdir, cap[1].trim()); - // 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 or if there is a circular reference + if (!fs.existsSync(filePath)) { + // child file does not exist + errorMessage = notFoundMessage.replace('{{FILE}}', filePath); + } else if (filesProcessed.indexOf(filePath) !== -1) { + // reference would be circular + errorMessage = circularMessage.replace('{{FILE}}', filePath).replace('{{PARENT}}', parentFilePath); + } + + // check if there were any errors + if (errorMessage) { + if (throwError) { + throw new Error(errorMessage); + } + mdSrc = errorMessage; + } 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; diff --git a/dist/markdown-it-include.min.js b/dist/markdown-it-include.min.js index 01289b3..c2badef 100644 --- a/dist/markdown-it-include.min.js +++ b/dist/markdown-it-include.min.js @@ -1 +1 @@ -!function(e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define([],e):("undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:this).markdownitInclude=e()}(function(){return function o(u,f,s){function c(r,e){if(!f[r]){if(!u[r]){var n="function"==typeof require&&require;if(!e&&n)return n(r,!0);if(l)return l(r,!0);var t=new Error("Cannot find module '"+r+"'");throw t.code="MODULE_NOT_FOUND",t}var i=f[r]={exports:{}};u[r][0].call(i.exports,function(e){return c(u[r][1][e]||e)},i,i.exports,o,u,f,s)}return f[r].exports}for(var l="function"==typeof require&&require,e=0;e