Closed miadabdi closed 3 years ago
because of import is asynchronous,you can use like this:
const hljs = require('highlight.js');
window.hljs = hljs;
require('highlightjs-line-numbers.js');
because of 'highlightjs-line-numbers.js' check window.hljs is exist,so you need to set window.hljs = hljs,or it will be show error message like 'highlight.js not detected! '
@qinchao888 Thanks, it did fix it. but apparently import
in ES6 modules
is async and cannot be used for this package and we're restricted to use CommonJs
!
I was only able to get line numbers to work like this.
import markdownStyles from './markdown-styles.module.css';
const hljs = require('highlight.js');
export async function highlightJSLoad() {
return await import('highlight.js');
}
export async function highlightLineNumbersLoad() {
return await import('highlightjs-line-numbers.js');
}
const PostBody = ({ content }: any) => {
useEffect(() => {
highlightJSLoad().then((hljs_arg) => {
window.hljs = hljs;
hljs.highlightAll();
highlightLineNumbersLoad().then(() => {
const blocks = document.querySelectorAll('pre code');
blocks.forEach((block) => {
hljs.lineNumbersBlock(block);
});
});
});
}, []);
return (
<div className="max-w-2xl mx-auto">
<div
className={markdownStyles['markdown']}
dangerouslySetInnerHTML={{ __html: content }}
/>
</div>
);
};
export default PostBody;
Describe the bug I'm trying to use this package. even though I import it after
highlight.js
, it keeps throwinghighlight.js not detected!
To Reproduce Steps to reproduce the behavior: First of all I'm using Webpack with this config:
I installed
highlight.js
andhighlightjs-line-numbers.js
using npm. I imported them this way (using es6 import/export):and it throws:
I also tried to import
highlight.js
this way. still throws the same errorExpected behavior To detect
highlight.js
Desktop (please complete the following information):
webpack version: 4.44.1 highlight.js version: 10.2.1 highlightjs-line-numbers.js version: 2.8.0