npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

babel-plugin-markdown-in-jsx

v1.0.5

Published

Transform Markdown inside JSX to DOM elements at compile time.

Downloads

14

Readme

babel-plugin-markdown-in-jsx

这是一个 babel 插件,帮助你在 jsx 中使用 Markdown。

import * as React from 'react';
import Markdown from 'babel-plugin-markdown-in-jsx/component';
import Button from '@material-ui/core/Button';

function TestComponent() {
  return (
    <Markdown>
      # babel-plugin-markdown-in-jsx

      Transform Markdown inside `JSX` to `DOM` elements at compile time.

      <Button variant="contained">Hello World</Button>

      log(E) = {Math.log(Math.E)}
    </Markdown>
  )
}

example

Getting Started

在你的项目中打开终端,输入以下代码。

npm install --save-dev babel-plugin-markdown-in-jsx

修改 .babelrc

{
  "presets": [
    "@babel/preset-env",
    "@babel/react"
  ],
  "plugins": [
    "markdown-in-jsx"
  ]
}

首先你需要在代码中声明 Markdown 组件

const Markdown = require('babel-plugin-markdown-in-jsx/component');

也可以使用 esmodule 语法

import Markdown from 'babel-plugin-markdown-in-jsx/component';

这里声明的变量名 Markdown 是随意的,但包名必须是 babel-plugin-markdown-in-jsx/component 一字不差。

请注意,这条声明在编译后会被删除。

现在你就可以在 JSX 中使用 Markdown 组件。 组件中的文本会被作为 Markdown 编译成 HTML。 表达式和 JSX 组件会被保留在原地。

function TestComponent() {
  const imgsrc = 'img.png';
  return (
    <Markdown>
      # Markdown Hello world

      也可以插入JS表达式: log(E) = {Math.log(Math.E)}

      图片的链接也可以写变量: ![图片](http://{imgsrc})

      <div>
        这里的内容不会被编译。

        <Markdown>
          # 这里的内容会作为 Markdown 编译。
        </Markdown>

      </div>
    </Markdown>
  )
}

Inline Mode

babel-plugin-markdown-in-jsx 有一种 inline 模式。

Markdown 组件上加上 inline 属性,可以使用 inline 模式编译。

<Markdown>
  123
</Markdown>
编译后
<div>
  <p>123</p>
</div>
=============================
<Markdown inline>
  123
</Markdown>
编译后
<span>
  123
</span>

Built-in Component Proxy

babel-plugin-markdown-in-jsx 提供一种方法可以自定义 Markdown 编译结果中的 React 原生组件。

有几种使用场景:

  1. 如果你希望用更复杂的 <custom-img /> 来替换原生的 <img />
  2. 如果你使用 css in js,为 Markdown 内容增加样式可能会有困难。
  3. 如果你想要给所有 a 标签加上 target="_blank"

你需要修改 .babelrc 打开 proxy 功能。

{
  "plugins": [
    ["markdown-in-jsx", {"proxy": true}]
  ]
}

proxy 功能使用了 reactcontext 来实现,关于 Context 的详细信息

引入 babel-plugin-markdown-in-jsx/component/proxy

import * as Proxy from 'babel-plugin-markdown-in-jsx/component/proxy';

再使用 <Proxy.Provider /> 并设置 value 属性

比如要替换文中所有的 <img /><h1 />

<Proxy.Provider 
  value={{
    img: CustomImage,
    h1: CustomH1
  }}
>
  ...
</Proxy.Provider>

<Proxy.Provider /> 下面的所有 <Markdown /> 都会被设置影响。

下面是一个为所有的图片增加红色边框的例子。

import * as React from 'react';
import Markdown from 'babel-plugin-markdown-in-jsx/component';
import * as Proxy from 'babel-plugin-markdown-in-jsx/component/proxy';

function CustomImage(props) {
  return (
    <img style={{border: '3px solid #f00'}} {...props} />
  );
}

function TestComponent() {
  return (
    <Proxy.Provider value={{img: CustomImage}}>
      <Markdown>
        # babel-plugin-markdown-in-jsx

        Transform Markdown inside `JSX` to `DOM` elements at compile time.

        ![](./github.png)
      </Markdown>
    </Proxy.Provider>
  )
}

proxy

License

GNU General Public License Version 3