How to read mdx files from local files? What is leerob.io's 'contentlayer/generated' code?
Answered
ascee posted this in #help-forum
Answered by Asian black bear
It is made somehow by this package https://github.com/contentlayerdev/contentlayer
17 Replies
Asian black bear
in tsconfig.json we can see that it is a
paths alias to something generated at build (?) time "paths": {
"contentlayer/generated": ["./.contentlayer/generated"]
},Asian black bear
It is made somehow by this package https://github.com/contentlayerdev/contentlayer
Answer
Asian black bear
It seems to maybe be indexing the MDX files to aid in navigation?
It uses the actual Next built-in MDX support to finally render it though https://github.com/leerob/leerob.io/blob/3038b518f3e6746a9e6f7bb790d80ece37b484f8/app/blog/%5Bslug%5D/page.tsx#L110C10-L110C36
This is definitely a pretty complicated way if you want do just load some static MDX files, but a pretty good example of how to do a blog with metadata from the MDX posts like
---
title: 'Programming Portfolios'
publishedAt: '2017-02-24'
summary: 'Tips for creating a portfolio that stands out.'
---@Asian black bear It uses the actual Next built-in MDX support to finally render it though https://github.com/leerob/leerob.io/blob/3038b518f3e6746a9e6f7bb790d80ece37b484f8/app/blog/%5Bslug%5D/page.tsx#L110C10-L110C36
it actually still uses contentlayer to render... https://github.com/leerob/leerob.io/blob/main/app/components/mdx.tsx#L102
see contentlayer docs for how it works: [[docs](https://contentlayer.dev/docs/environments/nextjs-dcf8e39e)] [[nextjs example](https://contentlayer.dev/examples/nextjs)]
@Asian black bear This is definitely a pretty complicated way if you want do just load some static MDX files, but a pretty good example of how to do a blog with metadata from the MDX posts like
---
title: 'Programming Portfolios'
publishedAt: '2017-02-24'
summary: 'Tips for creating a portfolio that stands out.'
---
i would argue its actually easier as you don't need to implement your own file api
but it might require some more understanding to initially work it out (for markdown, id say better, but just pages with jsx, harder)
@riský it actually still uses contentlayer to render... https://github.com/leerob/leerob.io/blob/main/app/components/mdx.tsx#L102
Asian black bear
Yes, I concur. Not sure how I missed it.
in either case, try the built in Next MDX support first, it is pretty good
@ascee are you there/ do you understand what minabot and I shared...
asceeOP
Absolutely sorry for not replying! Was Trying these as you shared thank you so much
ahh yeah thats understandable
(i just wanted to make sure you didn't leave the server or give up like some do)
contentlayer is very good when you need typed metadata for mdx files
@riský ahh yeah thats understandable
asceeOP
we holding strong here