Skip to content

Commit 1e9e1ec

Browse files
committed
.
1 parent 2e5659f commit 1e9e1ec

File tree

1 file changed

+39
-27
lines changed

1 file changed

+39
-27
lines changed

autoload/mde_links.vim

Lines changed: 39 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,27 @@ export def IsLink(): dict<list<list<number>>>
262262
const range_info = utils.IsInRange()
263263
if !empty(range_info) && keys(range_info)[0] == 'markdownLinkText'
264264
return range_info
265+
elseif synIDattr(synID(line("."), charcol("."), 1), "name") == 'markdownUrl'
266+
# Find beginning of the URL
267+
var a = [line('.'), 1]
268+
var b = searchpos(' ', 'nbW')
269+
var start_pos = [0, 0]
270+
if utils.IsLess(a, b) && b != [0, 0]
271+
start_pos = [b[0], b[1] + 1]
272+
else
273+
start_pos = a
274+
endif
275+
276+
# Find end of the URL
277+
a = searchpos(' ', 'nW')
278+
b = [line('.'), charcol('$') - 1]
279+
var end_pos = [0, 0]
280+
if utils.IsLess(a, b) && a != [0, 0]
281+
end_pos = [a[0], a[1] - 1]
282+
else
283+
end_pos = b
284+
endif
285+
return {'markdownUrl': [start_pos, end_pos]}
265286
else
266287
return {}
267288
endif
@@ -314,22 +335,7 @@ export def OpenLink(is_split: bool = false)
314335
const saved_curpos = getcurpos()
315336
var link = ''
316337

317-
if synIDattr(synID(line("."), charcol("."), 1), "name") == 'markdownUrl'
318-
# The end of the URL is an empty space or the end of the line. We have
319-
# to capture the correct one.
320-
321-
# if "norm! T " don't move, then the URL string is until the end of the line
322-
const a = getcursorcharpos()
323-
execute "normal! t "
324-
const b = getcursorcharpos()
325-
if a == b
326-
# cursor didn't move
327-
link = utils.GetTextObject('$').text
328-
else
329-
execute "normal! T "
330-
link = utils.GetTextObject('t ').text
331-
endif
332-
else
338+
if synIDattr(synID(line("."), charcol("."), 1), "name") != 'markdownUrl'
333339
# Start the search from the end of the text-link
334340
var symbol = ''
335341
norm! f]
@@ -352,10 +358,14 @@ export def OpenLink(is_split: bool = false)
352358
else
353359
link = utils.GetTextObject('i(').text
354360
endif
361+
else
362+
const link_interval = values(IsLink())[0]
363+
const start = link_interval[0][1] - 1
364+
const length = link_interval[1][1] - link_interval[0][1] + 1
365+
link = strcharpart(getline('.'), start, length)
355366
endif
356367

357-
358-
368+
# COMMON
359369
# Assume that a file is always small (=1 byte) is no large_file_support is
360370
# enabled
361371
const file_size = link =~ '^file://' && large_files_threshold > 0
@@ -432,7 +442,8 @@ enddef
432442
export def RemoveLink(range_info: dict<list<list<number>>> = {})
433443
const link_info = empty(range_info) ? IsLink() : range_info
434444
# TODO: it may not be the best but it works so far
435-
if !empty(link_info)
445+
echom "link_info: " .. string(keys(link_info))
446+
if !empty(link_info) && keys(link_info)[0] != 'markdownUrl'
436447
const saved_curpos = getcurpos()
437448
# Start the search from the end of the text-link
438449
norm! f]
@@ -734,7 +745,9 @@ export def PreviewPopup()
734745
var previewText = []
735746
var link_name = ''
736747
const saved_curpos = getcurpos()
737-
if !empty(IsLink())
748+
const link_info = IsLink()
749+
# CASE 1: on an alias
750+
if !empty(link_info) && keys(link_info)[0] != 'markdownUrl'
738751
# Search from the current cursor position to the end of line
739752
# Start the search from the end of the text-link
740753
norm! f]
@@ -760,20 +773,19 @@ export def PreviewPopup()
760773
else
761774
link_name = utils.GetTextObject('i(').text
762775
endif
763-
endif
764-
765-
# Show preview also if the cursor is on the reference Section
766-
if synIDattr(synID(line("."), charcol("."), 1), "name") == 'markdownUrl'
767-
execute "normal! T "
768-
link_name = utils.GetTextObject('$').text
776+
# CASE 2: on an actual link, like those in the reference Section
777+
elseif keys(link_info)[0] == 'markdownUrl'
778+
const link_interval = values(link_info)[0]
779+
const start = link_interval[0][1] - 1
780+
const length = link_interval[1][1] - link_interval[0][1] + 1
781+
link_name = strcharpart(getline('.'), start, length)
769782
endif
770783

771784
if !empty(link_name)
772785
# TODO At the moment only .md files have syntax highlight.
773786
var refFiletype = $'{fnamemodify(link_name, ":e")}' == 'md'
774787
? 'markdown'
775788
: 'text'
776-
# echom GetFileSize(link_name)
777789
const file_size = !IsURL(link_name) && large_files_threshold > 0
778790
? GetFileSize(link_name)
779791
: 0

0 commit comments

Comments
 (0)