mirror of
https://github.com/jkingsman/Remote-Terminal-for-MeshCore.git
synced 2026-08-02 06:53:09 +02:00
Add hashtag link detection. Closes #134.
This commit is contained in:
@@ -2,6 +2,9 @@
|
||||
* Parse sender from channel message text.
|
||||
* Channel messages have format "sender: message".
|
||||
*/
|
||||
const HASHTAG_CHANNEL_NAME_PATTERN = /^[a-z0-9]+(?:-[a-z0-9]+)*$/;
|
||||
const HASHTAG_CHANNEL_REFERENCE_PATTERN = /(^|\s)(#[a-z0-9]+(?:-[a-z0-9]+)*)(?=$|\s)/g;
|
||||
|
||||
export function parseSenderFromText(text: string): { sender: string | null; content: string } {
|
||||
const colonIndex = text.indexOf(': ');
|
||||
if (colonIndex > 0 && colonIndex < 50) {
|
||||
@@ -17,6 +20,35 @@ export function parseSenderFromText(text: string): { sender: string | null; cont
|
||||
return { sender: null, content: text };
|
||||
}
|
||||
|
||||
export interface HashtagChannelReference {
|
||||
label: string;
|
||||
start: number;
|
||||
end: number;
|
||||
}
|
||||
|
||||
export function isValidLinkedChannelName(name: string): boolean {
|
||||
return HASHTAG_CHANNEL_NAME_PATTERN.test(name);
|
||||
}
|
||||
|
||||
export function findLinkedChannelReferences(text: string): HashtagChannelReference[] {
|
||||
const references: HashtagChannelReference[] = [];
|
||||
let match: RegExpExecArray | null;
|
||||
|
||||
HASHTAG_CHANNEL_REFERENCE_PATTERN.lastIndex = 0;
|
||||
while ((match = HASHTAG_CHANNEL_REFERENCE_PATTERN.exec(text)) !== null) {
|
||||
const prefix = match[1];
|
||||
const label = match[2];
|
||||
const start = match.index + prefix.length;
|
||||
references.push({
|
||||
label,
|
||||
start,
|
||||
end: start + label.length,
|
||||
});
|
||||
}
|
||||
|
||||
return references;
|
||||
}
|
||||
|
||||
/**
|
||||
* Format a Unix timestamp to a time string.
|
||||
* Shows date for messages not from today.
|
||||
|
||||
Reference in New Issue
Block a user