mirror of
https://github.com/Smaug123/KaTeX
synced 2025-10-05 03:08:40 +00:00
* Create a separate entry point for webpack
Created a webpack entry point for KaTeX, which imports katex.less. As
flow[1] and jest[2] doesn't support CSS modules natively, a separate
entry point is used and it is not flowtyped.
[1] https://gist.github.com/lambdahands/d19e0da96285b749f0ef
[2] https://facebook.github.io/jest/docs/en/webpack.html
* Use webpack to build files
* Made webpack.config.js export valid webpack configuration
* Use:
browserify -> webpack
babelify -> babel-loader
UglifyJS CLI -> UglifyJsPlugin
Less CLI -> less-loader
cleancss -> cssnano in css-loader
build/fonts -> file-loader
* Inline CSS(Less) using style-loader and export them using
ExtractTextPlugin
* Add `watch` npm script calling `webpack --watch`
* Improve local testing(webpack-dev-server)
* Made webpackDevServer export a valid webpack configuration
* Compile Less and inline CSS using less-loader and style-loader
* Instead of copying files serve files from /static and use file-loader
* Remove old server.js and its dependencies
* Use webpack-dev-server in Screenshotter
* Include contrib in webpack-dev-server
+ Moved common configurations to webpack.common.js
* Rename webpackDevServer.js to webpack.dev...
to be consistent, avoid confusion with webpack-dev-server and follow
webpack configuration naming convention.
* Remove unnecessary conditional output.path
* Use map instead of reduce
+ Add comments regarding function arguments
* Remove unnecessary mkdir and clean build/* before build
* Use katex as external dependency instead of global variable in contrib
Fixes #692.
* Unblock codes as they are built as a module
* Update package-lock.json
* Add comments regarding devServer option
* Lint renamed webpack.dev.js
a0d8b33
* Export ES6 module and expose its default export
* Revert "Browserify hotfix (#1057)"
This reverts commit f6b509123b
.
* Enables colors on the console when running the dev server in Screenshotter
* Add context to webpack configuration
Allows webpack to be run from other directories
* Move `rm -rf build/*` to npm scripts
* Check dependencies before build
* Move UglifyJsPlugin into config creation
* Let webpack handle ES6 modules
Do not transform modules to commonjs in Babel. However Jest doesn't not
support ES6 modules, so transfrom modules to commonjs when NODE_ENV is
`test`.
* Add documentation on testing in IE 9 and 10 using webpack-dev-server
Changed version range to include IE-compatible version
103 lines
3.3 KiB
JavaScript
103 lines
3.3 KiB
JavaScript
/* eslint no-console:0 */
|
|
|
|
import katex from "katex";
|
|
import splitAtDelimiters from "./splitAtDelimiters";
|
|
|
|
const splitWithDelimiters = function(text, delimiters) {
|
|
let data = [{type: "text", data: text}];
|
|
for (let i = 0; i < delimiters.length; i++) {
|
|
const delimiter = delimiters[i];
|
|
data = splitAtDelimiters(
|
|
data, delimiter.left, delimiter.right,
|
|
delimiter.display || false);
|
|
}
|
|
return data;
|
|
};
|
|
|
|
/* Note: optionsCopy is mutated by this method. If it is ever exposed in the
|
|
* API, we should copy it before mutating.
|
|
*/
|
|
const renderMathInText = function(text, optionsCopy) {
|
|
const data = splitWithDelimiters(text, optionsCopy.delimiters);
|
|
const fragment = document.createDocumentFragment();
|
|
|
|
for (let i = 0; i < data.length; i++) {
|
|
if (data[i].type === "text") {
|
|
fragment.appendChild(document.createTextNode(data[i].data));
|
|
} else {
|
|
const span = document.createElement("span");
|
|
const math = data[i].data;
|
|
// Override any display mode defined in the settings with that
|
|
// defined by the text itself
|
|
optionsCopy.displayMode = data[i].display;
|
|
try {
|
|
katex.render(math, span, optionsCopy);
|
|
} catch (e) {
|
|
if (!(e instanceof katex.ParseError)) {
|
|
throw e;
|
|
}
|
|
optionsCopy.errorCallback(
|
|
"KaTeX auto-render: Failed to parse `" + data[i].data +
|
|
"` with ",
|
|
e
|
|
);
|
|
fragment.appendChild(document.createTextNode(data[i].rawData));
|
|
continue;
|
|
}
|
|
fragment.appendChild(span);
|
|
}
|
|
}
|
|
|
|
return fragment;
|
|
};
|
|
|
|
const renderElem = function(elem, optionsCopy) {
|
|
for (let i = 0; i < elem.childNodes.length; i++) {
|
|
const childNode = elem.childNodes[i];
|
|
if (childNode.nodeType === 3) {
|
|
// Text node
|
|
const frag = renderMathInText(childNode.textContent, optionsCopy);
|
|
i += frag.childNodes.length - 1;
|
|
elem.replaceChild(frag, childNode);
|
|
} else if (childNode.nodeType === 1) {
|
|
// Element node
|
|
const shouldRender = optionsCopy.ignoredTags.indexOf(
|
|
childNode.nodeName.toLowerCase()) === -1;
|
|
|
|
if (shouldRender) {
|
|
renderElem(childNode, optionsCopy);
|
|
}
|
|
}
|
|
// Otherwise, it's something else, and ignore it.
|
|
}
|
|
};
|
|
|
|
const defaultAutoRenderOptions = {
|
|
delimiters: [
|
|
{left: "$$", right: "$$", display: true},
|
|
{left: "\\[", right: "\\]", display: true},
|
|
{left: "\\(", right: "\\)", display: false},
|
|
// LaTeX uses this, but it ruins the display of normal `$` in text:
|
|
// {left: "$", right: "$", display: false},
|
|
],
|
|
|
|
ignoredTags: [
|
|
"script", "noscript", "style", "textarea", "pre", "code",
|
|
],
|
|
|
|
errorCallback: function(msg, err) {
|
|
console.error(msg, err);
|
|
},
|
|
};
|
|
|
|
const renderMathInElement = function(elem, options) {
|
|
if (!elem) {
|
|
throw new Error("No element provided to render");
|
|
}
|
|
|
|
const optionsCopy = Object.assign({}, defaultAutoRenderOptions, options);
|
|
renderElem(elem, optionsCopy);
|
|
};
|
|
|
|
export default renderMathInElement;
|