From 0f80c1d9228ae77993925bdb291bfae0233a5ff1 Mon Sep 17 00:00:00 2001 From: Alexander Schwartz Date: Sun, 2 Mar 2025 22:15:35 +0100 Subject: [PATCH] Support new Hugo configuration file names like `hugo.toml` to auto-enable Hugo image lookups for editor and preview (#1766) --- CHANGELOG.adoc | 1 + .../ROOT/pages/features/advanced/hugo.adoc | 9 ++++++++- .../intellij/psi/AsciiDocFileReference.java | 14 ++++++++------ .../org/asciidoc/intellij/psi/AsciiDocUtil.java | 15 +++++++++------ 4 files changed, 26 insertions(+), 13 deletions(-) diff --git a/CHANGELOG.adoc b/CHANGELOG.adoc index 075029419..cfeb1c754 100644 --- a/CHANGELOG.adoc +++ b/CHANGELOG.adoc @@ -10,6 +10,7 @@ This document provides a high-level view of the changes introduced by release. === 0.44.3 - When pasting a URL from the clipboard to an existing URL in the text, replace the existing URL with the new one (#1763) +- Support new Hugo configuration file names like `hugo.toml` to auto-enable Hugo image lookups for editor and preview (#1766) === 0.44.2 diff --git a/doc/users-guide/modules/ROOT/pages/features/advanced/hugo.adoc b/doc/users-guide/modules/ROOT/pages/features/advanced/hugo.adoc index 9befec225..dd588a89c 100644 --- a/doc/users-guide/modules/ROOT/pages/features/advanced/hugo.adoc +++ b/doc/users-guide/modules/ROOT/pages/features/advanced/hugo.adoc @@ -13,7 +13,13 @@ NOTE: Support for static resources was added in plugin version 0.37.41. A project needs to have the following features for the AsciiDoc plugin for IntelliJ to recognize it as a Hugo project: -* A configuration file (`config.toml`, `config.json` or `config.yaml`) or configuration folder (`config`). +* A configuration file (`hugo.toml`, `hugo.json`, `hugo.yaml`, `config.toml`, `config.json` or `config.yaml`) or configuration folder (`config`). ++ +[NOTE] +==== +The `hugo.*` file names are supported from plugin version 0.44.3 onwards. +==== + * A folder `content` next to the configuration file or folder. * If a folder for static resources exists, it has to have the name `static` and needs to be next to the configuration file or folder. @@ -52,6 +58,7 @@ https://gohugo.io/content-management/page-resources/[Page resources] exist in th └─📄 post-2-without-page-resources.adoc ---- + To reference an image in the post's folder, use a relative target name for the image. To reference an image in the static resources, use a target name for the image starting with a slash (`/`). diff --git a/src/main/java/org/asciidoc/intellij/psi/AsciiDocFileReference.java b/src/main/java/org/asciidoc/intellij/psi/AsciiDocFileReference.java index d265825a4..6f627d0e3 100644 --- a/src/main/java/org/asciidoc/intellij/psi/AsciiDocFileReference.java +++ b/src/main/java/org/asciidoc/intellij/psi/AsciiDocFileReference.java @@ -666,12 +666,6 @@ private void resolve(String key, List results, int depth, Collect resolveAttributes(k, results, depth, searchedKeys); if (results.size() == c && ("image".equals(macroName) || "video".equals(macroName) || "audio".equals(macroName)) && k.equals(key) && depth == 0) { resolveAttributes("{imagesdir}/" + k, results, depth, searchedKeys); - if (results.size() == c && k.startsWith("/")) { - VirtualFile hugoStaticFile = AsciiDocUtil.findHugoStaticFolder(myElement); - if (hugoStaticFile != null) { - resolveAttributes(hugoStaticFile.getCanonicalPath() + k, results, depth, searchedKeys); - } - } } } resolveAntoraPageAlias(key, results, depth); @@ -1337,6 +1331,14 @@ public TextRange getRangeInElement() { if (startDir == null) { startDir = myFile.getOriginalFile().getContainingDirectory(); } + if (("image".equals(macroName) || "video".equals(macroName) || "audio".equals(macroName)) && fileName.startsWith("/")) { + VirtualFile hugoStaticFile = AsciiDocUtil.findHugoStaticFolder(myElement); + if (hugoStaticFile != null && hugoStaticFile.getCanonicalPath() != null) { + startDir = PsiManager.getInstance(root.getProject()).findDirectory(hugoStaticFile); + fileName = fileName.substring(1); + } + } + if (startDir == null) { return null; } diff --git a/src/main/java/org/asciidoc/intellij/psi/AsciiDocUtil.java b/src/main/java/org/asciidoc/intellij/psi/AsciiDocUtil.java index cb871480d..6146078f3 100644 --- a/src/main/java/org/asciidoc/intellij/psi/AsciiDocUtil.java +++ b/src/main/java/org/asciidoc/intellij/psi/AsciiDocUtil.java @@ -1143,12 +1143,15 @@ public static VirtualFile findHugoStaticFolder(Project project, VirtualFile file VirtualFile dir = fileBaseDir; Collection roots = getRoots(project); while (dir != null) { - if (dir.getParent() != null && dir.getParent().getName().equals("content")) { - VirtualFile staticFolder = dir.getParent().getParent().findChild("static"); - boolean configExists = dir.getParent().getParent().findChild("config") != null || - dir.getParent().getParent().findChild("config.toml") != null || - dir.getParent().getParent().findChild("config.yaml") != null || - dir.getParent().getParent().findChild("config.json") != null; + if (dir.getParent() != null && dir.getName().equals("content")) { + VirtualFile staticFolder = dir.getParent().findChild("static"); + boolean configExists = dir.getParent().findChild("config") != null || + dir.getParent().findChild("config.toml") != null || + dir.getParent().findChild("config.yaml") != null || + dir.getParent().findChild("config.json") != null || + dir.getParent().findChild("hugo.toml") != null || + dir.getParent().findChild("hugo.yaml") != null || + dir.getParent().findChild("hugo.json") != null; if (staticFolder != null && configExists) { return staticFolder; }