Style ul>li
using MDX Components?
#1325
-
I am making a blog using However, my blog doesn't contain https://github.com/tailwindlabs/tailwindcss-typography/ which comes with default styles So I want to apply default styles for I'm not sure how to do that while using I can do basic stuff like: export const MDXComponents = {
h1: H1,
h2: H2,
h3: H3,
h4: H4,
h5: H5,
h6: H6,
p: P,
hr: HR,
a: A,
strong: Strong,
img: Img,
pre: Pre,
'pre.code': PreCode,
} Here's the code for import { HTMLAttributes } from 'react'
type Props = {
className: string
props: HTMLAttributes<HTMLPreElement>
}
export const PreCode = ({ className = '', ...props }: Props) => (
<code className={`${className} text-gray-200`} {...props} />
) However, when I want to do something for Basically, I want to style the following CSS (including {
'ul > li': {
position: 'relative',
},
'ul > li::before': {
content: '""',
position: 'absolute',
backgroundColor: defaultTheme.colors.gray[300],
borderRadius: '50%',
},
} And the only way I know to do it without polluting global CSS is to use So how do I do that? |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 4 replies
-
A few options. If you want to add a css class only to an |
Beta Was this translation helpful? Give feedback.
-
Beta Was this translation helpful? Give feedback.
-
I changed the above code a little bit. I replaced: case 'inlineCode': {
return '`' + l.value + '`'
} with case 'inlineCode': {
return `<code>${l.value}</code>`
} I think the issue is <li class="">Automatic Image Optimization -> <code><img class="full-bleed my-16"></code> component allows optimizing & resizing images before serving them to the user</li> And if I do <li class="">Automatic Image Optimization -> <code><image1></image1></code> component allows optimizing & resizing images before serving them to the user</li> So I think it treats anything in square brackets |
Beta Was this translation helpful? Give feedback.
-
Yay, I found the solution. Had to use list.jsconst visit = require('unist-util-visit')
const toHast = require('mdast-util-to-hast')
const toHtml = require('hast-util-to-html')
module.exports.list = () => {
return (tree) => {
visit(tree, 'list', (node) => {
const str = []
str.push(`<ul class="">`)
for (let i = 0; i < node.children.length; i++) {
for (let j = 0; j < node.children[i].children.length; j++) {
const k = node.children[i].children[j].children.map((l) => toHtml(toHast(l))).join('')
str.push(`<li class="">${k}</li>`)
}
}
str.push(`</ul>`)
if (node.lang !== null) {
node.type = 'html'
node.value = str.filter(Boolean).join('')
}
})
}
} I have one last question. Here, I have They're in TS & use JSX. src/components/mdx/index.tsimport { Code } from '@/components/mdx/Code'
export const MDXComponents = {
inlineCode: Code,
} src/components/mdx/Code.tsximport { HTMLAttributes } from 'react'
export const Code = ({ className = '', ...props }: HTMLAttributes<HTMLElement>) => (
<code
className={`${className} p-1 overflow-x-auto bg-gray-900 text-blue-gray-200 rounded-md text-base`}
{...props}
/>
) Can I use them in Other solution I think is I have to write a bunch of if-else to see if an |
Beta Was this translation helpful? Give feedback.
Yay, I found the solution. Had to use
toHtml(toHast(l))
. I wished there wasmdast-util-to-html
directly.list.js