mirror of
https://github.com/Smaug123/KaTeX
synced 2025-10-06 19:58:40 +00:00
* Initial webpack config. Moving to ES6 modules. Some module cleanup. * WIP * WIP * Removing commented out code. * Removing old deps. * Removing the build script (used for testing). * Working tests. * Switching to node api over cli. * Updating per comments. Still need to fix server.js to properly run the selenium tests. * Cleaning up the config. * More cleanup. * Bringing back server.js for selenium tests. * Bringing back old dependencies. * Adding back eslint rules for webpack config. Final cleanup for webpack config. * Pointing to correct pre-existing module versions. Adding some extra logic to server.js to ensure it gets transpiled properly. * Getting make build to work again. Updating package.json with some shortcut scripts. * Resolving conflict. * Reverting back to commonjs modules. * Removing extra spaces in babelrc
53 lines
2.1 KiB
JavaScript
53 lines
2.1 KiB
JavaScript
// Set these to how you want inline and display math to be delimited.
|
|
export const defaultCopyDelimiters = {
|
|
inline: ['$', '$'], // alternative: ['\(', '\)']
|
|
display: ['$$', '$$'], // alternative: ['\[', '\]']
|
|
};
|
|
|
|
// Replace .katex elements with their TeX source (<annotation> element).
|
|
// Modifies fragment in-place. Useful for writing your own 'copy' handler,
|
|
// as in copy-tex.js.
|
|
export const katexReplaceWithTex = function(fragment,
|
|
copyDelimiters = defaultCopyDelimiters) {
|
|
// Remove .katex-html blocks that are preceded by .katex-mathml blocks
|
|
// (which will get replaced below).
|
|
const katexHtml = fragment.querySelectorAll('.katex-mathml + .katex-html');
|
|
for (let i = 0; i < katexHtml.length; i++) {
|
|
const element = katexHtml[i];
|
|
if (element.remove) {
|
|
element.remove(null);
|
|
} else {
|
|
element.parentNode.removeChild(element);
|
|
}
|
|
}
|
|
// Replace .katex-mathml elements with their annotation (TeX source)
|
|
// descendant, with inline delimiters.
|
|
const katexMathml = fragment.querySelectorAll('.katex-mathml');
|
|
for (let i = 0; i < katexMathml.length; i++) {
|
|
const element = katexMathml[i];
|
|
const texSource = element.querySelector('annotation');
|
|
if (texSource) {
|
|
if (element.replaceWith) {
|
|
element.replaceWith(texSource);
|
|
} else {
|
|
element.parentNode.replaceChild(texSource, element);
|
|
}
|
|
texSource.innerHTML = copyDelimiters.inline[0] +
|
|
texSource.innerHTML + copyDelimiters.inline[1];
|
|
}
|
|
}
|
|
// Switch display math to display delimiters.
|
|
const displays = fragment.querySelectorAll('.katex-display annotation');
|
|
for (let i = 0; i < displays.length; i++) {
|
|
const element = displays[i];
|
|
element.innerHTML = copyDelimiters.display[0] +
|
|
element.innerHTML.substr(copyDelimiters.inline[0].length,
|
|
element.innerHTML.length - copyDelimiters.inline[0].length
|
|
- copyDelimiters.inline[1].length)
|
|
+ copyDelimiters.display[1];
|
|
}
|
|
return fragment;
|
|
};
|
|
|
|
export default katexReplaceWithTex;
|