Skip to content

Commit

Permalink
Proposal for #5, #6 and #7; includes README update
Browse files Browse the repository at this point in the history
  • Loading branch information
SIPS1980 committed Jun 7, 2019
1 parent 1d8fdaf commit 0ad2ee4
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 15 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
38 changes: 30 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,37 +7,59 @@ 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 === 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) {
filesProcessed.push(parentFilePath);
}
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;
Expand Down

0 comments on commit 0ad2ee4

Please sign in to comment.