Add hashtag link detection. Closes #134.

This commit is contained in:
Jack Kingsman
2026-03-31 12:55:52 -07:00
parent 29e9a5f701
commit 9f4737d350
8 changed files with 308 additions and 15 deletions
+32
View File
@@ -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.