Skip to content

Commit

Permalink
Merged PR from @SIPS1980 for #5, #6 and #7
Browse files Browse the repository at this point in the history
  • Loading branch information
Camel Aissani committed May 15, 2020
2 parents f906a34 + 8c58261 commit adee5e2
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 16 deletions.
32 changes: 25 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down
36 changes: 28 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,26 @@ 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') {
root = 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) {
Expand All @@ -29,15 +35,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;
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 adee5e2

Please sign in to comment.