比如 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/)"