如何在 React.js 中使用 react-markdown 嵌入 YouTube 视频?

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

How can I embed a youtube video in reactjs markdown with react-markdown

问题

我是ReactJS的新手。我在使用`react-markdown`时遇到一些问题。我想使用markdown嵌入视频。

这是我问题的简单示例。

// 导入 react-markdown
import ReactMarkdown from 'react-markdown';

// markdown 字符串
const md =
你好,嵌入来自YouTube的视频。
{/* 我如何在这里嵌入视频 */}

// 使用 markdown
<ReactMarkdown
linkTarget="_blank"

{md}


<details>
<summary>英文:</summary>

I am new with ReactJS. I have some problems with `react-markdown`. I want to embed a video using markdown.

Here is my simple example of my problem.

// import react-markdown
import ReactMarkdown from 'react-markdown'

// markdown string
const md =
Hello video embeded fro youtube.
{/* how can I embed a video here */}

// use markdown
<ReactMarkdown
linkTarget="_blank"
> {md} </ReactMarkdown>


</details>


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

You could do this by writing a **iframe** inside the markdown.

For this to work you'll need to install [rehype-raw](https://github.com/rehypejs/rehype-raw) which is a plugin you can use with `ReactMarkdown`.

```sh
yarn add rehype-raw
# or
npm install rehype-raw --save

Then you can use it like this

import rehypeRaw from "rehype-raw";

const md = `Hello this video is embeded from Youtube!

<iframe width="560" height="315" src="https://www.youtube.com/embed/JvwW5Tsf97c" />`;

return (
  <ReactMarkdown rehypePlugins={[rehypeRaw]} linkTarget="_blank">
    {md}
  </ReactMarkdown>
);

Note that you'll need a embed Youtube link and not a ?watch= link.

英文:

You could do this by writing a iframe inside the markdown.

For this to work you'll need to install rehype-raw which is a plugin you can use with ReactMarkdown.

yarn add rehype-raw
# or
npm install rehype-raw --save

Then you can use it like this

import rehypeRaw from &quot;rehype-raw&quot;;

const md = `Hello this video is embeded from Youtube!

&lt;iframe width=&quot;560&quot; height=&quot;315&quot; src=&quot;https://www.youtube.com/embed/JvwW5Tsf97c&quot; /&gt;`;

return (
  &lt;ReactMarkdown rehypePlugins={[rehypeRaw]} linkTarget=&quot;_blank&quot;&gt;
    {md}
  &lt;/ReactMarkdown&gt;
);

Note that you'll need a embed Youtube link and not a ?watch= link.

答案2

得分: 0

<!-- 开始代码段JavaScript 隐藏false 控制台true Babeltrue -->

<!-- 语言lang-js -->
const App = () => {
  const data = `lorem &lt;b onmouseover=&quot;alert(&#39;mouseover&#39;);&quot;&gt;ipsum&lt;/b&gt;`;

  return (
    <div
      dangerouslySetInnerHTML={{__html: data}}
    />
  );
}

export default App;

const App = () => {
  const data = `lorem ipsum &lt;img src=&quot;&quot; onerror=&quot;alert(&#39;message&#39;);&quot; />`;

  return (
    <div
      dangerouslySetInnerHTML={{__html: data}}
    />
  );
}

export default App;

<!-- 语言lang-html -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>

<!-- 结束代码段 -->
英文:

<!-- begin snippet: js hide: false console: true babel: true -->

<!-- language: lang-js -->

    const App = () =&gt; {
  const data = `lorem &lt;b onmouseover=&quot;alert(&#39;mouseover&#39;);&quot;&gt;ipsum&lt;/b&gt;`;

  return (
    &lt;div
      dangerouslySetInnerHTML={{__html: data}}
    /&gt;
  );
}

export default App;


const App = () =&gt; {
  const data = `lorem ipsum &lt;img src=&quot;&quot; onerror=&quot;alert(&#39;message&#39;);&quot; /&gt;`;

  return (
    &lt;div
      dangerouslySetInnerHTML={{__html: data}}
    /&gt;
  );
}

export default App;

<!-- language: lang-html -->

&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js&quot;&gt;&lt;/script&gt;
&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js&quot;&gt;&lt;/script&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年2月6日 14:41:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/75358080.html
匿名

发表评论

匿名网友

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

确定