Skip to content

Commit

Permalink
Allow recursive includes (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
jescalan authored Oct 10, 2019
1 parent 0714d1e commit 52aed66
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 11 deletions.
8 changes: 5 additions & 3 deletions plugins/include-markdown/fixtures/basic.expected.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
hello this is a file that uses an include

hi i come from an **included file**
include/before

yay!
nested/include2

I come from a nested folder
include/after

nested/include2

isn't that neat?
6 changes: 4 additions & 2 deletions plugins/include-markdown/fixtures/include.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
hi i come from an **included file**
include/before

yay!
@include 'nested/include2.md'

include/after
2 changes: 1 addition & 1 deletion plugins/include-markdown/fixtures/nested/include2.md
Original file line number Diff line number Diff line change
@@ -1 +1 @@
I come from a nested folder
nested/include2
3 changes: 3 additions & 0 deletions plugins/include-markdown/fixtures/nested/include3.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
nested/include3

@include 'include2.md'
4 changes: 3 additions & 1 deletion plugins/include-markdown/fixtures/resolve-from.expected.md
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
I come from a nested folder
nested/include3

nested/include2
2 changes: 1 addition & 1 deletion plugins/include-markdown/fixtures/resolve-from.md
Original file line number Diff line number Diff line change
@@ -1 +1 @@
@include 'include2.md'
@include 'include3.md'
8 changes: 5 additions & 3 deletions plugins/include-markdown/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,18 @@ module.exports = function includeMarkdownPlugin({ resolveFrom } = {}) {
)
let includeContents
try {
includeContents = readSync(includePath)
includeContents = readSync(includePath, 'utf8')
} catch (err) {
throw new Error(
`The @include file path at ${includePath} was not found.\n\nInclude Location: ${file.path}:${node.position.start.line}:${node.position.start.column}`
)
}

// return the file contents in place of the @include

return remark().parse(includeContents).children
// this takes a couple steps because we allow recursive includes
const processor = remark().use(includeMarkdownPlugin, { resolveFrom })
const ast = processor.parse(includeContents)
return processor.runSync(ast, includeContents).children
})
}
}

0 comments on commit 52aed66

Please sign in to comment.