Skip to content

Commit

Permalink
Fix links to pages with underscores in their namespace.
Browse files Browse the repository at this point in the history
  • Loading branch information
shartte committed Feb 10, 2025
1 parent 7360e56 commit da6059e
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 25 deletions.
1 change: 1 addition & 0 deletions docs/docs/02-changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import Video from '@site/src/components/Video';
- Improved query parsing for full-text search. Search will now always apply "incremental" search for the last entered word,
assuming the user might not have entered it fully yet. This means searching for "io po" will search for both "io po"
and "io po*", although it will score an exact hit for "po" higher than a hit for "port" (for example).
- Fix parsing of links to pages where the mod-id contained underscores (i.e. `modern_industrialization:some_page.md`).

## 2.6.0 (Minecraft 1.21.1)

Expand Down
46 changes: 21 additions & 25 deletions src/main/java/guideme/compiler/LinkParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,42 +18,38 @@ public static void parseLink(PageCompiler compiler, String href, Visitor visitor
try {
uri = URI.create(href);
} catch (Exception ignored) {
visitor.handleError("Invalid URL");
uri = null;
}

// External link
if (uri != null && uri.isAbsolute()
&& (uri.getScheme().equals("http") || uri.getScheme().equalsIgnoreCase("https"))) {
visitor.handleExternal(uri);
return;
}

ResourceLocation pageId;
String fragment = null;
var fragmentSep = href.indexOf('#');
if (fragmentSep != -1) {
fragment = href.substring(fragmentSep + 1);
href = href.substring(0, fragmentSep);
}

// External link
if (uri.isAbsolute()) {
if (uri.getScheme().equals("http") || uri.getScheme().equalsIgnoreCase("https")) {
visitor.handleExternal(uri);
return;
} else {
// Fully namespaced, absolute page id
try {
pageId = ResourceLocation.parse(uri.getScheme() + ":" + uri.getSchemeSpecificPart());
} catch (ResourceLocationException ignored) {
visitor.handleError("Invalid resource location");
return;
}
}
} else {
// Determine the page id, account for relative paths
try {
pageId = IdUtils.resolveLink(uri.getPath(), compiler.getPageId());
} catch (ResourceLocationException ignored) {
visitor.handleError("Invalid link");
return;
}
// Determine the page id, account for relative paths
ResourceLocation pageId;
try {
pageId = IdUtils.resolveLink(href, compiler.getPageId());
} catch (ResourceLocationException ignored) {
visitor.handleError("Invalid link");
return;
}

if (!compiler.getPageCollection().pageExists(pageId)) {
visitor.handleError("Page does not exist");
return;
}

visitor.handlePage(new PageAnchor(pageId, uri.getFragment()));
visitor.handlePage(new PageAnchor(pageId, fragment));
}

public interface Visitor {
Expand Down
9 changes: 9 additions & 0 deletions src/test/java/guideme/compiler/LinkParserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,15 @@ void testLinkToOtherNamespace() {
assertThat(anchors).containsExactly(PageAnchor.page(ResourceLocation.parse("ns2:abc/def.md")));
}

// This failed previously due to URI rules not allowing a scheme with _
@Test
void testLinkToOtherNamespaceWithUnderscore() {
LinkParser.parseLink(compiler, "ns_2:abc/def.md", visitor);
assertThat(external).containsExactly();
assertThat(errors).containsExactly();
assertThat(anchors).containsExactly(PageAnchor.page(ResourceLocation.parse("ns_2:abc/def.md")));
}

@Test
void testLinkToOtherNamespaceWithFragment() {
LinkParser.parseLink(compiler, "ns2:abc/def.md#fragment", visitor);
Expand Down

0 comments on commit da6059e

Please sign in to comment.