Skip to content

Commit a664804

Browse files
brichetdlqqq
andauthored
Revert the logic to display symbol, added in #95 (#99)
Co-authored-by: david qiu <[email protected]>
1 parent 1f21f30 commit a664804

File tree

1 file changed

+1
-57
lines changed

1 file changed

+1
-57
lines changed

packages/jupyter-chat/src/components/rendermime-markdown.tsx

Lines changed: 1 addition & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -39,61 +39,6 @@ function escapeLatexDelimiters(text: string) {
3939
.replace('\\]/g', '\\\\]');
4040
}
4141

42-
/**
43-
* Type predicate function that determines whether a given DOM Node is a Text
44-
* node.
45-
*/
46-
function isTextNode(node: Node | null): node is Text {
47-
return node?.nodeType === Node.TEXT_NODE;
48-
}
49-
50-
/**
51-
* Escapes all `$` symbols present in an HTML element except those within the
52-
* following elements: `pre`, `code`, `samp`, `kbd`.
53-
*
54-
* This prevents `$` symbols from being used as inline math delimiters, allowing
55-
* `$` symbols to be used literally to denote quantities of USD. This does not
56-
* escape literal `$` within elements that display their contents literally,
57-
* like code elements. This overrides JupyterLab's default rendering of MarkDown
58-
* w/ LaTeX.
59-
*
60-
* The Jupyter AI system prompt should explicitly request that the LLM not use
61-
* `$` as an inline math delimiter. This is the default behavior.
62-
*/
63-
function escapeDollarSymbols(el: HTMLElement) {
64-
// Get all text nodes that are not within pre, code, samp, or kbd elements
65-
const walker = document.createTreeWalker(el, NodeFilter.SHOW_TEXT, {
66-
acceptNode: node => {
67-
const isInSkippedElements = node.parentElement?.closest(
68-
'pre, code, samp, kbd'
69-
);
70-
return isInSkippedElements
71-
? NodeFilter.FILTER_SKIP
72-
: NodeFilter.FILTER_ACCEPT;
73-
}
74-
});
75-
76-
// Collect all valid text nodes in an array.
77-
const textNodes: Text[] = [];
78-
let currentNode: Node | null;
79-
while ((currentNode = walker.nextNode())) {
80-
if (isTextNode(currentNode)) {
81-
textNodes.push(currentNode);
82-
}
83-
}
84-
85-
// Replace each `$` symbol with `\$` for each text node, unless there is
86-
// another `$` symbol adjacent or it is already escaped. Examples:
87-
// - `$10 - $5` => `\$10 - \$5` (escaped)
88-
// - `$$ \infty $$` => `$$ \infty $$` (unchanged)
89-
// - `\$10` => `\$10` (unchanged, already escaped)
90-
textNodes.forEach(node => {
91-
if (node.textContent) {
92-
node.textContent = node.textContent.replace(/(?<![$\\])\$(?!\$)/g, '\\$');
93-
}
94-
});
95-
}
96-
9742
function RendermimeMarkdownBase(props: RendermimeMarkdownProps): JSX.Element {
9843
const appendContent = props.appendContent || false;
9944
const [renderedContent, setRenderedContent] = useState<HTMLElement | null>(
@@ -124,8 +69,7 @@ function RendermimeMarkdownBase(props: RendermimeMarkdownProps): JSX.Element {
12469
);
12570
}
12671

127-
// step 2: render LaTeX via MathJax, while escaping single dollar symbols.
128-
escapeDollarSymbols(renderer.node);
72+
// step 2: render LaTeX via MathJax.
12973
props.rmRegistry.latexTypesetter?.typeset(renderer.node);
13074

13175
const newCodeToolbarDefns: [HTMLDivElement, CodeToolbarProps][] = [];

0 commit comments

Comments
 (0)