arronhunt / highlightjs-copy

📋❇️ A simple, accessible highlightjs plugin to add a copy button to your code blocks.
https://highlightjs-copy.netlify.app
The Unlicense
97 stars 23 forks source link

how to import highlightjs-copy in js file after npm install #8

Open YavarR opened 1 year ago

YavarR commented 1 year ago

Hi Arron I installed highlightjs-copy, But I can’t import it after npm install. Could you pleas help me. I already instals highlight.js and it works perfectly.

That is for highlight.js: import hljs from 'highlight.js/lib/core'; import 'highlight.js/styles/base16/railscasts.css'; import javascript from 'highlight.js/lib/languages/javascript'

pep108 commented 1 year ago

I had the same problem with this. I ended up adding the file to my repo and changing the first line to:

module.exports = class CopyButtonPlugin {

Then I included it with:

const CopyButtonPlugin = require('./vendor/highlightjs-copy.js')

To initialize, I wrapped the code in a timeout because I found that it wouldn't work in Chrome otherwise:

setTimeout(() => { hljs.addPlugin(new CopyButtonPlugin()) hljs.highlightAll() }, 100)

nmangold commented 1 year ago

I was having a similar issue, and I created a PR to export a default, https://github.com/arronhunt/highlightjs-copy/pull/21.

The previous change restricts the export to NodeJS environment, which did not work in my case.