@@ -121,6 +121,16 @@ def GetFileSize(filename: string): number
121121 return filesize- >substitute (' \n' , ' ' , ' g' )- >str2nr ()
122122enddef
123123
124+ def LastReferenceLine (): number
125+ const saved_curpos = getcursorcharpos ()
126+ cursor (' $' , 1 )
127+ # Search backwards line starting with e .g . ' [32]: '
128+ const lastline = search (' ^\s*\[\d\+\]:\s\+' , ' bW' )
129+ setpos (' .' , saved_curpos)
130+ return lastline
131+ enddef
132+
133+
124134export def RefreshLinksDict (): dict <string>
125135 # Generate the b: markdown_extras_links by parsing the ' references_comment'
126136 # Section.
@@ -134,8 +144,11 @@ export def RefreshLinksDict(): dict<string>
134144 # Cleanup the current b: markdown_extras_links
135145 var links_dict = {}
136146 const references_line = search ($ ' ^{references_comment}' , ' nw' )
147+ # UBA to check
148+ const lastline = LastReferenceLine () == 0 ? line (' $' ) : LastReferenceLine ()
149+
137150 if references_line != 0
138- for l in range (references_line + 1 , line ( ' $ ' ) )
151+ for l in range (references_line + 1 , lastline + 1 )
139152 var ref = getline (l )
140153 if ! empty (ref)
141154 var key = ref- >matchstr (' \[\zs\d\+\ze\]' )
@@ -195,7 +208,10 @@ def GetLinkID(): number
195208 if link_id == 1
196209 append (line (' $' ), ' ' )
197210 endif
198- append (line (' $' ), $ ' [{link_id}]: {link}' )
211+ const lastline = LastReferenceLine () == 0 ? line (' $' ) : LastReferenceLine ()
212+ if lastline != 0
213+ append (lastline, $ ' [{link_id}]: {link}' )
214+ endif
199215 else
200216 # Reuse existing link
201217 var tmp = getline (link_line)- >substitute (' \v^\[(\d*)\].*' , ' \1' , ' ' )
@@ -417,8 +433,10 @@ export def ConvertLinks()
417433
418434 # Fix dict
419435 b: markdown_extras_links [link_id] = link
420- var lastline = line (' $' )
421- append (lastline, $ ' [{link_id}]: {link}' )
436+ const lastline = LastReferenceLine ()
437+ if lastline != 0
438+ append (lastline, $ ' [{link_id}]: {link}' )
439+ endif
422440 # TODO Find last proper line
423441 # var line = search (' \s*#\+\s*References' , ' n' ) + 2
424442 # var lastline = -1
@@ -442,7 +460,6 @@ enddef
442460export def RemoveLink (range_info: dict <list<list<number> >> = {})
443461 const link_info = empty (range_info) ? IsLink () : range_info
444462 # TODO : it may not be the best but it works so far
445- echom " link_info: " .. string (keys (link_info))
446463 if ! empty (link_info) && keys (link_info)[0 ] != ' markdownUrl'
447464 const saved_curpos = getcurpos ()
448465 # Start the search from the end of the text- link
0 commit comments