Skip to content

Commit 9ef68d4

Browse files
authored
Merge pull request #9 from ubaldot/improved_MDEIndex
Improved mde index
2 parents b468f7a + 6a4e1ec commit 9ef68d4

File tree

5 files changed

+58
-20
lines changed

5 files changed

+58
-20
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -154,10 +154,10 @@ following:
154154
### Indices
155155

156156
As the plugin can be used for note-taking, it may be desirable to access
157-
different indices in an ergonomic way. This can be achieved with the
158-
`g:markdown_extras_indices` list in combination with the `:MDEIndices`
159-
command. Note that `g:markdown_extras_indices` is nothing, but a list or a
160-
dict of files.
157+
different indices in an ergonomic way. This can be achieved with the command
158+
`:MDEIndex` that takes `list<string>`, `list<list<string>>` or
159+
`dict<string>` as argument. If no argument is passed, it reads the content
160+
of `g:markdown_extras_indices`. See `:h MDEIndex` for more info.
161161

162162
For more information about key-bindings, configuration, etc. take a look at
163163
`:h markdown-extras`.

autoload/mde_indices.vim

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,15 @@ def IndicesCallback(id: number, idx: number)
2727
exe $'edit {fnameescape(links.URLToPath(selection))}'
2828
elseif links.IsURL(selection)
2929
exe $'Open {selection}'
30-
else
30+
elseif filereadable(fnameescape(selection))
3131
exe $'edit {fnameescape(selection)}'
32+
elseif selection =~ "^function("
33+
try
34+
var Tmp = eval(selection)
35+
Tmp()
36+
catch
37+
utils.Echoerr("Function must be global")
38+
endtry
3239
endif
3340
endif
3441

@@ -38,7 +45,6 @@ enddef
3845

3946
export def ShowIndices(passed_indices: string='')
4047
var indices_found = false
41-
echom typename(passed_indices)
4248

4349
if !empty(passed_indices)
4450
# TODO: remove the eval() with something better

doc/markdown_extras.txt

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -251,26 +251,35 @@ COMMANDS *markdown-extras-commands*
251251
:MDEConvertLinks Convert inline links into reference-style links.
252252

253253
*:MDEIndex*
254-
:MDEIndex [{args}] Open a popup with all the URLs specified in {args}.
254+
:MDEIndex [{args}] Open a popup with what is specified in {args}.
255255
The type of {args} can be list<string>, list<list<string>>
256256
or dict<string>. If no argument is passed, then
257257
|g:markdown_extras_indices| is considered.
258258
Example:
259259
>
260-
:MDEIndex # Uses g:markdown_extras_indices
261-
:MDEIndex ['path1', 'path2', 'path3]
262-
:MDEIndex {foo: 'path1', bar: 'path2', baz: 'path3}
263-
:MDEIndex [['foo', 'path1'], ['bar', 'path2'], ['baz', 'path3]]
260+
:MDEIndex # Uses g:markdown_extras_indices
261+
:MDEIndex ['local/path', 'some%20url', 'function("some_Funcref")']
262+
:MDEIndex {foo: 'local/path', bar: 'some%20url', 'function("some_Funcref")'}
263+
:MDEIndex [['foo', 'local/path'], ['bar', 'some%20url'],
264+
['baz', 'function("some_Funcref")']]
264265
<
265-
Or you can use in a script, like for example:
266+
Or it can be used in a script, like for example:
266267
>
267-
const my_index = {
268-
foo: '/Users/ubaldot/home/Documents/foo.vim',
269-
bar: 'https://example.com',
270-
banana: '/mnt/c/files/bar.md'
271-
}
272-
execute $"MDEIndex {my_index}"
268+
def Foo()
269+
const my_index = {
270+
foo: '/Users/ubaldot/home/Documents/foo.vim',
271+
bar: 'https://example.com',
272+
banana: 'function("g:Foo")'
273+
}
274+
execute $"MDEIndex {my_index}"
275+
enddef
276+
277+
command! MyIndex Foo()
273278
<
279+
The string used in the various cases can be a local path,
280+
a valid URL or a function reference.
281+
In the latter case, the referenced function must be global
282+
and shall take no arguments.
274283

275284
*:MDEMake*
276285
:MDEMake Invoke |:make| and |:Open| the rendered file. It requires

plugin/markdown_extras.vim

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ Typically, they are quite accurate.
4242
2. `:MDEIndices` has been renamed to `:MDEIndex` and can take an argument, for
4343
example you can call as `:MDEIndices ['apple', 'banana', 'strawberry']`.
4444
Furthermore, such a command is no longer valid only for markdown files but
45-
it become global.
45+
it become global. See `:h :MDEIndex` for more info.
4646

4747
Press <Esc> to close this popup.
4848
FOO
@@ -125,4 +125,4 @@ endif
125125
command! -nargs=0 MDEReleaseNotes ShowReleaseNotes()
126126
command! -nargs=? MDEIndex indices.ShowIndices(<f-args>)
127127

128-
g:markdown_extras_loaded = false
128+
g:markdown_extras_loaded = true

test/index_manual_test.vim

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
vim9script
2+
3+
message clear
4+
5+
def g:Foo()
6+
echom "Fatto!"
7+
enddef
8+
9+
var test = [
10+
["file", 'C:\Users\yt75534\vimfiles\plugins\vim-markdown-extras\testfile.md'],
11+
["link", 'https://example.com'],
12+
["func", "function('g:Cazzo')"],
13+
["empty", ""],
14+
["wrong_type", 12234],
15+
["func_lambda", string(() => 'echo "ECCOLA!"')],
16+
]
17+
18+
var test_safe = [
19+
["file", 'C:\Users\yt75534\vimfiles\plugins\vim-markdown-extras\testfile.md'],
20+
["link", 'https://example.com'],
21+
["global_func", "function('g:Foo')"],
22+
]
23+
execute $"MDEIndex {test_safe}"

0 commit comments

Comments
 (0)