如何在 React + TypeScript + Vite 应用中使用 Markdoc

huangapple go评论64阅读模式
英文:

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.
&gt; My first Markdoc page
}
/>
</div>
);
}

export default Home;


I wanna see markdown but it&#39;s being rendered as a normal text `# Hello world. &gt; My first Markdoc page`.

There were some tutorials and guides for Next.js + Markdoc app, but they all didn&#39;t work for my app.

</details>


# 答案1
**得分**: 0

Markdown对缩进很敏感,你的`source`部分缩进不正确。推荐使用空行分隔标题、段落引用等。

```javascript
{ Document } from &quot;@containers&quot;;

export function Home() {
  return (
    &lt;div&gt;
      &lt;Document
        source={`# 你好,世界。

&gt; 我的第一个Markdoc页面`}
      /&gt;
    &lt;/div&gt;
  );
}

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 &quot;@containers&quot;;

export function Home() {
  return (
    &lt;div&gt;
      &lt;Document
        source={`# Hello world.

&gt; My first Markdoc page`}
      /&gt;
    &lt;/div&gt;
  );
}

export default Home;

huangapple
  • 本文由 发表于 2023年5月10日 12:58:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/76215035.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定