Khalid Rafi <[email protected]> writes:
> Currently, ox-md can't export links verbatim. For example, if I have a
> link like [[/en/install-emacs-on-android][install Emacs]], it will
> be treated as a file: link. But, it's a common case in Markdown blogging
> to use such links as relative to authors' site root domain. I think we can
> add an option for when the file doesn't exist to export it without file:
> prefix. We can also let users choose if links without file: and starting
> with a / should be prepended with file: prefix.
+1 for the feature request, but I would go a different route and have
this be directory-dependent. We could perhaps let root-relative html
links be the automatic behavior for file links matching the
:base-directory of a current publishing process, as well as for any
other directories included in a new custom variable
('org-html-root-dirs'?).
A lengthy discussion to make clear what I mean follows (including a
workaround you might find useful).
The problem will typically only be relevant for one or a few base
directories where you keep the files you want to publish to your
blog(s)/website(s). Say your user is "user", your base directory is
"~/www-source", and your site is "mysite.com"; then in your example you
have the file
/home/user/www-source/en/install-emacs-on-android.org
and want to link to it from another file inside www-source so that the
exported link becomes
/en/install-emacs-on-android
and, when clicked from a page on mysite.com, takes the reader to
https://mysite.com/en/install-emacs-on-android.html
This could also be done with relative links, but relative links get very
cumbersome ('../../../' etc.) if you want to link to this page from,
say, a place like
/home/user/www-source/fr/blog/2026/05/28/installer-emacs.html
There are several possible ways of achieving what you want to do.
In another thread, Earl Chase has proposed a new link type.
https://list.orgmode.org/ca+hmbpo_ux7t315p4hs7dnp0spz2e2ryghj-raxmupovouw...@mail.gmail.com
As a workaround, I have been using an export filter, and you could
probably just adapt it by replacing "/user/www-source/" with any regexp
that uniquely matches the path to your own base directory. This lets you
use links like "[[/en/install-emacs-on-android]]", precisely as you wish.
(defun my-filter-root-links (output backend info)
"Fix any root-relative references in links."
(when (and (eq backend 'html)
(string-match "/user/www-source/" (buffer-file-name)))
(replace-regexp-in-string "file:///" "/" output)))
(add-to-list 'org-export-filter-link-functions
'my-filter-root-links)
However, I no longer think this is a good general solution. It is useful
to be able to add links between .org blog files with 'C-u C-c C-l' file
link completion, and to be able to follow them while editing. Taken
together, this means using links like
"[[file:~/www-source/en/install-emacs-on-android.org]]" instead.
So I think a better way to handle this in Org is to use Org file links
as normal but add a feature to the link export that replaces the path to
a publishing base directory, here the "file:///home/me/www-source" bit,
with "/".
Regards,
Christian