Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to use it in vue.js with vue.directive #73

Open
Mirocos opened this issue Feb 27, 2020 · 9 comments
Open

how to use it in vue.js with vue.directive #73

Mirocos opened this issue Feb 27, 2020 · 9 comments

Comments

@Mirocos
Copy link

Mirocos commented Feb 27, 2020

I want use it in vue with its directive with hightlightjs, how to use it?

Vue.directive('highlightjs', {
  deep: true,
  bind: function(el, binding) {
    // on first bind, highlight all targets
    let targets = el.querySelectorAll('code')
    targets.forEach((target) => {
      // if a value is directly assigned to the directive, use this
      // instead of the element content.
      if (binding.value) {
        target.textContent = binding.value
        console.log(target)
      }
      hljs.highlightBlock(target)
      // hljs.lineNumbersBlock(target)
    })
  },
  componentUpdated: function(el, binding) {
    // after an update, re-fill the content and then highlight
    let targets = el.querySelectorAll('code')
    targets.forEach((target) => {
      if (binding.value) {
        target.textContent = binding.value
        // hljs.initLineNumbersOnLoad();
        hljs.highlightBlock(target)

        // hljs.lineNumbersBlock(target)
      }
    })
  }
})
@wcoder
Copy link
Owner

wcoder commented Feb 27, 2020

I haven't experience in vue.js, but found some solution for you in vue-highlightjs package:

https://github.com/metachris/vue-highlightjs/blob/master/index.js#L8

To continue will be the same as highlightjs,

Usage - initialization: https://github.com/wcoder/highlightjs-line-numbers.js#usage

@xiaodun
Copy link

xiaodun commented Mar 6, 2020

I want use it in vue with its directive with hightlightjs, how to use it?

Vue.directive('highlightjs', {
  deep: true,
  bind: function(el, binding) {
    // on first bind, highlight all targets
    let targets = el.querySelectorAll('code')
    targets.forEach((target) => {
      // if a value is directly assigned to the directive, use this
      // instead of the element content.
      if (binding.value) {
        target.textContent = binding.value
        console.log(target)
      }
      hljs.highlightBlock(target)
      // hljs.lineNumbersBlock(target)
    })
  },
  componentUpdated: function(el, binding) {
    // after an update, re-fill the content and then highlight
    let targets = el.querySelectorAll('code')
    targets.forEach((target) => {
      if (binding.value) {
        target.textContent = binding.value
        // hljs.initLineNumbersOnLoad();
        hljs.highlightBlock(target)

        // hljs.lineNumbersBlock(target)
      }
    })
  }
})

image

@xiaodun
Copy link

xiaodun commented Mar 6, 2020

I want use it in vue with its directive with hightlightjs, how to use it?

Vue.directive('highlightjs', {
  deep: true,
  bind: function(el, binding) {
    // on first bind, highlight all targets
    let targets = el.querySelectorAll('code')
    targets.forEach((target) => {
      // if a value is directly assigned to the directive, use this
      // instead of the element content.
      if (binding.value) {
        target.textContent = binding.value
        console.log(target)
      }
      hljs.highlightBlock(target)
      // hljs.lineNumbersBlock(target)
    })
  },
  componentUpdated: function(el, binding) {
    // after an update, re-fill the content and then highlight
    let targets = el.querySelectorAll('code')
    targets.forEach((target) => {
      if (binding.value) {
        target.textContent = binding.value
        // hljs.initLineNumbersOnLoad();
        hljs.highlightBlock(target)

        // hljs.lineNumbersBlock(target)
      }
    })
  }
})

image

but the format is invalid!

image

@tanganyu1114
Copy link

@xiaodun 这个问题你找到解决的办法了吗?遇到相同的问题了,求告知,谢谢

@Mirocos
Copy link
Author

Mirocos commented Nov 26, 2020

I import highlight.js and highlightjs-line-numbers.js in index.html. Then use following code to initialize:

hljs.initHighlightingOnLoad();
hljs.initLineNumbersOnLoad();

when it comes to update the highlight code and just use following code to re-render it:

this.code = hljs.highlightAuto(this.shader.vs).value;  // in my case , this.shader.vs is the target code
hljs.initLineNumbersOnLoad();

The html code is here:

<pre ><code v-html="code">



 </code></pre>

@tanganyu1114
Copy link

I try it ,but failed o(╥﹏╥)o , Thanks all the way

@Mirocos
Copy link
Author

Mirocos commented Nov 26, 2020

I try it ,but failed o(╥﹏╥)o , Thanks all the way

where did you initialize the format?

@Nothing-bit
Copy link

i have solved this problem, and i've recorded it in my blog https://www.zhoujianguo.ltd/#/fore/article?id=140

@hywhuangyuwei
Copy link

My Vue3 + TypeScript + "vue-plugin-load-script" package solution:

import * as vue from 'vue'
import hljs from "highlight.js";
import {loadScript} from "vue-plugin-load-script";

const codeStr = vue.ref("")

vue.onBeforeMount(async () => {
  // codeStr.value = "..."

  await vue.nextTick();
  (window as any).hljs = hljs;
  loadScript("https://cdnjs.cloudflare.com/ajax/libs/highlightjs-line-numbers.js/2.8.0/highlightjs-line-numbers.min.js").then(() => {
    (window as any).hljs.initLineNumbersOnLoad();
  })
})
<template>
  <div>
    <highlightjs :code="codeStr" />
  </div>
</template>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants