@mdx-js/mdx
Advanced tools
Comparing version 0.5.0 to 0.7.0-0
137
index.js
const unified = require('unified') | ||
const toMDAST = require('remark-parse') | ||
const emoji = require('remark-emoji') | ||
const squeeze = require('remark-squeeze-paragraphs') | ||
const images = require('remark-images') | ||
const toHAST = require('mdast-util-to-hast') | ||
const toMDXAST = require('@mdx-js/mdxast') | ||
const mdxAstToMdxHast = require('./mdx-ast-to-mdx-hast') | ||
const mdxHastToJsx = require('./mdx-hast-to-jsx') | ||
function renderer(options) { | ||
this.Compiler = node => { | ||
const handlers = { | ||
// `inlineCode` gets passed as `code` by the HAST transform. | ||
// This makes sure it ends up being `inlineCode` | ||
inlineCode(h, node) { | ||
return Object.assign({}, node, { | ||
type: 'element', | ||
tagName: 'inlineCode', | ||
children: [ | ||
{ | ||
type: 'text', | ||
value: node.value | ||
} | ||
] | ||
}) | ||
}, | ||
import(h, node) { | ||
return Object.assign({}, node, { | ||
type: 'import' | ||
}) | ||
}, | ||
export(h, node) { | ||
return Object.assign({}, node, { | ||
type: 'export' | ||
}) | ||
}, | ||
jsx(h, node) { | ||
return Object.assign({}, node, { | ||
type: 'jsx' | ||
}) | ||
} | ||
} | ||
function createMdxAstCompiler(options = {}) { | ||
const mdPlugins = options.mdPlugins || [] | ||
const hast = toHAST(node, { | ||
handlers | ||
}) | ||
options.blocks = options.blocks || ['[a-z]+(\\.){0,1}[a-z]'] | ||
const walk = node => { | ||
let children = '' | ||
const fn = unified() | ||
.use(toMDAST, options) | ||
.use(images, options) | ||
.use(squeeze, options) | ||
if (node.type === 'root') { | ||
const importNodes = node.children | ||
.filter(node => node.type === 'import') | ||
.map(walk) | ||
.join('\n') | ||
const exportNodes = node.children | ||
.filter(node => node.type === 'export') | ||
.map(walk) | ||
.join('\n') | ||
const otherNodes = node.children | ||
.filter(node => node.type !== 'import' && node.type !== 'export') | ||
.map(walk) | ||
.join('') | ||
return ( | ||
importNodes + | ||
'\n' + | ||
exportNodes + | ||
'\n' + | ||
`export default ({components}) => <MDXTag name="wrapper">${otherNodes}</MDXTag>` | ||
) | ||
} | ||
mdPlugins.forEach(plugin => fn.use(plugin, options)) | ||
// recursively walk through children | ||
if (node.children) { | ||
children = node.children.map(walk).join('') | ||
} | ||
fn.use(toMDXAST, options).use(mdxAstToMdxHast, options) | ||
if (node.type === 'element') { | ||
// This makes sure codeblocks can hold code and backticks | ||
if (node.tagName === 'code') { | ||
children = | ||
'{`' + children.replace(/`/g, '\\`').replace(/\$/g, '\\$') + '`}' | ||
} | ||
return fn | ||
} | ||
return `<MDXTag name="${ | ||
node.tagName | ||
}" components={components} props={${JSON.stringify( | ||
node.properties | ||
)}}>${children}</MDXTag>` | ||
} | ||
if ( | ||
node.type === 'text' || | ||
node.type === 'import' || | ||
node.type === 'export' || | ||
node.type === 'jsx' | ||
) { | ||
return node.value | ||
} | ||
} | ||
const result = walk(hast) | ||
return result | ||
async function compile(mdx, options = {}) { | ||
// TODO: v1 change | ||
// For now let's also default old plugins key to hastPlugins so this | ||
// api change isn't breaking until v1. | ||
const hastPlugins = options.hastPlugins || options.plugins || [] | ||
if (options.plugins) { | ||
console.log( | ||
'MDX DEPRECATION: options.plugins is no longer supported please see the latest plugin api docs' | ||
) | ||
console.log('https://github.com/mdx-js/mdx#options') | ||
} | ||
} | ||
module.exports = function(mdx, options = {}) { | ||
options.blocks = options.blocks || ['[a-z]+(\\.){0,1}[a-z]'] | ||
const plugins = options.plugins || [] | ||
const compilers = options.compilers || [] | ||
const fn = unified() | ||
.use(toMDAST, options) | ||
.use(emoji, options) | ||
.use(images, options) | ||
.use(squeeze, options) | ||
.use(toMDXAST, options) | ||
const fn = createMdxAstCompiler(options) | ||
plugins.forEach(plugins => fn.use(plugins, options)) | ||
hastPlugins.forEach(plugin => fn.use(plugin, options)) | ||
fn.use(options.renderer || renderer, options) | ||
fn.use(mdxHastToJsx, options) | ||
compilers.forEach(compiler => fn.use(compiler, options)) | ||
return fn.processSync(mdx).contents | ||
const { contents } = await fn.process(mdx) | ||
return contents | ||
} | ||
module.exports = compile | ||
exports = compile | ||
exports.createMdxAstCompiler = createMdxAstCompiler | ||
exports.default = compile |
{ | ||
"name": "@mdx-js/mdx", | ||
"version": "0.5.0", | ||
"version": "0.7.0-0", | ||
"license": "MIT", | ||
@@ -11,3 +11,3 @@ "repository": "mdx-js/mdx", | ||
"scripts": { | ||
"test": "ava -v" | ||
"test": "jest" | ||
}, | ||
@@ -22,6 +22,5 @@ "keywords": [ | ||
"dependencies": { | ||
"@mdx-js/mdxast": "^0.4.0", | ||
"@mdx-js/mdxast": "^0.7.0-0", | ||
"mdast-util-to-hast": "^3.0.0", | ||
"remark-emoji": "^2.0.1", | ||
"remark-images": "^0.4.0", | ||
"remark-images": "^0.7.0-0", | ||
"remark-parse": "^5.0.0", | ||
@@ -33,4 +32,8 @@ "remark-squeeze-paragraphs": "^3.0.1", | ||
"devDependencies": { | ||
"ava": "^0.25.0" | ||
"@babel/core": "^7.0.0-beta.42", | ||
"@babel/plugin-syntax-jsx": "^7.0.0-beta.42", | ||
"hast-util-select": "^1.0.1", | ||
"jest": "^22.4.3", | ||
"request-image-size": "^2.1.0" | ||
} | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
7
3596
5
41
+ Added@mdx-js/mdxast@0.7.2(transitive)
+ Addedremark-images@0.7.4(transitive)
- Removedremark-emoji@^2.0.1
- Removed@mdx-js/mdxast@0.4.0(transitive)
- Removed@types/unist@2.0.11(transitive)
- Removedemoticon@3.2.0(transitive)
- Removedlodash@4.17.21(transitive)
- Removednode-emoji@1.11.0(transitive)
- Removedremark-emoji@2.2.0(transitive)
- Removedremark-images@0.4.0(transitive)
- Removedunist-util-is@4.1.0(transitive)
- Removedunist-util-visit@2.0.3(transitive)
- Removedunist-util-visit-parents@3.1.1(transitive)
Updated@mdx-js/mdxast@^0.7.0-0
Updatedremark-images@^0.7.0-0