Git Product home page Git Product logo

Comments (13)

jrblevin avatar jrblevin commented on July 2, 2024 1

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.

vyp avatar vyp commented on July 2, 2024

btw, i don't know what markdown you are using, but with pandoc you can just do [foo] instead of [foo][].

from markdown-mode.

anarcat avatar anarcat commented on July 2, 2024

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.

anarcat avatar anarcat commented on July 2, 2024

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.

anarcat avatar anarcat commented on July 2, 2024

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.

jrblevin avatar jrblevin commented on July 2, 2024

You can customize the location for inserting reference definitions by setting markdown-reference-location.

from markdown-mode.

anarcat avatar anarcat commented on July 2, 2024

cool, that's what i was looking for, thanks!

from markdown-mode.

anarcat avatar anarcat commented on July 2, 2024

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.

jrblevin avatar jrblevin commented on July 2, 2024

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.

anarcat avatar anarcat commented on July 2, 2024

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.

jrblevin avatar jrblevin commented on July 2, 2024

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.

anarcat avatar anarcat commented on July 2, 2024

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.

anarcat avatar anarcat commented on July 2, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.