英文:
How to use Markdoc in React + TypeScript + Vite app
问题
Document.tsx
import React from "react";
import Markdoc from "@markdoc/markdoc";
const config = {};
interface DocumentProps {
source: string;
}
export function Document({ source }: DocumentProps) {
const ast = Markdoc.parse(source);
const content = Markdoc.transform(ast, config);
return Markdoc.renderers.react(content, React);
}
export default Document;
Home.tsx
import { Document } from "@containers";
export function Home() {
return (
<div>
<Document
source={`
# Hello world.
> My first Markdoc page
`}
/>
</div>
);
}
export default Home;
I wanna see markdown but it's being rendered as normal text # Hello world. > My first Markdoc page
.
There were some tutorials and guides for Next.js + Markdoc app, but they all didn't work for my app.
<details>
<summary>英文:</summary>
Document.tsx
import React from "react";
import Markdoc from "@markdoc/markdoc";
const config = {};
interface DocumentProps {
source: string;
}
export function Document({ source }: DocumentProps) {
const ast = Markdoc.parse(source);
const content = Markdoc.transform(ast, config);
return Markdoc.renderers.react(content, React);
}
export default Document;
Home.tsx
import { Document } from "@containers";
export function Home() {
return (
<div>
<Document
source={
}
# Hello world.
> My first Markdoc page
/>
</div>
);
}
export default Home;
I wanna see markdown but it's being rendered as a normal text `# Hello world. > My first Markdoc page`.
There were some tutorials and guides for Next.js + Markdoc app, but they all didn't work for my app.
</details>
# 答案1
**得分**: 0
Markdown对缩进很敏感,你的`source`部分缩进不正确。推荐使用空行分隔标题、段落引用等。
```javascript
{ Document } from "@containers";
export function Home() {
return (
<div>
<Document
source={`# 你好,世界。
> 我的第一个Markdoc页面`}
/>
</div>
);
}
export default Home;
英文:
Markdown is sensitive for indentation and your source
has incorrect indentation. It's also recommended to separate headings, paragraph quotes etc. with a empty line.
import { Document } from "@containers";
export function Home() {
return (
<div>
<Document
source={`# Hello world.
> My first Markdoc page`}
/>
</div>
);
}
export default Home;
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论