Skip to content

Commit

Permalink
Support new Hugo configuration file names like hugo.toml to auto-en…
Browse files Browse the repository at this point in the history
…able Hugo image lookups for editor and preview (#1766)
  • Loading branch information
ahus1 committed Mar 2, 2025
1 parent d31aeb8 commit 0f80c1d
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 13 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down Expand Up @@ -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 (`/`).

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -666,12 +666,6 @@ private void resolve(String key, List<ResolveResult> 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);
Expand Down Expand Up @@ -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;
}
Expand Down
15 changes: 9 additions & 6 deletions src/main/java/org/asciidoc/intellij/psi/AsciiDocUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -1143,12 +1143,15 @@ public static VirtualFile findHugoStaticFolder(Project project, VirtualFile file
VirtualFile dir = fileBaseDir;
Collection<VirtualFile> 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;
}
Expand Down

0 comments on commit 0f80c1d

Please sign in to comment.