Comments (13)
This is an existing feature: Place the point anywhere in the inline link and press C-c C-a r (as if you were inserting a reference link) and the link will be converted to a reference style link.
from markdown-mode.
btw, i don't know what markdown you are using, but with pandoc you can just do [foo]
instead of [foo][]
.
from markdown-mode.
damn, i was offline and ended up rewriting this thing twice... here's what i ended up with:
(defun anarcat/markdown-convert-link ()
"convert a [text](link) link format into [text][] ... [text]:
link
allowing search-backwards (say with a prefix) could also be
interesting.
This is discussed in
https://github.com/jrblevin/markdown-mode/issues/94 and should be
eventually merged upstream once it is acceptable elisp.
"
(interactive)
;; find unconverted link
;; we have 3 groups in here:
;; 1. text (without brakets)
;; 2. link (with parens, to replace it with replace-match)
;; 3. link (without parens, to extract it to insert it later)
(re-search-forward "\\[\\([^]]*\\)]\\((\\([^)]*\\))\\)")
(let ((text (match-string 1))
(link (match-string 3)))
;; remove the link and replace with brakets
;; link is the "2" below, and replace only that
(replace-match "[]" nil nil nil 2)
;; go at the end of the paragraph and add stuff there
(save-excursion
(search-forward "\n\n")
;; insert the link reference
(insert (format " [%s]: %s\n\n" text link))
;; XXX: we insert two newlines to avoid breaking the paragraph
;; break, but that makes too many spaces if we insert many links
;; like this. not sure how to fix this.
)
)
)
also, C-c C-a r
does not convert a link here: it just adds a new one. i am using markdown-mode.el 2.0 from Debian jessie, looking at the current code it seems this is a new feature from 2.1... grml.
oh well, i gues it's time to load a new version from ELPA. :)
@vyp as for the markdown dialect, i'm writing for ikiwiki, using the default markdown processor (which is not pandoc).
from markdown-mode.
markdown-mode is still at version 2.0 in marmalade, it seems. MELPA has a snapshot from 2016, which works, so I guess i just wasted time re-learning elisp programming. which was actually nice. :)
thanks!
from markdown-mode.
ah - one thing my function does a little better is that it puts the link at the end of the paragraph instead of at the end of file, which I like because it makes the plain text more readable: you don't have to jump to the end of file to follow links by hand.
from markdown-mode.
You can customize the location for inserting reference definitions by setting markdown-reference-location
.
from markdown-mode.
cool, that's what i was looking for, thanks!
from markdown-mode.
This is an existing feature: Place the point anywhere in the inline link and press C-c C-a r (as if you were inserting a reference link) and the link will be converted to a reference style link.
Is it me or did this feature disappear?
If I have a document that only has [foo](link)
and I move the point to "link" then type C-c C-a r, it just prompts me for the same link info again but doesn't convert the link...
Ideas?
from markdown-mode.
I combined the three old functions (for inline, reference, plain URL links) into one with the new C-c C-l command. The old commands are now aliases for it. If you remove the URL (kill it for using later), then give a reference label it will convert for you. If the ref isn't defined, one will be added and you'll be prompted for the URL (which you can then yank).
The idea is that it uses whatever information you give it to determine the link type. There are examples of other possible conversions in the documentation.
from markdown-mode.
oh i see. sorry for not catching up on the docs and all the noise. ;) short too, i like it. i'll try to train my muscle memory back in polace - thanks!
from markdown-mode.
It's no problem. My sense at the time was that C-c C-l would be more intuitive, but I'm considering bringing back dedicated conversion commands, as they do avoid shuffling URLs/text around.
from markdown-mode.
If you remove the URL (kill it for using later), then give a reference label it will convert for you. If the ref isn't defined, one will be added and you'll be prompted for the URL (which you can then yank).
I understand this ticket is getting really old by now, but I keep on thinking about it every time I switch links between inline and ref. That process actually takes a long time. The actual keystrokes are:
- C-c
- C-l
- alt-backspace (orc-s-left c-w)
- RET
- RET
- c-y
- RET
phew! i often end up doing a macro instead, but it's not very reliable: if a ref already exist, it kind of explodes midway...
it would still be nice to have a wrapper around this, possibly by setting a prefix argument?
similar: #565
from markdown-mode.
i reworked my code a bit and ended up with something like this:
(defun anarcat/markdown-convert-link ()
"Convert markdown inline links to references.
This convers a [text](link) link format into [text][] ... [text]:
linké
Allowing search-backwards (say with a prefix) could also be
interesting.
This is discussed in:
https://github.com/jrblevin/markdown-mode/issues/94
Should eventually merged upstream once it is acceptable elisp."
(interactive)
;; find unconverted link
;; we have 3 groups in here:
;; 1. text (without brakets)
;; 2. link (with parens, to replace it with replace-match)
;; 3. link (without parens, to extract it to insert it later)
(if (re-search-forward "\\[\\([^]]*\\)]\\((\\([^)]*\\))\\)" nil t)
(let ((text (match-string 1))
(link (match-string 3)))
;; remove the link and replace with brakets
;; link is the "2" below, and replace only that
(replace-match "[]" nil nil nil 2)
;; go at the end of the paragraph and add stuff there
(save-excursion
(search-forward "\n\n" nil 1)
;; insert the link reference
(insert (format " [%s]: %s\n\n" text link))
;; XXX: we insert two newlines to avoid breaking the paragraph
;; break, but that makes too many spaces if we insert many links
;; like this. not sure how to fix this.
))
(message "no inline link found until end of buffer")))
would you be open a merge request to add that to markdown-mode?
from markdown-mode.
Related Issues (20)
- Add support for LaTeX enviroments HOT 1
- Markdown mode leads to heavy input lag HOT 11
- [filelink](does not support spaces in filename) HOT 3
- Caret signs inside math are fontified as Markdown markup
- register yank-media-handler so we can paste images HOT 7
- Should filename links be URI-decoded? HOT 1
- Update MacPorts links in README
- tab in lists items results in invalid list indent itemization
- [Feature Suggestion] markdown-beginning/end-of-line functions
- `move-beginning-of-line` move to wrong position on heading when markdown-hide-markup is enabled HOT 1
- preserve indent-tabs-mode in markdown-edit-code-block HOT 5
- Incorrect table alignment with code block HOT 1
- Markdown hiding for links broken when text surrounded by square brackets
- resurrect emacs-26 support? HOT 2
- Bug: Angle-bracketed URLs in inline code are fontified as links
- "_" character in a variable name turns on underline HOT 1
- Wrong gfm-view-mode syntax highlight if only using *-ts-mode (tree-sitter modes) HOT 1
- v2.6 ERT test failures on wiki-link-rules and wiki-link-search-under-project with Emacs 29.1 HOT 4
- R language isn't included in markdown-gfm-recognized-languages HOT 3
- Wrong table width calculation for Unicode Combining Diacritical Marks
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from markdown-mode.