MCPcopy Create free account
hub / github.com/dailydotdev/apps / isSelectionInMarkdownLink

Function isSelectionInMarkdownLink

packages/shared/src/lib/markdown.ts:18–96  ·  view source on GitHub ↗
(
  textarea: HTMLTextAreaElement,
  selectionStart: number,
  selectionEnd: number,
)

Source from the content-addressed store, hash-verified

16 * Returns true if the selection is inside a pattern like [text](url) or [](url)
17 */
18export const isSelectionInMarkdownLink = (
19 textarea: HTMLTextAreaElement,
20 selectionStart: number,
21 selectionEnd: number,
22): boolean => {
23 const text = textarea.value;
24
25 // Look backwards from selection start to find the structure
26 let openBracketIndex = -1;
27 let closeBracketIndex = -1;
28 let openParenIndex = -1;
29
30 for (let i = selectionStart - 1; i >= 0; i -= 1) {
31 if (text[i] === '(' && openParenIndex === -1) {
32 openParenIndex = i;
33 } else if (text[i] === ']' && closeBracketIndex === -1) {
34 closeBracketIndex = i;
35 } else if (text[i] === '[' && openBracketIndex === -1) {
36 openBracketIndex = i;
37 break;
38 }
39 // If we hit a newline, stop looking
40 if (text[i] === '\n') {
41 return false;
42 }
43 }
44
45 // Look forward from selection end to find closing parenthesis
46 let closeParenIndex = -1;
47 let forwardCloseBracketIndex = -1;
48 let forwardOpenParenIndex = -1;
49
50 for (let i = selectionEnd; i < text.length; i += 1) {
51 if (text[i] === ']' && forwardCloseBracketIndex === -1) {
52 forwardCloseBracketIndex = i;
53 } else if (
54 text[i] === '(' &&
55 forwardCloseBracketIndex !== -1 &&
56 forwardOpenParenIndex === -1
57 ) {
58 forwardOpenParenIndex = i;
59 } else if (text[i] === ')') {
60 closeParenIndex = i;
61 break;
62 }
63 // If we hit a newline, stop looking
64 if (text[i] === '\n') {
65 return false;
66 }
67 }
68
69 // Case 1: Selection is in the text part [text](url)
70 if (
71 openBracketIndex !== -1 &&
72 forwardCloseBracketIndex !== -1 &&
73 forwardOpenParenIndex !== -1 &&
74 closeParenIndex !== -1
75 ) {

Callers 3

markdown.test.tsFile · 0.90
checkMentionFunction · 0.90
onPasteFunction · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected