How to find and reference the part of the syntax tree that corresponds to currently selected text #166
-
Say I have an HTML document in which I have selected a sentence with the cursor. How would I find and manipulate the text in the AST? I have looked for utilities that might accept positional information obtained from the DOM, and let you wrap/decorate it with a callback, but I can't tell if the ones I looked at do what I want. I did look at the unified-doc project, and I see the functionality I want is in there, but it seems to have last been updated 3-4 years ago. I would rather use something that is actively maintained. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 7 replies
-
Why? And what if the DOM is changed (by browser extensions, or other scripts)? There are probably things that are maintained. The gist of what you want is not too complex and depends more on what you want to do and what else you are using (say, react). |
Beta Was this translation helpful? Give feedback.
Taking a few steps back.
It really sounds like you're building an editing experience more than a pure preview experience.
You may want to consider leveraging an editor framework like slate, which is specifically designed to handle use cases like this https://jkrsp.com/slate-highlight-last-active-selection/
There are also full native options like the ones noted here: https://docs.expo.dev/guides/editing-richtext/#native-editors
Webview may be an easier entry point for HAST, but it is also possible to translate all content to react native.
A related project in the MDX space you could look at for reference https://g…