REfactor qwan functions from config.el, esp. for blogging
This commit is contained in:
@@ -27,3 +27,82 @@
|
||||
(set-visited-file-name dest-path)
|
||||
(set-buffer-modified-p nil)
|
||||
(message "Successfully moved %s to %s" source-path dest-path)))
|
||||
|
||||
;; functions to convert emacs org-roam towards markdown without internal links
|
||||
(defun qwan/org-replace-link-by-link-description ()
|
||||
"Remove the link part of an org-mode link at point and keep
|
||||
only the description"
|
||||
(interactive)
|
||||
(let ((elem (org-element-context)))
|
||||
(if (eq (car elem) 'link)
|
||||
(let* ((content-begin (org-element-property :contents-begin elem))
|
||||
(content-end (org-element-property :contents-end elem))
|
||||
(link-begin (org-element-property :begin elem))
|
||||
(link-end (org-element-property :end elem)))
|
||||
(if (and content-begin content-end)
|
||||
(let ((content
|
||||
(buffer-substring-no-properties
|
||||
content-begin content-end)))
|
||||
(delete-region link-begin (- link-end 1))
|
||||
(insert content)))))))
|
||||
(bind-key "C-c r" 'qwan/org-replace-link-by-link-description)
|
||||
|
||||
(defun org-roam-replace-file-link-by-website-or-description ()
|
||||
"If a linked org-roam file has a website as key,
|
||||
use that as the link target instead of the filename.
|
||||
Otherwise, remove the link, and leave just the description.
|
||||
This may destroy existing links, or change their targets so
|
||||
use in a copy of buffers you hold dearly as notes"
|
||||
(interactive)
|
||||
(let ((elem (org-element-context)))
|
||||
(if (eq (car elem) 'link)
|
||||
(let* ((content-begin (org-element-property :contents-begin elem))
|
||||
(content-end (org-element-property :contents-end elem))
|
||||
(target-type (org-element-property :type elem))
|
||||
(target-path (org-element-property :path elem))
|
||||
(link-begin (org-element-property :begin elem))
|
||||
(link-end (org-element-property :end elem)))
|
||||
(if (and content-begin
|
||||
content-end
|
||||
(string-equal target-type "file")
|
||||
target-path
|
||||
link-begin
|
||||
link-end)
|
||||
(let ((new-link
|
||||
(org-roam-description-file-to-org-url
|
||||
(buffer-substring-no-properties content-begin content-end)
|
||||
target-path)))
|
||||
(delete-region link-begin (- link-end 1))
|
||||
(insert new-link)))))))
|
||||
(defun qwan/string-as-markdown-image-link (a-string)
|
||||
(concat ""))
|
||||
(defun qwan/image-mode-copy-filename-as-markdown-image-link ()
|
||||
"Push a markdown link to the currently visited image onto the kill ring."
|
||||
(interactive)
|
||||
(unless buffer-file-name
|
||||
(error "The current buffer doesn't visit an image file"))
|
||||
(let ((markdown-link
|
||||
(qwan/string-as-markdown-image-link
|
||||
(qwan/relative-path-to-attachments buffer-file-name))))
|
||||
(kill-new markdown-link)
|
||||
(message "Copied %s" markdown-link)))
|
||||
|
||||
(defun qwan/relative-path-to-attachments (full-path)
|
||||
(substring full-path (string-search "/attachments" full-path 0)))
|
||||
(require 'ert)
|
||||
(ert-deftest relative-path-to-attachments-removes-prefix ()
|
||||
(should
|
||||
(equal
|
||||
(qwan/attachments-remove-prefix-path "prefix/attachments") "/attachments")))
|
||||
(ert-deftest
|
||||
relative-path-to-attachments-does-not-remove-prefix-when-no-attachments
|
||||
()
|
||||
(should
|
||||
(equal
|
||||
(qwan/attachments-remove-prefix-path "prefix/somethingelse")
|
||||
"prefix/somethingelse")))
|
||||
(ert-deftest markdown-link-tokens-should-surround-string ()
|
||||
(should
|
||||
(equal
|
||||
(qwan/string-as-markdown-image-link "prefix/image.png")
|
||||
"")))
|
||||
|
||||
Reference in New Issue
Block a user