From fd0912bcc3fc11c418a284e35bcc3c9f1647edfa Mon Sep 17 00:00:00 2001 From: Kaushal Modi Date: Mon, 14 Mar 2022 14:01:13 -0400 Subject: [PATCH] Mask broken link err during pre-process if `broken-links' is non-nil Add a broken link test. Fixes https://github.com/kaushalmodi/ox-hugo/issues/587 --- ox-hugo.el | 20 +++++++++++++------ test/site/content-org/all-posts.org | 18 +++++++++++++++++ test/site/content/posts/allow-broken-links.md | 12 +++++++++++ 3 files changed, 44 insertions(+), 6 deletions(-) create mode 100644 test/site/content/posts/allow-broken-links.md diff --git a/ox-hugo.el b/ox-hugo.el index 0033a6e4..d8947aad 100644 --- a/ox-hugo.el +++ b/ox-hugo.el @@ -2117,8 +2117,8 @@ INFO is a plist used as a communication channel." (org-export-get-environment) ;Eval #+bind keywords, etc. (org-link-search search-str) ;This is extracted from the `org-open-file' function. (setq elem (org-element-at-point)) - ;; (message "[search and get anchor DBG] elem: %S" elem) - (cond + ;; (message "[search and get anchor DBG] elem: %S" elem) + (cond ((equal (org-element-type elem) 'headline) (setq anchor (org-hugo--get-anchor elem info))) (t @@ -4327,6 +4327,8 @@ INFO is a plist holding export options." (defun org-hugo--get-pre-processed-buffer () "Return a pre-processed copy of the current buffer. +This function is called only exporting valid Hugo post subtrees. + Internal links to other subtrees are converted to external links." (let ((pre-processed-buffer-prefix "*Ox-hugo Pre-processed ")) @@ -4342,9 +4344,9 @@ links." (org-use-property-inheritance (org-hugo--selective-property-inheritance)) (info (org-combine-plists (list :parse-tree ast) - (org-export--get-export-attributes 'hugo) + (org-export--get-export-attributes 'hugo :subtreep) (org-export--get-buffer-attributes) - (org-export-get-environment 'hugo))) + (org-export-get-environment 'hugo :subtreep))) (local-variables (buffer-local-variables)) (bound-variables (org-export--list-bound-variables)) vars) @@ -4379,9 +4381,15 @@ links." (let ((type (org-element-property :type link))) (when (member type '("custom-id" "id" "fuzzy")) (let* ((raw-link (org-element-property :raw-link link)) - (destination (if (string= type "fuzzy") - (org-export-resolve-fuzzy-link link info) + (progn + ;; Derived from ox.el -> `org-export-data'. If a broken link is seen + ;; and if `broken-links' option is not nil, ignore the error. + (condition-case err + (org-export-resolve-fuzzy-link link info) + (org-link-broken + (unless (plist-get info :with-broken-links) + (user-error "Unable to resolve link: %S" (nth 1 err)))))) (progn ;; Update `org-id-locations' if it's nil or empty hash table ;; to avoid broken link. diff --git a/test/site/content-org/all-posts.org b/test/site/content-org/all-posts.org index 1a4a4bed..664657b3 100644 --- a/test/site/content-org/all-posts.org +++ b/test/site/content-org/all-posts.org @@ -3746,6 +3746,24 @@ Below links are invalid, they are there just for test purpose. - [[https://jira.example.com/issues/?jql=(project=MYPRJ)AND(assignee=currentUser())][A dummy Jira JQL link]] - [[https://jira.example.com/issues/?jql=(project=MYPRJ)AND(assignee=currentUser())AND(labels in (foo))][A dummy Jira JQL link with spaces]] +** Allow broken links (dummy parent heading) +:PROPERTIES: +:EXPORT_OPTIONS: broken-links:t +:END: +The ~broken-links:t~ export option needs to be set in the parent +heading because of [[https://lists.gnu.org/r/emacs-orgmode/2022-03/msg00149.html][this issue]] in upstream ~ox.el~. Remove this extra +heading nesting once that is fixed. +*** Allow broken links :broken_links: +:PROPERTIES: +:EXPORT_FILE_NAME: allow-broken-links +:EXPORT_OPTIONS: broken-links:t +:END: +#+begin_description +Test that export finishes without any error even when the post has +broken links. +#+end_description +- something [[foo:bar]] something +- something [[roam:git]] something * Equations :equations: ** MathJax :mathjax: *** Inline equations diff --git a/test/site/content/posts/allow-broken-links.md b/test/site/content/posts/allow-broken-links.md new file mode 100644 index 00000000..50dd4d68 --- /dev/null +++ b/test/site/content/posts/allow-broken-links.md @@ -0,0 +1,12 @@ ++++ +title = "Allow broken links (dummy parent heading)" +description = """ + Test that export finishes without any error even when the post has + broken links. + """ +tags = ["links", "broken-links"] +draft = false ++++ + +- something [{{< relref "" >}}]({{< relref "" >}}) something +- something [{{< relref "" >}}]({{< relref "" >}}) something