GithubHelp home page GithubHelp logo

kevelena / vue-plugin Goto Github PK

View Code? Open in Web Editor NEW

This project forked from highlightjs/vue-plugin

0.0 0.0 0.0 63 KB

Highlight.js Vue Plugin

License: BSD 3-Clause "New" or "Revised" License

JavaScript 19.02% TypeScript 80.98%

vue-plugin's Introduction

Highlight.js plugin for Vue.js

latest version latest version vue 3 vue 2 license

publish size gzipped size slack

This plugin provides a highlightjs component for use in your Vue.js 3 applications:

<div id="app">
    <!-- bind to a data property named `code` -->
    <highlightjs autodetect :code="code" />
    <!-- or literal code works as well -->
    <highlightjs language='javascript' code="var x = 5;" />
</div>

Note: For Vue.js version 2 support see the 1-stable branch here on GitHub. The latest tag via NPM is compatible with Vue.js v2; the next tag Vue.js v3.

Using the pre-built libraries

<link rel="stylesheet" href="/path/to/styles/default.css">
<script src="/path/to/highlight.min.js"></script>
<script src="/path/to/highlight-vue.min.js"></script>

Then simply register the plugin with Vue:

const app = createApp(App)
app.use(hljsVuePlugin)

Using ES6 modules / bundling

import 'highlight.js/styles/stackoverflow-light.css'
import hljs from 'highlight.js/lib/core';
import javascript from 'highlight.js/lib/languages/javascript';
import hljsVuePlugin from "@highlightjs/vue-plugin";

hljs.registerLanguage('javascript', javascript);

const app = createApp(App)
app.use(hljsVuePlugin)
app.mount('#app')

Note: The plugin imports highlight.js/lib/core internally (but no languages). Thanks to the magic of ES6 modules you can import Highlight.js anywhere to register languages or configure the library. Any import of Highlight.js refers to the same singleton instance of the library, so configuring the library anywhere configures it everywhere.

You can also simply load all "common" languages at once (as of v11):

import 'highlight.js/styles/stackoverflow-light.css'
import 'highlight.js/lib/common';
import hljsVuePlugin from "@highlightjs/vue-plugin";

const app = createApp(App)
app.use(hljsVuePlugin)
app.mount('#app')

Note that the highlight.js/lib/common import does not import the hljs object because it registers common languages internally and modern web bundlers like webpack will optimize out unused imported names. If you want to customize the hljs object, you can import it like the previous example.

Using component locally

<template>
    <highlightjs
        language="js"
        code="console.log('Hello World');"
    />
</template>

<script>
import 'highlight.js/lib/common';
import hljsVuePlugin from "@highlightjs/vue-plugin";

export default {
    components: {
        highlightjs: hljsVuePlugin.component
    }
}
</script>

Building the pre-built library from source

We use rollup to build the dist distributable.

npm run build

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.