如何导入HighlightJS

时间:2019-02-19 04:07:46

标签: typescript highlightjs

VS Code扩展通常使用Typescript编写。我正在实现打印扩展,我想使用highlightjs进行语法着色。

所以我们带上它

npm i highlightjs -s

和不可避免的

npm i @types/highlight.js -s

在这一点上,我们发现我们已经在VS Code中完成了代码,并且可以编写

之类的东西。
import * as hljs from 'highlight.js';
...
let text = editor.document.getText();
let html = hljs.highlightAuto(text);

,它会编译并运行...直到我们执行触发加载highlight.js的尝试。此时会发生错误。

  

错误:找不到模块'highlight.js'extensionHostProcess.js:331     在Function.Module._resolveFilename   (内部/模块/cjs/loader.js:602:15)

../node_modules/highlightjs中的粘喙表明我们有highlight.pack.jshighlight.pack.min.js,但没有highlight.js

非常令人困惑。谁能告诉我如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

问题在于import React from 'react' import * as actions from '../store/actions/ticketaction' import { withRouter } from 'react-router-dom' import { connect } from 'react-redux'; import { withStyles } from '@material-ui/core/styles'; import compose from 'recompose/compose'; import TicketListView from '../ticketcontainers/TicketListView' const styles = theme => ({}) class CreateComment extends React.Component { handleSubmit = (e) => { // e.preventDefault(); const date = Date.now() const user = this.props.user.username const ticket = this.props.ticket.id this.props.postComment(e.target.comment.value, date, user, ticket) }; render(){ console.log(this.props) let commentStatus = null if(this.props.commentCreated){ commentStatus = ( <p>Your comment added</p> ) } return( <form onSubmit={this.handleSubmit}> {commentStatus} <textarea name='comment'></textarea> <button type='submit'>Submit </button> </form> ) } } const mapStateToDispatch = (dispatch) => ({ postComment: (comment, date, user, ticket) => dispatch(actions.postComment(comment, date, user, ticket)), }) export default withRouter(connect(null, mapStateToDispatch)(CreateComment)) highlightjs软件包都存在,但是highlight.js必须与@types/highlight.js软件包配对。