babel-plugin-markdown-in-jsx
Advanced tools
Comparing version 1.0.3 to 1.0.4
import * as React from "react"; | ||
declare function Markdown(props: { children?: React.ReactNode }): React.ReactElement<any>; | ||
declare function Markdown(props: { children?: React.ReactNode, inline?: boolean }): React.ReactElement<any>; | ||
export default Markdown; |
48
index.js
@@ -77,3 +77,3 @@ const _ = require('lodash'); | ||
before, | ||
`<!--${replacement.placeholder}-->`, | ||
`$${replacement.placeholder}$`, | ||
after | ||
@@ -112,2 +112,48 @@ ].join(''); | ||
} | ||
}, | ||
JSXText(path) { | ||
const strs = path.node.value.split(/(\$bpjm\d+\$)/); | ||
if (strs.length > 1) | ||
{ | ||
path.replaceWithMultiple( | ||
strs.map((text) => { | ||
if (/^\$bpjm\d+\$$/.test(text)) { | ||
for (let index = 0; index < replacements.length; index++) { | ||
const placeholder = replacements[index].placeholder; | ||
if (text === '$' + placeholder + '$') { | ||
return replacements[index].node; | ||
} | ||
} | ||
} | ||
return babelTypes.jsxText(text) | ||
}) | ||
); | ||
} | ||
}, | ||
JSXAttribute(path) { | ||
if (babelTypes.isStringLiteral(path.node.value)) | ||
{ | ||
const strs = path.node.value.value.split(/(\$bpjm\d+\$)/); | ||
if (strs.length > 1) | ||
{ | ||
path.get('value').replaceWith(babelTypes.JSXExpressionContainer( | ||
strs.map((text) => { | ||
if (/^\$bpjm\d+\$$/.test(text)) { | ||
for (let index = 0; index < replacements.length; index++) { | ||
const placeholder = replacements[index].placeholder; | ||
if (text === '$' + placeholder + '$') { | ||
if (babelTypes.isJSXExpressionContainer(replacements[index].node)) { | ||
return replacements[index].node.expression; | ||
} | ||
return replacements[index].node; | ||
} | ||
} | ||
} | ||
return babelTypes.stringLiteral(text) | ||
}).reduce((pre, value) => { | ||
return babelTypes.binaryExpression('+', pre, value); | ||
}) | ||
)); | ||
} | ||
} | ||
} | ||
@@ -114,0 +160,0 @@ }); |
{ | ||
"name": "babel-plugin-markdown-in-jsx", | ||
"version": "1.0.3", | ||
"version": "1.0.4", | ||
"description": "Transform Markdown inside JSX to DOM elements at compile time.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -118,2 +118,16 @@ const babel = require('@babel/core'); | ||
test('with variable img href', () => { | ||
const renderBody = ` | ||
const h = 'http://example.com'; | ||
return ( | ||
<Markdown> | ||
# Title | ||
![](123{h}) | ||
</Markdown> | ||
)`; | ||
expect(transform(renderBody)).toMatchSnapshot(); | ||
}); | ||
test('bulitin component proxy', () => { | ||
@@ -175,2 +189,2 @@ const ReactDOMServer = require('react-dom/server'); | ||
expect(code).toMatchSnapshot(); | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
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
339038
1948