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

@cloud-ui/x-highlight.vue

v0.9.0

Published

本组件对 [highlight.js](https://highlightjs.org/usage/) 进行了封装,方便代码高亮展示。

Downloads

19

Readme

XHighlight 代码高亮

三方组件, 块级展示

本组件对 highlight.js 进行了封装,方便代码高亮展示。

安装配置

安装

npm i --save @cloud-ui/x-highlight.vue

引入

直接注册

import XHighlight from '@cloud-ui/x-highlight.vue';

Vue.component('x-highlight', XHighlight);

或者使用 vusion-utils 安装(常用于同时安装多个组件)

import { install } from 'vusion-utils';
import XHighlight from '@cloud-ui/x-highlight.vue';

install(Vue, { XHighlight });

打包配置

由于 highlight.js 包较大,可以添加在 Webpack 的配置里 externals 中。

或者用 dll 进行处理。

示例

基本用法

在组件上设置langcontent属性。

为了控制打包大小,我们没有引入全量包。使用时还需按照下面的方式,自行引入语言包和样式包。

import hljs from 'highlight.js/lib/highlight';
import javascript from 'highlight.js/lib/languages/javascript';
hljs.registerLanguage('javascript', javascript);
import 'highlight.js/styles/tomorrow.css';

更多语言和样式可以查看源码中的语言目录样式目录

下面是一段 javascript 的例子。

<template>
<x-highlight lang="javascript" :content="content"></x-highlight>
</template>

<script>
import hljs from 'highlight.js/lib/highlight';
import javascript from 'highlight.js/lib/languages/javascript';
hljs.registerLanguage('javascript', javascript);

export default {
    data() {
        return {
            content: `
const flat = {};
[[0, 1], [2, 3], [4, 5]].reduce((memo, item, index) => {
    const flatten = memo.concat(item);
    flat[index] = flatten;
    return flatten;
});
            `,
        };
    },
};
</script>

下面是一段 html 的例子,html 的语言包名称是 xml。

<template>
<x-highlight lang="html" :content="content"></x-highlight>
</template>

<script>
import hljs from 'highlight.js/lib/highlight';
import xml from 'highlight.js/lib/languages/xml';
hljs.registerLanguage('xml', xml);

export default {
    data() {
        return {
            content: `
<div class="app">
    <button>Button</button>
</div>
            `,
        };
    },
};
</script>

API

Props/Attrs

| Prop/Attr | Type | Options | Default | Description | | --------- | ---- | ------- | ------- | ----------- | | content | string | | '' | 原始代码内容 | | lang | string | | '' | 语言 | | auto | boolean | | false | 是否自动探测代码中的语言 | | preRender | Function<content: string, lang: string> | | 'this.defaultPreRender' | 渲染前对代码的处理函数 | | postRender | Function<result: string> | | 'this.defaultPostRender' | 渲染后对结果的处理函数 |

Events

@before-render

渲染前触发

| Param | Type | Description | | ----- | ---- | ----------- | | $event.content | string | 原始代码内容 | | $event.lang | string | 语言 | | $event.result | string | 渲染后的结果 | | $event.preventDefault | Function | 阻止渲染流程 | | senderVM | XHighlight | 发送事件实例 |

@render

渲染后触发

| Param | Type | Description | | ----- | ---- | ----------- | | $event.content | string | 原始代码内容 | | $event.lang | string | 语言 | | $event.result | string | 渲染后的结果 | | senderVM | XHighlight | 发送事件实例 |

Methods

render()

渲染代码。初始化时和content属性改变时会自动调用。

| Param | Type | Default | Description | | ----- | ---- | ------- | ----------- |