From b86cb77685325f7d5b13e4d69fee6cb8a3444696 Mon Sep 17 00:00:00 2001 From: Natik Gadzhi Date: Sun, 2 Jul 2023 21:21:11 -0700 Subject: [PATCH] Allow anchor within same article --- .../Link Resolution/PathHierarchy.swift | 10 ++++++-- .../Infrastructure/PathHierarchyTests.swift | 25 +++++++++++++++++++ 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/Sources/SwiftDocC/Infrastructure/Link Resolution/PathHierarchy.swift b/Sources/SwiftDocC/Infrastructure/Link Resolution/PathHierarchy.swift index f6842763f3..87d5ac8fcf 100644 --- a/Sources/SwiftDocC/Infrastructure/Link Resolution/PathHierarchy.swift +++ b/Sources/SwiftDocC/Infrastructure/Link Resolution/PathHierarchy.swift @@ -764,10 +764,16 @@ extension PathHierarchy { let isAbsolute = path.first == "/" || String(components.first ?? "") == NodeURLGenerator.Path.documentationFolderName || String(components.first ?? "") == NodeURLGenerator.Path.tutorialsFolderName - + + // If there is a # character in the last component, split that into two components if let hashIndex = components.last?.firstIndex(of: "#") { let last = components.removeLast() - components.append(last[.. + - + + """.write(to: url.appendingPathComponent("ArticleWithHeading.md"), atomically: true, encoding: .utf8) + } + + let tree = try XCTUnwrap(context.hierarchyBasedLinkResolver?.pathHierarchy) + let articleNode = try tree.findNode(path: "/MixedLanguageFramework/ArticleWithHeading", onlyFindSymbols: false) + + let linkNode = try tree.find(path: "TestTargetHeading", parent: articleNode.identifier, onlyFindSymbols: false) + let anchorLinkNode = try tree.find(path: "#TestTargetHeading", parent: articleNode.identifier, onlyFindSymbols: false) + XCTAssertNotNil(linkNode) + XCTAssertNotNil(anchorLinkNode) + } + func testOverloadedSymbols() throws { try XCTSkipUnless(LinkResolutionMigrationConfiguration.shouldUseHierarchyBasedLinkResolver) let (_, context) = try testBundleAndContext(named: "OverloadedSymbols") @@ -1555,6 +1579,7 @@ class PathHierarchyTests: XCTestCase { assertParsedPathComponents("first/", [("first", nil, nil)]) assertParsedPathComponents("first//second", [("first", nil, nil), ("second", nil, nil)]) assertParsedPathComponents("first/second#third", [("first", nil, nil), ("second", nil, nil), ("third", nil, nil)]) + assertParsedPathComponents("#first", [("first", nil, nil)]) // Check disambiguation assertParsedPathComponents("path-hash", [("path", nil, "hash")])