question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to use line-number plugin with webpack

See original GitHub issue

I hava code link this. But it not show line number

const Prism = require('prismjs/components/prism-core.js');
require('prismjs/plugins/line-numbers/prism-line-numbers.js');
require('prismjs/themes/prism.css');
require('prismjs/components/prism-javascript');
const code = 'var a = 1;';

render() {
    <pre className="line-numbers" data-start="0">
        <code className="language-javascript" dangerouslySetInnerHTML={{ __html: code }} />
    </pre>
}

Issue Analytics

  • State:open
  • Created 5 years ago
  • Comments:15 (3 by maintainers)

github_iconTop GitHub Comments

4reactions
tonix-tuftcommented, May 4, 2020

@mAAdhaTTah Awesome man! It works, thanks here is the complete working code:

import React, { useEffect } from "react";
import Prism from "prismjs";
import "../../node_modules/prism-themes/themes/prism-vsc-dark-plus.css";
import { compose } from "js-utl";
import { classNames } from "react-js-utl/utils";
import { useUniqueKey } from "react-js-utl/hooks";
import "prismjs/plugins/line-numbers/prism-line-numbers";
import "../../node_modules/prismjs/plugins/line-numbers/prism-line-numbers.css";

const PrismCode = compose(React.memo)(function PrismCode({
  className = void 0,
  codeClassName = void 0,
  children,
  withLineNumbers = true,
} = {}) {
  const id = useUniqueKey();

  useEffect(() => {
    Prism.highlightAllUnder(document.getElementById(id));
  }, [id, children]);

  return (
    <div id={id} className={classNames("prism-code", className)}>
      <pre
        className={classNames(codeClassName, withLineNumbers && "line-numbers")}
      >
        <code className={codeClassName}>{children}</code>
      </pre>
    </div>
  );
});
PrismCode.displayName = "PrismCode";
export default PrismCode;
3reactions
Yee1014commented, Mar 11, 2020

In Vue.js Components

<template>
      <div>
        <pre class="line-numbers language-markup"><code v-html="codeData"></code></pre>
      </div>
</template>

<script>
import Prism from 'prismjs'
import 'prismjs/plugins/line-numbers/prism-line-numbers.js'
import 'prismjs/plugins/line-numbers/prism-line-numbers.css'
import 'prismjs/themes/prism-tomorrow.css'

export default {
 data () {
    return {
      codeData: '',
    }
  },
 methods: {
    /**
     * show highlight code
     * @param {string} data
     */
 showCode (data) {
      this.codeData = data
      this.codeData = Prism.highlight(data, Prism.languages.markup)
      // key code
      this.$nextTick(() => {
        Prism.highlightAll()
      })
    }
}
}

ReView

QQ截图20200311172539

Others

babel-plugin-prismjs, i don’t know it how to work?😂

Read more comments on GitHub >

github_iconTop Results From Across the Web

Webpack from Nothing - Stack Traces and Line Numbers that ...
The main disadvantage is that it doesn't display line numbers correctly. Like…why am I using source maps if I am also OK with...
Read more >
TerserWebpackPlugin | webpack
This plugin uses terser to minify/minimize your JavaScript. Getting Started. Webpack v5 comes with the latest terser-webpack-plugin out of the box. If you...
Read more >
webpack-dev-server: how to get error line numbers of orignal ...
I'm using webpack with babel for ES2015 js. Screenshot. webpack.config.dev.js. const path = require('path'); const webpack = require('webpack'); ...
Read more >
Line Numbers Prism plugins
Add the line-numbers class to your desired <pre> or any of its ancestors, and the Line Numbers plugin will take care of the...
Read more >
Add Line Numbers to Prismjs Code Blocks in a Next.js Project
</code> block is required to get highlighting to work. ... npm install prismjs ... import 'prismjs/plugins/line-numbers/prism-line-numbers.js' import ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found