Skip to content

Commit

Permalink
ability to set new line on message using \n and user's messages are n…
Browse files Browse the repository at this point in the history
…ow also formatted
  • Loading branch information
OvidijusParsiunas committed May 29, 2024
1 parent ecc92f2 commit 2a9b692
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
6 changes: 4 additions & 2 deletions component/src/views/chat/messages/messagesBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {ProcessedTextToSpeechConfig} from './textToSpeech/textToSpeech';
import {ElementUtils} from '../../../utils/element/elementUtils';
import {HTMLDeepChatElements} from './html/htmlDeepChatElements';
import {RemarkableConfig} from './remarkable/remarkableConfig';
import {RemarkableUtils} from './remarkable/remarkableUtils';
import {FireEvents} from '../../../utils/events/fireEvents';
import {HTMLClassUtilities} from '../../../types/html';
import {MessageStyleUtils} from './messageStyleUtils';
Expand Down Expand Up @@ -166,10 +167,11 @@ export class MessagesBase {
}

public renderText(bubbleElement: HTMLElement, text: string) {
bubbleElement.innerHTML = this._remarkable.render(text);
const processedText = RemarkableUtils.replaceNewlineWithDouble(text);
bubbleElement.innerHTML = this._remarkable.render(processedText);
// there is a bug in remarkable where text with only numbers and full stop after them causes the creation
// of a list which has no innert text and is instead prepended as a prefix in the start attribute (12.)
if (bubbleElement.innerText.trim().length === 0) bubbleElement.innerText = text;
if (bubbleElement.innerText.trim().length === 0) bubbleElement.innerText = processedText;
}

// this is mostly used for enabling highlight.js to highlight code if it downloads later
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export class RemarkableUtils {
// this is to allow remarkable to render \n as a new line:
// it operates by replacing \n with \n\n
public static replaceNewlineWithDouble(str: string) {
return str.replace(/(?<!\n)\n(?!\n)/g, '\n\n');
}
}

0 comments on commit 2a9b692

Please sign in to comment.