uiwjs / uiwjs/react-md-editor

KaTeX Preview example does not work

Open
#372 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
TypeScript
Stars
2.9k
Forks
201
Avg merge
12m
Merged PRs (30d)
1

Description

Hello,

I tried the Support Custom KaTeX Preview example but it did not work because the children for ``` this code block ``` may have multiple children. Regular multi-line code blocks also fail because only the first child gets passed.

I came up with a solution of traversing down each of the tree node. This finally works but I am not sure if there is no better solution to this since the typing and scrolling performance just feels so sluggish. Is there any way to improve this?

                components: {
                    code: ({inline, children = [], className}) => {
                        let shouldRenderKatex = false;
                        let customRenderText = '';
                        interface BranchType {
                            props: {
                                children: BranchType[]
                            }
                        }
                        type BranchNodeType = string|BranchType;

                        const traverseTree = (branch: BranchNodeType[] = children) => {
                            for (const child of branch) {
                                if (typeof child === 'string') {
                                    customRenderText += child;
                                    continue;
                                }
                                traverseTree(child['props']['children']);
                            }
                        }
                        if (inline && /^\$\$(.*)\$\$/.test(children[0] as string ?? '')) {
                            customRenderText = (children[0] as string).replace(/^\$\$(.*)\$\$/, '$1');
                            shouldRenderKatex = true;
                        } else if (/^language-KaTeX/.test(className as string ?? '')) {
                            traverseTree();
                            shouldRenderKatex = true;
                        }
                        if (shouldRenderKatex) {
                            const html = katex.renderToString(customRenderText ?? '', {
                                throwOnError: false,
                            });
                            return <code dangerouslySetInnerHTML={{ __html: html }} />;
                        }
                        return <code className={String(className)}>{children}</code>;
                    },
                },

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the Support Custom KaTeX Preview example and its custom code renderer. Reproduce the failure with multiline KaTeX and regular multiline code blocks, then compare rendering all children with the reported traversal approach; done means both cases render correctly without the noted typing or scrolling sluggishness.

Written by the indexing model from the issue text.

Assessment

Tech stack
react, typescript
Domain
frontend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.