Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proposal for #5, #6 and #7; includes README update #8

Merged
merged 2 commits into from
May 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
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
34 changes: 26 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,37 +7,55 @@ 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;

// 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 = '';
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would have used a new variable to handle the error message. Like errorMessage.


// 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) === false) {
// child file does not exist
mdSrc = notFoundMessage.replace('{{FILE}}', filePath);
} 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 !== '' && 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
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