From 8ed5a5305f5ddd59a2fc5a9faa82522354c5df9d Mon Sep 17 00:00:00 2001 From: SqrtMinusOne Date: Sat, 2 Jul 2022 21:52:08 +0300 Subject: [PATCH] fix: slugs --- scripts/publish-configs.el | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/scripts/publish-configs.el b/scripts/publish-configs.el index 42453e9..668fb9c 100644 --- a/scripts/publish-configs.el +++ b/scripts/publish-configs.el @@ -31,8 +31,38 @@ :ensure t) (setq org-make-toc-link-type-fn #'org-make-toc--link-entry-org) + +(defvar-local my/org-hugo-heading-slugs nil) + +(defun my/increase-slug (string) + (if (string-match (rx "-" (1+ num) eos) string) + (replace-match + (concat + "-" + (thread-first + (match-string 0 string) + (substring 1) + (string-to-number) + (+ 1) + (number-to-string))) + t t string) + (concat string "-" "1"))) + +(defun my/org-hugo-get-heading-slug (element info) + (let* ((title (org-export-data-with-backend + (org-element-property :title element) 'md info)) + (slug (org-string-nw-p (org-hugo-slug title :allow-double-hyphens)))) + (unless my/org-hugo-heading-slugs + (setq my/org-hugo-heading-slugs (make-hash-table :test 'equal))) + (unless (or (null slug) (string-empty-p slug)) + (while (gethash slug my/org-hugo-heading-slugs) + (setq slug (my/increase-slug slug))) + (puthash slug t my/org-hugo-heading-slugs)) + slug)) + (setq org-hugo-anchor-functions '(org-hugo-get-page-or-bundle-name org-hugo-get-custom-id + my/org-hugo-get-heading-slug org-hugo-get-md5)) (setq org-hugo-section "configs")