(doc: string, pos: number)
| 143 | * link's URL, or a bare URL. Returns null when the offset isn't inside a link. |
| 144 | */ |
| 145 | export 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 |
no test coverage detected