从 URL 构建 Markdown 行内链接

比如 https://emacs-china.org,我想得到对应的 Markdown 链接:

  • [Emacs China](https://emacs-china.org)

代码:

(defun chunyang-markdown-link (url)
  (interactive (list (read-string "URL: " (thing-at-point 'url))))
  (let (title markdown)
    (with-current-buffer (url-retrieve-synchronously url)
      (set-buffer-multibyte t)
      (let ((dom (libxml-parse-html-region url-http-end-of-headers (point-max))))
        (require 'dom)
        (setq title (dom-text (car (dom-by-tag dom 'title))))))
    (setq markdown (format "[%s](%s)" title url))
    (kill-new markdown)
    (message "Copied: %s" markdown)
    markdown))

应用:

(chunyang-markdown-link "https://emacs-china.org")
     => "[Emacs China](https://emacs-china.org)"

(chunyang-markdown-link "https://www.wikipedia.org/")
     => "[Wikipedia](https://www.wikipedia.org/)"

emmet 已经有这个功能了,我记得。你找找。

不过弄个通用的,还是挺好的。

直接调用 Emmet 插件的函数更方便点,体验更好。如果添加更多文件类型支持,也是可以的。