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

Add support for "absolute" root syntax #23

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions dist/markdownItInclude.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/markdownItInclude.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions dist/markdownItInclude.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ const include_plugin = (md, options) => {
}

if (!errorMessage) {
filePath = path.resolve(rootdir, includePath); // check if child file exists or if there is a circular reference
filePath = path.resolve(includePath.indexOf('/') === 0 ? rootdir : parentFilePath ? path.dirname(parentFilePath) : rootdir, `./${includePath}`); // check if child file exists or if there is a circular reference

if (!fs.existsSync(filePath)) {
// child file does not exist
Expand All @@ -93,7 +93,7 @@ const include_plugin = (md, options) => {
// 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); // remove one trailing newline, if it exists: that way, the included content does NOT
mdSrc = _replaceIncludeByContent(mdSrc, rootdir, filePath, filesProcessed); // remove one trailing newline, if it exists: that way, the included content does NOT
// automatically terminate the paragraph it is in due to the writer of the included
// part having terminated the content with a newline.
// However, when that snippet writer terminated with TWO (or more) newlines, these, minus one,
Expand Down
2 changes: 1 addition & 1 deletion dist/markdownItInclude.mjs.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions dist/markdownItInclude.modern.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/markdownItInclude.modern.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions dist/markdownItInclude.umd.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/markdownItInclude.umd.js.map

Large diffs are not rendered by default.

9 changes: 7 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,12 @@ const include_plugin = (md, options) => {
}

if (!errorMessage) {
filePath = path.resolve(rootdir, includePath);
filePath = path.resolve(includePath.indexOf('/') === 0
? rootdir
: parentFilePath
? path.dirname(parentFilePath)
: rootdir
, `./${includePath}`);

// check if child file exists or if there is a circular reference
if (!fs.existsSync(filePath)) {
Expand All @@ -73,7 +78,7 @@ const include_plugin = (md, options) => {
// 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);
mdSrc = _replaceIncludeByContent(mdSrc, rootdir, filePath, filesProcessed);
// remove one trailing newline, if it exists: that way, the included content does NOT
// automatically terminate the paragraph it is in due to the writer of the included
// part having terminated the content with a newline.
Expand Down
8 changes: 8 additions & 0 deletions test/default.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@ describe('plugin', () => {
'<p><em>a content</em>\n<em>a content</em></p>\n');
});

it ('absolute root path reference', () => {
const md = markdown()
.use(markdown_it_include, fixturesPath);

assert.equal(md.render('!!! include( L1/L2/r.md ) !!!'),
'<p><em>a content</em></p>\n');
});

it ('default options', () => {
let md = markdown()
.use(markdown_it_include);
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/L1/L2/r.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
!!!include(/a.md)!!!