larstvei / ox-gfm

Github Flavored Markdown Back-End for Org Export Engine
230 stars 44 forks source link

ox-gfm change ".org" to ".md" in links to local files #23

Closed kyeongsoo closed 7 years ago

kyeongsoo commented 7 years ago

The title says it all: If I have

[[./file.org][file]]

in an org file, it is exported to

[file](./file.md)

in the exported md file.

Now I have to manually change back those extensions after each export.

kyeongsoo commented 7 years ago

Per larstvei's answer in #21, I did directly check 'ox-md' and found that the following internal function replaces '.org' to '.md' in nearly every type of links:

(let ((link-org-files-as-md
     (lambda (raw-path)
       ;; Treat links to `file.org' as links to `file.md'.
       (if (string= ".org" (downcase (file-name-extension raw-path ".")))
           (concat (file-name-sans-extension raw-path) ".md")
         raw-path)))

Unfortunately, there is no option to change this behavior, so I ended up with the following workaround:

  (let ((link-org-files-as-md
         (lambda (raw-path)
           raw-path))        ; disable translation of '.org' to '.md'
       ;; ;; Treat links to `file.org' as links to `file.md'.
       ;; (if (string= ".org" (downcase (file-name-extension raw-path ".")))
       ;;     (concat (file-name-sans-extension raw-path) ".md")
       ;;   raw-path)))

Still, I don't understand why this translation is hard-coded as default and cannot be changed or customized at all. At least, however, I hope that the above workaround could be of any help to those in a similar situation.

larstvei commented 7 years ago

Hi!

Thanks for filing the issue! I agree this default is pretty bonkers. I would suggest that you send an email to the orgmode mailing list if you haven't already 👍

jgkamat commented 7 years ago

@kyeongsoo this is expected behavior, as far as I know, in order to keep org projects working properly.

Suppose you have an org project of two files, one.org and two.org. When you export this project to md, org generates a one.md and two.md. Any links from one.org to two.org need to be converted to two.md, else, the links would suddenly be broken (since the exported files can be in a different location altogether!). Leaving the links as .org would be extremely confusing and break all existing ox-publish projects.

I wrote a blog post a while back explaining how great org projects are, if you want some more information.

If you have a specific use case that requires it for some reason, please let me know and I'll try to help you out. It sounds like you are trying to implement half of ox-publish yourself from scratch :).

kyeongsoo commented 7 years ago

@jgkamat Great thanks for the information. In fact, I, too, have been using org-mode for nearly everything, including my home page, TODO lists, and preparation of papers and slides.

Your explanation, however, convinces me that there should be different behaviors depending on the export mode.

While, as you correctly said, publishing the whole project (i.e., C-c C-e P p) requires the current behavior, exporting an individual file as in my usage case (i.e., C-c C-e m m/M or C-c C-e g g/G) shouldn't replace those org links. In the latter case, because there is no project setup (i.e., just a single org file at hand), converting every link to org file to that of md does not make sense; for instance, how about the external org files in those links which are not under your project/control?

jgkamat commented 7 years ago

@kyeongsoo hmm, that makes a lot of sense for a use case. It's a little tricky though, since having an export via the individual file vs export via project be different in such a subtle way is bound to trick people. If you think that should be the default, you should send a mail to the mailing list (emacs-orgmode@gnu.org).

I took a look at the ox-html export and it seems to have a org-html-link-org-files-as-html config option which seems like what you would want. It woudn't change the behavior based on the different types of exports, but it's much closer to what you want (and you can test it out right now with the html exporter). Would implementing a org-md-link-org-files-as-md config option help you? I could submit a patch to add that to ox-md if you'd like.

related code from ox-html:

      (lambda (raw-path info)
        ;; Treat links to `file.org' as links to `file.html', if
        ;; needed.  See `org-html-link-org-files-as-html'.
        (cond
         ((and (plist-get info :html-link-org-files-as-html)
           (string= ".org"
                (downcase (file-name-extension raw-path "."))))
          (concat (file-name-sans-extension raw-path) "."
              (plist-get info :html-extension)))
         (t raw-path)))