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

vite-plugin-doc-preview

v0.2.1

Published

基于Vite插件机制实现对markdown文件内容进行转换。核心通过marked结合hightlight.js实现代码高亮和美化。并且可让文档中带有如preview指定标识的markdown代码块替换为组件,体验预览效果。支持Vue和React框架。

Downloads

61

Readme

vite-plugin-doc-preview

vite-plugin-doc-preview 是基于 Vite 插件机制实现对 markdown 文件内容进行转换。核心通过 marked 结合 hightlight.js 实现代码高亮和美化。并且可让文档中带有如 preview 指定标识的 markdown 代码块替换为组件,体验预览效果。支持 VueReact 框架

示例

https://337547038.github.io/vite-plugin-doc-preview/

安装使用

安装依赖

npm install vite-plugin-doc-preview -D
# or
pnpm install vite-plugin-doc-preview -D
# or
yarn install vite-plugin-doc-preview -D

使用配置

在 Vue 中使用

// vite.config.ts
import { defineConfig } from 'vite'
import Vue from '@vitejs/plugin-vue'
import MarkedPreview from 'vite-plugin-doc-preview'

export default defineConfig({
  plugins: [
    Vue({
      include: [/\.vue$/, /\.md$/],
    }),
    MarkedPreview()
    // 可根据需要设置参数,可选
    /*MarkedPreview({
      marked:{}, // marked转换options,可参考https://marked.js.org/
      component:false // 是否自定义预览组件,默认false
      previewId:'vue preview' // 预览标识,默认vue preview
    })*/
  ]
})

在 React 中使用

// vite.config.ts
import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react';
import MarkedPreview from 'vite-plugin-doc-preview'

export default defineConfig({
  plugins: [
    react(),
    MarkedPreview({mode:'react'})
    // 可根据需要设置参数,可选
    /*MarkedPreview({
      marked:{}, // marked转换options,可参考https://marked.js.org/
      component:false // 是否自定义预览组件,默认false
      previewId:'vue preview' // 预览标识,默认vue preview
    })*/
  ]
})

引入样式

在适当组件页面引入高亮样式,如 main.ts 中。其他主题风格可参考 highlight.js 引入对应主题

import "vite-plugin-doc-preview/style/style.css"

代码预览标识

给需要预览的 markdown 文档代码块加上指定标识,如 vue preview 。其他标识可在vite.config.ts配置中修改

README.md 文件内容为:


下方代码块将会被解析为 Vue 组件并展示预览效果和代码高亮美化

```vue preview
<template>{{value}}</template>
<script setup>
 import {ref} from 'vue'
 const value = ref('hello 我是Vue模板')
</script>
```

或者为React 

```jsx preview
const Example = ()=>{
 return (<div>hello 我是React模板</div>)
}
export default Example
```

路由配置

 [
  {
    path: '/',
    name: '/md',
    component: () => import('./views/README.md')
  }
]

自定义预览组件

Vue 预览组件

如果默认的样式不能满足需求,可以全局注册一个 CodePreview 组件来代替默认组件。

设定自定义预览组件时

//1. vite.config.ts中配置:
MarkedPreview({component:true})  // 传入参数,表示为自定义预览组件

//2. main.ts中
import CodePreview from 'xxxxx'
app.component('CodePreview', CodePreview) // 注册自己的自定义好的预览组件

CodePreview 需要按约定支持如下 propsslot

  • props
    • code string 代码块的原始代码,代码已经encodeURIComponent处理
  • slot
    • default 代码块生成的 vue 组件
    • code 代码块经过高亮转换的 html

React 预览组件

React 没有全局注册的概念,可通过 component 传入预览组件路径,如

// 1.vite.config.ts中配置
MarkedPreview({component:'@/component/codePreview.jsx'})  // 传入参数,表示为自定义预览组件

预览组件需按约定支持如下 propschildren

  • props
    • code string 代码块的原始代码,代码已经encodeURIComponent处理
  • children
    • children[0] 代码块生成的 React 组件
    • children[1] 代码块经过高亮转换的 html