diff --git a/src/main/java/guideme/compiler/tags/OptionalSectionCompiler.java b/src/main/java/guideme/compiler/tags/OptionalSectionCompiler.java new file mode 100644 index 00000000..967c400b --- /dev/null +++ b/src/main/java/guideme/compiler/tags/OptionalSectionCompiler.java @@ -0,0 +1,32 @@ +package guideme.compiler.tags; + +import guideme.compiler.PageCompiler; +import guideme.document.block.LytBlockContainer; +import guideme.libs.mdast.mdx.model.MdxJsxElementFields; +import java.util.Set; +import net.neoforged.fml.ModList; + +public class OptionalSectionCompiler extends BlockTagCompiler { + @Override + public Set getTagNames() { + return Set.of("OptionalSection"); + } + + @Override + protected void compile(PageCompiler compiler, LytBlockContainer parent, MdxJsxElementFields el) { + var modId = MdxAttrs.getString(compiler, parent, el, "modId", null); + if (modId == null) { + parent.appendError(compiler, "Missing modId attribute.", el); + return; + } + + boolean invert = false; + if (modId.startsWith("!")) { + modId = modId.substring(1); + invert = true; + } + if (ModList.get().isLoaded(modId) ^ invert) { + compiler.compileBlockContext(el.children(), parent); + } + } +} diff --git a/src/main/java/guideme/internal/extensions/DefaultExtensions.java b/src/main/java/guideme/internal/extensions/DefaultExtensions.java index f4fa13e9..08b9e340 100644 --- a/src/main/java/guideme/internal/extensions/DefaultExtensions.java +++ b/src/main/java/guideme/internal/extensions/DefaultExtensions.java @@ -13,6 +13,7 @@ import guideme.compiler.tags.ItemGridCompiler; import guideme.compiler.tags.ItemLinkCompiler; import guideme.compiler.tags.KeyBindTagCompiler; +import guideme.compiler.tags.OptionalSectionCompiler; import guideme.compiler.tags.PlayerNameTagCompiler; import guideme.compiler.tags.RecipeCompiler; import guideme.compiler.tags.RecipeTypeMappingSupplier; @@ -85,7 +86,8 @@ private static List tagCompilers() { new SubPagesCompiler(), new CommandLinkCompiler(), new PlayerNameTagCompiler(), - new KeyBindTagCompiler()); + new KeyBindTagCompiler(), + new OptionalSectionCompiler()); } private static List sceneElementTagCompilers() { diff --git a/src/testmod/resources/assets/testmod/guides/testmod/guide/index.md b/src/testmod/resources/assets/testmod/guides/testmod/guide/index.md index ee564a5c..48f4ea4b 100644 --- a/src/testmod/resources/assets/testmod/guides/testmod/guide/index.md +++ b/src/testmod/resources/assets/testmod/guides/testmod/guide/index.md @@ -82,3 +82,19 @@ You may ~~need~~ a door do + +## Optional content + + + This text must not be visible because no mod with ID `not_installed` is loaded! + + + + This text must not be visible because GuideME is loaded! + + + + This text is visible because Minecraft is loaded! + + +End of optional content