Reputation: 111
I want to change hyperlinks to Markdown Hyperlinks all at once.
For example I have (and more of them)
https://stackoverflow.com/questions/ask
https://stackoverflow.com/questions/59030873/keyboard-shortcut-in-vscode-for-markdown-links
https://stackoverflow.com/questions/ask
https://stackoverflow.com/questions/59030873/keyboard-shortcut-in-vscode-for-markdown-links
and want to have:
[ask](https://stackoverflow.com/questions/ask)
[keyboard-shortcut-in-vscode-for-markdown-links](https://stackoverflow.com/questions/59030873/keyboard-shortcut-in-vscode-for-markdown-links)
[ask](https://stackoverflow.com/questions/ask)
[keyboard-shortcut-in-vscode-for-markdown-links](https://stackoverflow.com/questions/59030873/keyboard-shortcut-in-vscode-for-markdown-links)
I already found (for keybindings.json)
{
"key": "ctrl+q b", // Lines 2 Array Only
"command": "editor.action.insertSnippet",
"args": {
"snippet": "${TM_SELECTED_TEXT/(.+)(\\r?(?=\\S))?/[]($1)/g}",
},
"when": "editorTextFocus && editorLangId == 'markdown'"
}
which does a lot, but maybe anybody can add or give me a possibility to find out myself:
tia
Upvotes: 1
Views: 292
Reputation: 182121
I don't think using a snippet will work (or I can't figure it out) because of your requirement to only change links if there is any text following the link. That makes TM_SELECTED_TEXT
not work. But it can be done with a find and replace pretty easily.
I wrote an extension Find and Transform that you can use to save the find/replace for future use. Example keybinding:
{
"key": "alt+q", // whatever keybinding you want
"command": "findInCurrentFile",
"args": {
// "find": "(^https:.+\\/)(.*$)(\r?\n)(?=(?!https:))", // if single line of text between links
"find": "(.+\\/)(.*$)(\r?\n)(?=\\s*(?!https:)[\\s\\S]*^https:)", // if multiple lines of text
"replace": "[$2]($1$2)$3",
"restrictFind": "selections" // if you want to only find within selections
},
"when": "editorTextFocus && editorLangId == 'markdown'"
}
I believe that satisfies your requirement that some text (I made it for any amount of lines of intervening text) be between links. And thus the last link - which may have text after it - but no following link - should not be transformed.
[If there is only ever one line of text between links, the regex could be simplified quite a bit.]
Upvotes: 1