MCPcopy Create free account
hub / github.com/ZenNotes/zennotes / extractLinkAtCursor

Function extractLinkAtCursor

packages/app-core/src/lib/internal-links.ts:145–169  ·  view source on GitHub ↗
(doc: string, pos: number)

Source from the content-addressed store, hash-verified

143 * link's URL, or a bare URL. Returns null when the offset isn't inside a link.
144 */
145export function extractLinkAtCursor(doc: string, pos: number): string | null {
146 const lineStart = doc.lastIndexOf('\n', pos - 1) + 1
147 const lineEnd = doc.indexOf('\n', pos)
148 const line = doc.slice(lineStart, lineEnd === -1 ? undefined : lineEnd)
149 const col = pos - lineStart
150 const wikiRe = /\[\[([^\]|]+?)(?:\|[^\]]+)?\]\]/g
151 let m: RegExpExecArray | null
152 while ((m = wikiRe.exec(line)) !== null) {
153 if (col >= m.index && col < m.index + m[0].length) return m[1]
154 }
155 // Angle-bracketed URLs can contain `)` so match them specifically first.
156 const mdAngleRe = /\[([^\]]*)\]\(<([^>]+)>\)/g
157 while ((m = mdAngleRe.exec(line)) !== null) {
158 if (col >= m.index && col < m.index + m[0].length) return m[2]
159 }
160 const mdRe = /\[([^\]]*)\]\(([^)]+)\)/g
161 while ((m = mdRe.exec(line)) !== null) {
162 if (col >= m.index && col < m.index + m[0].length) return unwrapMdUrl(m[2])
163 }
164 const urlRe = /https?:\/\/[^\s)>\]]+/g
165 while ((m = urlRe.exec(line)) !== null) {
166 if (col >= m.index && col < m.index + m[0].length) return m[0]
167 }
168 return null
169}
170
171/**
172 * The Markdown link `[label](href)` covering a document offset, with its full

Callers 3

EditorPaneFunction · 0.90
registerVimCommandsFunction · 0.90

Calls 1

unwrapMdUrlFunction · 0.85

Tested by

no test coverage detected