remcohaszing / remark-mdx-code-meta Goto Github PK
View Code? Open in Web Editor NEWA remark MDX plugin for using markdown code block metadata
License: MIT License
A remark MDX plugin for using markdown code block metadata
License: MIT License
I am trying it on my Next.js app โ https://github.com/deadcoder0904/better-code-blocks but I'm getting an error:
./src/pages/tutorial/better-code-blocks/index.mdx
SyntaxError: JSX value should be either an expression or a quoted JSX text (1:11)
const path = require('path')
const { createLoader } = require('simple-functional-loader')
const visit = require('unist-util-visit')
const withBundleAnalyzer = require('@next/bundle-analyzer')({
enabled: process.env.ANALYZE === 'true',
})
const { remarkMdxCodeMeta } = require('remark-mdx-code-meta')
const rehypeHighlightCode = require('./rehype/highlight-code')
const rehypeMetaAttribute = require('./rehype/meta-attribute')
// const { withSyntaxHighlighting } = require('./remark/withSyntaxHighlighting')
// const withCodeSamples = require('./remark/withCodeSamples')
module.exports = withBundleAnalyzer({
future: {
webpack5: true,
},
pageExtensions: ['js', 'jsx', 'ts', 'tsx', 'md', 'mdx'],
experimental: {
modern: true,
},
redirects: async () => {
return require('./redirects.json')
},
webpack: (config, options) => {
config.module.rules.push({
test: /\.(svg|png|jpe?g|gif|mp4)$/i,
use: [
{
loader: 'file-loader',
options: {
publicPath: '/_next',
name: 'static/media/[name].[hash].[ext]',
},
},
],
})
const mdx = [
options.defaultLoaders.babel,
{
loader: '@mdx-js/loader',
options: {
remarkPlugins: [
/*withCodeSamples, withSyntaxHighlighting*/
remarkMdxCodeMeta,
],
rehypePlugins: [rehypeMetaAttribute, rehypeHighlightCode],
},
},
]
config.module.rules.push({
test: /\.mdx$/,
oneOf: [
{
resourceQuery: /preview/,
use: [
...mdx,
createLoader(function (src) {
if (src.includes('<!-- more -->')) {
const [preview] = src.split('<!-- more -->')
return this.callback(null, preview)
}
const [preview] = src.split('<!--/excerpt-->')
return this.callback(null, preview.replace('<!--excerpt-->', ''))
}),
],
},
{
resourceQuery: /rss/,
use: [
...mdx,
createLoader(function (src) {
return this.callback(null, src)
}),
],
},
{
include: [path.resolve(__dirname, 'src/pages/essay')],
use: [
...mdx,
createLoader(function (src) {
const content = [
'import Essay from "@/components/Essay"',
'export { getStaticProps } from "@/utils/essay/getStaticProps"',
src,
'export default (props) => <Essay meta={meta} {...props} />',
].join('\n')
if (content.includes('<!-- more -->')) {
return this.callback(null, content.split('<!-- more -->').join('\n'))
}
return this.callback(null, content.replace(/<!--excerpt-->.*<!--\/excerpt-->/s, ''))
}),
],
},
{
include: [path.resolve(__dirname, 'src/pages/tutorial')],
use: [
...mdx,
createLoader(function (src) {
const content = [
'import Tutorial from "@/components/Tutorial"',
'export { getStaticProps } from "@/utils/tutorial/getStaticProps"',
src,
'export default (props) => <Tutorial meta={meta} {...props} />',
].join('\n')
if (content.includes('<!-- more -->')) {
return this.callback(null, content.split('<!-- more -->').join('\n'))
}
return this.callback(null, content.replace(/<!--excerpt-->.*<!--\/excerpt-->/s, ''))
}),
],
},
],
})
if (!options.dev && options.isServer) {
const originalEntry = config.entry
config.entry = async () => {
const entries = { ...(await originalEntry()) }
entries['./scripts/build-rss'] = './scripts/build-rss.js'
entries['./scripts/build-sitemap'] = './scripts/build-sitemap.js'
return entries
}
}
return config
},
})
Hi there,
Thanks for the package!
I'm currently trying to make this work with a custom react webpack setup to expose meta data on code blocks.
This is what my webpack config looks like:
const codeMeta = require('remark-mdx-code-meta');
[...]
{
test: /\.mdx?$/,
use: [
{
loader: 'xdm/webpack.cjs',
options: {
providerImportSource: '@mdx-js/react',
remarkPlugins: [codeMeta.remarkMdxCodeMeta],
},
},
],
},
[...]
While it compiles, I can't seem to get the actual plugin to work.
This is my React file:
import React from 'react'
import { MDXProvider } from '@mdx-js/react';
import Test from './test.mdx'
const components = {
code: (p) => {
console.log(p);
return <div />
}
}
export const Preview = () => (
<MDXProvider components={components}>
<Test />
</MDXProvider>
)
and my test.mdx
looks like:
# Test
```js render
<button>Test</button>
```
I'm expecting to see the render
attribute in the props passed to the code
element but no luck.
Can you tell me if I'm missing anything?
Thanks a lot!
Version 2.0.0 README example:
import { remarkMdxCodeMeta } from "remark-mdx-code-meta";
fails with:
SyntaxError: The requested module 'remark-mdx-code-meta' does not provide an export named 'remarkMdxCodeMeta'
Solution:
import remarkMdxCodeMeta from "remark-mdx-code-meta";
Hi there,
Thanks for this, it works great for passing down the metadata as props, but I have hit a problem: It breaks syntax highlighting with rehype-highlight
.
import { compile } from '@mdx-js/mdx'
import codeMeta from 'remark-mdx-code-meta'
import rehypeHighlight from 'rehype-highlight'
compile(md, {
outputFormat: 'function-body',
remarkPlugins: [
codeMeta, // adding this breaks syntax highlighting
],
rehypePlugins: [
[rehypeHighlight, { plainText: ['http'] }],
]
})
If you had any pointers on how to make this plugin play nice with rehype-highlight I would very much appreciate hearing about them ๐
thank you
joost
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.