San*_*nta 2 javascript static-site-generation astrojs
我想.md使用 Astro 在单个页面上呈现多个文件的内容。
我有这个文件结构:
pages/
items/
item-1.md
item-2.md
item-3.md
// etc
index.astro
Run Code Online (Sandbox Code Playgroud)
item-1.md看起来像这样:
---
title = 'Item 1'
---
Item 1 main text
Run Code Online (Sandbox Code Playgroud)
并在index.astro:
---
const items = await Astro.glob([
'./items/*.md',
]);
---
<html lang="en">
<body>
{items.map((item) =>
item.Content
)}
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
我得到的结果是:
[object Object][object Object][object Object]
Run Code Online (Sandbox Code Playgroud)
而不是预期的:
Item 1 main text
Item 2 main text
Item 3 main text
Run Code Online (Sandbox Code Playgroud)
如何才能使 Markdown 计算出的输出 HTML 出现在页面上?
item.Content是一个 Astro 组件,所以在你的index.astro文件中你需要像这样渲染它:
---
const items = await Astro.glob('./items/*.md');
---
<html lang="en">
<body>
{items.map((item) =>
<item.Content />
)}
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
您还可以在地图中对其进行解构,但它们在功能上是等效的:
{items.map(({ Content }) =>
<Content />
)}
Run Code Online (Sandbox Code Playgroud)