我正在尝试在标记的编辑器中获取 Katex, https://github.com/markedjs/marked/issues/1538 从上面的链接中获取了代码,效果很好!
const marked = require('marked');
const katex = require('katex'); // external
const renderer = new marked.Renderer();
function mathsExpression(expr) {
if (expr.match(/^\$\$[\s\S]*\$\$$/)) {
expr = expr.substr(2, expr.length - 4);
return katex.renderToString(expr, { displayMode: true });
} else if (expr.match(/^\$[\s\S]*\$$/)) {
expr = expr.substr(1, expr.length - 2);
return katex.renderToString(expr, { isplayMode: false });
}
}
但我想将 katex 作为单独的模块加载,所以我尝试使用 webpack 中的外部组件:https ://webpack.js.org/configuration/externals/
output: {
path: PATH(isDev),
filename: "bundle.js",
library: "BUNDLE",
libraryExport: "default",
libraryTarget: "umd",
globalObject: "this",
},
externals: {
katex: {
root: "katex",
commonjs2: "katex",
commonjs: "katex",
amd: "katex",
},
},
但是上面代码中的 katex 是未定义的,但它在 HTML 文件之外可用。因为我添加了 katex js 文件
<script
src="../katex/katex.min.js"
integrity="sha384-g7c+Jr9ZivxKLnZTDUhnkOnsh30B4H0rpLUpJ4jAIKs4fnJI+sEnkvrMWph2EDg4"
crossorigin="anonymous"
></script>
我想我在这里遗漏了一些东西......你能帮我吗?谢谢