-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support MDX components in partials (#28)
* feat: support MDX components in partials * 3.1.1-canary.0 * 3.2.0 * 3.2.0-canary.0 * Clean up comments * Rename option to resolveMdx * docs: update README with notes on resolveMdx * docs: update missing import in README * docs: tweak language around accepted value for resolveMdx * patch: fix issue with comment processing * 3.2.0-canary.1 * refactor: use createMdxAstCompiler proper * 3.2.0-canary.2 * Revert "refactor: use createMdxAstCompiler proper" This reverts commit c6b41e2. * 3.2.0-canary.3 * patch: fix issue with nested includes * 3.2.0-canary.4 * docs: add comment on md-ast-to-mdx-ast * Fix bungled heading include-markdown README Co-authored-by: Jeff Escalante <[email protected]> Co-authored-by: Jeff Escalante <[email protected]>
- Loading branch information
Showing
10 changed files
with
479 additions
and
84 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletions
3
plugins/include-markdown/fixtures/include-nested-component.mdx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
text at depth one | ||
|
||
@include 'nested/include-component.mdx' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
We should now be able include custom MDX components in partials. For example, a `"official"` `PluginTierLabel` should render below: | ||
|
||
<PluginTierLabel tier="official" /> | ||
|
||
Comments should NOT mess things up: | ||
|
||
<!-- HTML comment but nested --> | ||
|
||
But they seem to be messing things up, apparently due to differences in `remark` 12 vs 13. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
some text in an include | ||
|
||
<CustomComponent /> |
3 changes: 3 additions & 0 deletions
3
plugins/include-markdown/fixtures/nested/include-component.mdx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
some text in a nested include | ||
|
||
<NestedComponent /> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/* | ||
NOTE: | ||
This file is swiped directly from @mdxjs/mdx's createMdxAstCompiler. | ||
ref: https://github.com/mdx-js/mdx/blob/510bae2580958598ae29047bf755b1a2ea26cf7e/packages/mdx/md-ast-to-mdx-ast.js | ||
I considered the possibility of using createMdxAstCompiler rather than remark-mdx on its own. | ||
however, crucially, we do NOT want to transform our AST into a MDXAST, we ONLY want to | ||
transform custom component nodes (ie HTML that is really JSX) into JSX nodes. | ||
So it felt duplicative, but necessary, to copypasta this utility in to meet our needs. | ||
*/ | ||
|
||
const visit = require('unist-util-visit') | ||
const { isComment, getCommentContents } = require('@mdx-js/util') | ||
|
||
module.exports = (_options) => (tree) => { | ||
visit(tree, 'jsx', (node) => { | ||
if (isComment(node.value)) { | ||
node.type = 'comment' | ||
node.value = getCommentContents(node.value) | ||
} | ||
}) | ||
|
||
return tree | ||
} |