Skip to content

Can't access theme & showLineNumbers directly in pre tag in MDXComponents while using MDX & Rehype Plugins on a Next.js blog? #58

Discussion options

You must be logged in to vote

This did the trick:

meta-attribute.js

const visit = require('unist-util-visit')

var re = /\b([-\w]+)(?:=(?:"([^"]*)"|'([^']*)'|([^"'\s]+)))?/g

module.exports = (options = {}) => {
	return (tree) => {
		visit(tree, 'element', visitor)
	}

	function visitor(node, index, parentNode) {
		let match

		if (node.tagName === 'pre') {
			const code = node.children.find((d) => d.tagName === 'code')

			if (code.properties.metastring) {
				code.properties.metastring.split(' ').forEach((item) => {
					const [metaKey, metaValue] = item.split('=')
					node.properties[metaKey] = metaValue || ''
				})
			}
		}
	}
}

@wooorm Any way I can remove my custom properties like theme from DOM?

Replies: 2 comments 22 replies

Comment options

You must be logged in to vote
17 replies
@deadcoder0904
Comment options

@deadcoder0904
Comment options

@wooorm
Comment options

@deadcoder0904
Comment options

@wooorm
Comment options

Comment options

You must be logged in to vote
5 replies
@wooorm
Comment options

@deadcoder0904
Comment options

@wooorm
Comment options

@deadcoder0904
Comment options

@wooorm
Comment options

Answer selected by deadcoder0904
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants