get total lines prior to rendering #1310
-
i have a use case where i'm streaming markdown text to the renderer and want to show a text cursor at the end of each paragraph. this seems trivial, as shown below. since the streaming means paragraph lines increase gradually, the cursor should only be visible on the last paragraph. p: (props) => {
return (
<p>
{props.children}
{mainProps.shouldShowTextCursor && <CaretSVG />}
</p>
)
} i know i can read an individual node's position from const plugin = () => {
function transformer(tree: any) { // tree.position.end }
return transformer
} |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 5 replies
-
another potential solution could be to use a pseudo-selector in css to show the cursor only for the last |
Beta Was this translation helpful? Give feedback.
-
Welcome @nullhook! 👋 Handling streaming markdown text and adding a text cursor at each paragraph's end sounds straightforward, but ensuring smooth rendering requires careful consideration. Let's explore some recommendations you can apply and test. Before delving into complex content customization or resorting to heavy state management, consider leveraging CSS for dynamic styling. You can achieve this by wrapping your markdown content within a container element and applying a CSS class to it. Then, using CSS, target the last paragraph within this container and dynamically append the text cursor. Here's how you can implement this approach using React and the import React from 'react';
import ReactMarkdown from 'react-markdown';
import './MarkdownContainer.css'; // Assuming you have a CSS file for styling
const MarkdownContainer = ({ markdownContent, shouldShowTextCursor }) => {
return (
<div className="markdown-container">
<ReactMarkdown>{markdownContent}</ReactMarkdown>
{shouldShowTextCursor && <div className="text-cursor">|</div>}
</div>
);
};
export default MarkdownContainer; In this example, We wrap the markdown content within a container div with the class Now, let's define the CSS to target the last paragraph and style the text cursor: .markdown-container {
/* Your container styles here */
}
.markdown-container p:last-of-type {
position: relative;
}
.text-cursor {
position: absolute;
bottom: 0;
right: 0;
/* Additional styling for the text cursor */
} With this setup, the text cursor will dynamically appear at the end of the last paragraph as the markdown content is streamed. This CSS-first approach keeps the rendering logic simple and efficient. While remark or rehype plugins might offer more control over markdown rendering, they could be overkill for this use case. Starting with CSS provides a lightweight and effective solution that you can play around with and test. |
Beta Was this translation helpful? Give feedback.
-
was your reply written by chatgpt? sounds templatey. your css solution assumes that paragraph elements fill the bounding box completely. however, there might be text within that box ending prematurely, so this method wouldn't work. |
Beta Was this translation helpful? Give feedback.
With your updated message I suspect you're looking for something like https://css-tricks.com/snippets/css/typewriter-effect/ but it's still unclear.
Again, take time to frame your question, and chill.