Skip to content

Commit

Permalink
Remove the className prop entirely
Browse files Browse the repository at this point in the history
  • Loading branch information
remcohaszing committed Nov 17, 2023
1 parent ea2fa56 commit b2ce3ee
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 47 deletions.
24 changes: 3 additions & 21 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
/**
* @typedef {import('hast').Element} Element
* @typedef {import('hast').ElementContent} ElementContent
* @typedef {import('hast').Nodes} Nodes
* @typedef {import('hast').Parents} Parents
* @typedef {import('hast').Root} Root
* @typedef {import('hast-util-to-jsx-runtime').Components} JsxRuntimeComponents
Expand Down Expand Up @@ -47,8 +46,6 @@
* cannot combine w/ `disallowedElements`.
* @property {string | null | undefined} [children]
* Markdown.
* @property {string | null | undefined} [className]
* Wrap in a `div` with this class name.
* @property {Components | null | undefined} [components]
* Map tag names to components.
* @property {ReadonlyArray<string> | null | undefined} [disallowedElements]
Expand Down Expand Up @@ -150,7 +147,6 @@ export function Markdown(options) {
const allowedElements = options.allowedElements
const allowElement = options.allowElement
const children = options.children || ''
const className = options.className
const components = options.components
const disallowedElements = options.disallowedElements
const rehypePlugins = options.rehypePlugins || emptyPlugins
Expand Down Expand Up @@ -205,21 +201,7 @@ export function Markdown(options) {
}

const mdastTree = processor.parse(file)
/** @type {Nodes} */
let hastTree = processor.runSync(mdastTree, file)

// Wrap in `div` if there’s a class name.
if (className) {
hastTree = {
type: 'element',
tagName: 'div',
properties: {className},
// Assume no doctypes.
children: /** @type {Array<ElementContent>} */ (
hastTree.type === 'root' ? hastTree.children : [hastTree]
)
}
}
const hastTree = processor.runSync(mdastTree, file)

visit(hastTree, transform)

Expand Down Expand Up @@ -267,8 +249,8 @@ export function Markdown(options) {
let remove = allowedElements
? !allowedElements.includes(node.tagName)
: disallowedElements
? disallowedElements.includes(node.tagName)
: false
? disallowedElements.includes(node.tagName)
: false

if (!remove && allowElement && typeof index === 'number') {
remove = !allowElement(node, index, parent)
Expand Down
26 changes: 0 additions & 26 deletions test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,32 +61,6 @@ test('react-markdown', async function (t) {
}, /Unexpected `allowDangerousHtml` prop, remove it/)
})

await t.test('should support `className`', function () {
assert.equal(
asHtml(<Markdown children="a" className="md" />),
'<div class="md"><p>a</p></div>'
)
})

await t.test('should support `className` (if w/o root)', function () {
assert.equal(
asHtml(
<Markdown children={'a'} className="b" rehypePlugins={[plugin]} />
),
'<div class="b"></div>'
)

function plugin() {
/**
* @returns {Root}
*/
return function () {
// @ts-expect-error: check how non-roots are handled.
return {type: 'comment', value: 'things!'}
}
}
})

await t.test('should support a block quote', function () {
assert.equal(
asHtml(<Markdown children="> a" />),
Expand Down

0 comments on commit b2ce3ee

Please sign in to comment.