122 lines
4.8 KiB
EmacsLisp
122 lines
4.8 KiB
EmacsLisp
;;; qwan/qwan-blog.el -*- lexical-binding: t; -*-
|
|
;;;
|
|
|
|
(provide 'qwan-blog)
|
|
|
|
|
|
(defun qwan/copy-filename ()
|
|
"Push the file name of the currently visited buffer to the kill ring. Useful for markdown links"
|
|
(interactive)
|
|
(unless buffer-file-name
|
|
(error "The current buffer doesn't visit a file"))
|
|
|
|
(kill-new buffer-file-name)
|
|
(message "Copied %s" buffer-file-name))
|
|
|
|
(defun qwan/publish-draft ()
|
|
"Publish this in the _posts folder"
|
|
(interactive)
|
|
(let* ((dest-dir "../_posts")
|
|
(current-file (buffer-file-name))
|
|
(blogpost-name (file-name-nondirectory current-file))
|
|
(current-date (format-time-string "%Y-%m-%d"))
|
|
(new-filename (concat current-date "-" blogpost-name))
|
|
(source-path current-file)
|
|
(dest-path (concat dest-dir "/" new-filename)))
|
|
(save-buffer)
|
|
(rename-file source-path dest-path)
|
|
(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 strip-url (url)
|
|
"Remove everything after ? from the url, to hinder tracking."
|
|
(replace-regexp-in-string "\\?.*$" "" url))
|
|
|
|
;; TODO define key for this maybe SPC q is available? no, that is quit.
|
|
(defun qwan/paste-stripped-url ()
|
|
"Paste a URL without tracking from the clipboard into the current buffer."
|
|
(interactive)
|
|
(let ((url (x-get-selection 'CLIPBOARD)))
|
|
(when url
|
|
(insert (strip-url url)))))
|
|
|
|
(defun qwan/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")
|
|
"")))
|