Skip to content

Commit

Permalink
Change to export functions that return extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Jan 31, 2022
1 parent 93b0c73 commit d28aad1
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 22 deletions.
25 changes: 16 additions & 9 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,21 @@ import {
import {mdxJsxFromMarkdown, mdxJsxToMarkdown} from 'mdast-util-mdx-jsx'
import {mdxjsEsmFromMarkdown, mdxjsEsmToMarkdown} from 'mdast-util-mdxjs-esm'

/** @type {FromMarkdownExtension[]} */
export const mdxFromMarkdown = [
mdxExpressionFromMarkdown,
mdxJsxFromMarkdown,
mdxjsEsmFromMarkdown
]
/**
*
* @return {Array<FromMarkdownExtension>}
*/
export function mdxFromMarkdown() {
return [mdxExpressionFromMarkdown, mdxJsxFromMarkdown, mdxjsEsmFromMarkdown]
}

/** @type {ToMarkdownExtension} */
export const mdxToMarkdown = {
extensions: [mdxExpressionToMarkdown, mdxJsxToMarkdown, mdxjsEsmToMarkdown]
/** @return {ToMarkdownExtension} */
export function mdxToMarkdown() {
return {
extensions: [
mdxExpressionToMarkdown,
mdxJsxToMarkdown,
mdxjsEsmToMarkdown
]
}
}
12 changes: 6 additions & 6 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,12 @@ const doc = fs.readFileSync('example.mdx')

const tree = fromMarkdown(doc, {
extensions: [mdxjs()],
mdastExtensions: [mdxFromMarkdown]
mdastExtensions: [mdxFromMarkdown()]
})

console.log(tree)

const out = toMarkdown(tree, {extensions: [mdxToMarkdown]})
const out = toMarkdown(tree, {extensions: [mdxToMarkdown()]})

console.log(out)
```
Expand Down Expand Up @@ -226,13 +226,13 @@ This package exports the following identifier: `mdxFromMarkdown`,
`mdxToMarkdown`.
There is no default export.

### `mdxFromMarkdown`
### `mdxFromMarkdown()`

### `mdxToMarkdown`
### `mdxToMarkdown()`

Support MDX (or MDX.js).
The exports are respectively an extension for
[`mdast-util-from-markdown`][from-markdown] and
The exports are functions that can be called to respectively get an extension
for [`mdast-util-from-markdown`][from-markdown] and
[`mdast-util-to-markdown`][to-markdown].

There are no options.
Expand Down
14 changes: 7 additions & 7 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ test('markdown -> mdast', (t) => {
JSON.stringify(
fromMarkdown('import a from "b"', {
extensions: [mdxjs()],
mdastExtensions: [mdxFromMarkdown]
mdastExtensions: [mdxFromMarkdown()]
})
)
),
Expand Down Expand Up @@ -95,7 +95,7 @@ test('markdown -> mdast', (t) => {
t.deepEqual(
fromMarkdown('<x/>', {
extensions: [mdxjs()],
mdastExtensions: [mdxFromMarkdown]
mdastExtensions: [mdxFromMarkdown()]
}),
{
type: 'root',
Expand Down Expand Up @@ -124,7 +124,7 @@ test('markdown -> mdast', (t) => {
JSON.stringify(
fromMarkdown('{1 + 1}', {
extensions: [mdxjs()],
mdastExtensions: [mdxFromMarkdown]
mdastExtensions: [mdxFromMarkdown()]
})
)
),
Expand Down Expand Up @@ -210,7 +210,7 @@ test('mdast -> markdown', (t) => {
t.equal(
toMarkdown(
{type: 'mdxjsEsm', value: 'import a from "b"'},
{extensions: [mdxToMarkdown]}
{extensions: [mdxToMarkdown()]}
),
'import a from "b"\n',
'should support esm'
Expand All @@ -219,7 +219,7 @@ test('mdast -> markdown', (t) => {
t.equal(
toMarkdown(
{type: 'mdxJsxFlowElement', name: 'x', attributes: [], children: []},
{extensions: [mdxToMarkdown]}
{extensions: [mdxToMarkdown()]}
),
'<x/>\n',
'should support jsx'
Expand All @@ -228,7 +228,7 @@ test('mdast -> markdown', (t) => {
t.deepEqual(
toMarkdown(
{type: 'mdxFlowExpression', value: '1 + 1'},
{extensions: [mdxToMarkdown]}
{extensions: [mdxToMarkdown()]}
),
'{1 + 1}\n',
'should support expressions'
Expand All @@ -241,7 +241,7 @@ test('mdast -> markdown', (t) => {
url: 'tel:123',
children: [{type: 'text', value: 'tel:123'}]
},
{extensions: [mdxToMarkdown]}
{extensions: [mdxToMarkdown()]}
),
'[tel:123](tel:123)\n',
'should use link (resource) instead of link (auto)'
Expand Down

0 comments on commit d28aad1

Please sign in to comment.