#help me understand Astro - how to get markdown files to display

24 messages · Page 1 of 1 (latest)

red burrow
#

hi

so I have index.astro page

on it I created a main card, for content.

Inside I put few tabs.

I want to be able to have markdown files and include them as each tab content.

how would I do that?

exotic token
#

are you using content collections?

red burrow
#

help me understand Astro - how to get markdown files to display

#

how

exotic token
red burrow
#

thanks.

#

so i can name this any folder name

#

and then link to that folder, right?

exotic token
random quest
#

You can import markdown files from anywhere in your project, and use them as components.

---
// Import a single file
import * as myPost from '../pages/post/my-post.md';

// Import multiple files with Astro.glob
const posts = await Astro.glob('../markdown/*.md');
---

You can use the Content component to render that markdown, and access the frontmatter using frontmatter.

// src/pages/index.astro
---
const tabs = await Astro.glob('../markdown/tabs/*.md');
---

{tabs.map({frontmatter, Content} => (
  <>
    <h2>{frontmatter.title}</h2>
    <Content/>
  </>
)}
#

And yes! Content collections will be a lot nicer if you're willing to set it up

red burrow
random quest
#

Yes, in this example. But if you don't want to use frontmatter, you could also grab the headings if you use h1 headings. However, for content collections, frontmatter works much better (and is much more flexible)

red burrow
#

so my markdown files should be in content directory ?

random quest
#

if you're using content collections yes. src/content/ is reserved for content collections, so if you're not using them, use something else

red burrow
#

now I created

#

src>content>tabs

#

and put them inside

#

how to reach those?

random quest
idle pier
#

Hello, can I ask something about markdown rendering here?

#

I don't know where to ask actually and I'm new

random quest
#

Hi @idle pier! It's probably best to open a new #1019713903481081876 post, but yes, we'd be happy to answer