@@ -2379,6 +2379,80 @@ Hyperlink types supported:
2379
2379
- Headline title target within the same file (starts with `* ` ) (Example: `*Specific headline`)
2380
2380
- Headline with `CUSTOM_ID` property within the same file (starts with `#` ) (Example: `#my- custom - id` )
2381
2381
- Fallback: If file path, opens the file, otherwise, tries to find the headline title in the current file.
2382
+ - Your own custom type (| orgmode-see-below | )
2383
+
2384
+
2385
+ Custom hyperlink types *orgmode-Custom-hyperlink-types*
2386
+
2387
+ To add your own custom hyperlink type, provide a custom handler to
2388
+ `hyperlinks.sources` setting. Each handler needs to have a `get_name ()` method
2389
+ that returns a name for the handler. Additionally, `follow (link)` and
2390
+ `autocomplete (link)` optional methods are available to open the link and
2391
+ provide the autocompletion. Here's an example of adding a custom "ping"
2392
+ hyperlink type that opens the terminal and pings the provided URL and provides
2393
+ some autocompletion with few predefined URLs:
2394
+
2395
+ >lua
2396
+ local LinkPingType = {}
2397
+
2398
+ ---Unique name for the handler. MUST NOT be one of these: "http", "id", "line_number", "custom_id", "headline"
2399
+ ---This method is required
2400
+ ---@return string
2401
+ function LinkPingType:get_name()
2402
+ return 'ping'
2403
+ end
2404
+
2405
+ ---This method is in charge of "executing" the link. For "http" links, it would open the browser, for example.
2406
+ ---In this example, it will open the terminal and ping the value of the link.
2407
+ ---The value of the link is passed as an argument.
2408
+ ---For example, if you have a link [[ping:google.com][ping_google]], doing an `org_open_at_point` (<leader> oo by default)
2409
+ ---anywhere within the square brackets, will call this method with `ping: google .com ` as an argument.
2410
+ ---It's on this method to figure out what to do with the value.
2411
+ ---If this method returns `true` , it means that the link was successfully followed.
2412
+ ---If it returns `false` , it means that this handler cannot handle the link, and it will continue to the next source.
2413
+ ---This method is optional.
2414
+ ---@param link string - The current value of the link, for example: "ping:google.com"
2415
+ ---@return boolean - When true, link was handled, when false, continue to the next source
2416
+ function LinkPingType:follow(link)
2417
+ if not vim.startswith(link, 'ping:') then
2418
+ return false
2419
+ end
2420
+ -- Get the part after the `ping: ` part
2421
+ local url = link:sub(6)
2422
+ -- Open terminal in vertical split and ping the URL
2423
+ vim.cmd('vsplit | term ping ' .. url)
2424
+ return true
2425
+ end
2426
+
2427
+ ---This is an optional method that will provide autocompletion for your link type.
2428
+ ---This method needs to pre-filter the list of possible completions based on the current value of the link.
2429
+ ---For example, if this source has `ping: google .com ` and `ping: github .com ` as possible completions,
2430
+ ---And the current value of the link is `ping: go ` , this method should return `{' ping:google.com' }` .
2431
+ ---This method is optional.
2432
+ ---@param link string - The current value of the link, for example: "ping:go"
2433
+ ---@return string[]
2434
+ function LinkPingType:autocomplete(link)
2435
+ local items = {
2436
+ 'ping:google.com',
2437
+ 'ping:github.com'
2438
+ }
2439
+ return vim.tbl_filter(function(item) return vim.startswith(item, link) end, items)
2440
+ end
2441
+
2442
+ require('orgmode' ).setup({
2443
+ hyperlinks = {
2444
+ sources = {
2445
+ LinkPingType,
2446
+ -- Simpler types can be inlined like this:
2447
+ {
2448
+ get_name = function() return 'my_custom_type' end,
2449
+ follow = function(self, link) print('Following link:', link) return true end,
2450
+ autocomplete = function(self, link) return {'my_custom_type:my_custom_link'} end
2451
+ }
2452
+ }
2453
+ }
2454
+ })
2455
+ <
2382
2456
2383
2457
2384
2458
Notifications *orgmode-Notifications*
0 commit comments