Skip to content

Commit

Permalink
Try resolving non-aliased targets to local SimpleName if exists
Browse files Browse the repository at this point in the history
  • Loading branch information
Trinovantes committed Jun 30, 2024
1 parent 122d3b8 commit 56d7552
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 5 deletions.
12 changes: 7 additions & 5 deletions src/Parser/Resolver/SimpleNameResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,17 +128,19 @@ export class SimpleNameResolver {
return null
}

const target = this._simpleNameToTarget.get(simpleName)
if (!target) {
const docTarget = this._simpleNameToTarget.get(simpleName)
if (!docTarget) {
return null
}

if (!target.isAlias) {
return target.target
const candidateTargetName = normalizeSimpleName(docTarget.target)
const isTargetTangible = (!docTarget.isAlias && docTarget.target.startsWith('#')) || (!docTarget.isAlias && !this._simpleNameToTarget.has(candidateTargetName))
if (isTargetTangible) {
return docTarget.target
}

seenSimpleNames.add(simpleName)
simpleName = normalizeSimpleName(target.target)
simpleName = candidateTargetName
}
}

Expand Down
46 changes: 46 additions & 0 deletions tests/unit/RstNode/ExplicitMarkup/Hyperlink.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -887,4 +887,50 @@ describe('HyperlinkRef', () => {
[HyperlinkTarget id:4 children:0 label:"Foo<Bar>" target:"${goodUrl}" resolvedUrl:"${goodUrl}"]: #
`)
})

describe('when HyperlinkRef seems to be alias but target resolves to an existing local SimpleName, it prioritizes the local SimpleName', () => {
const input = `
See \`if/else/elif\`_
if/else/elif
^^^^^^^^^^^^
`

testParser(input, [
{
type: RstNodeType.Paragraph,
children: [
{
type: RstNodeType.Text,
text: 'See ',
},
{
type: RstNodeType.HyperlinkRef,
text: 'if/else/elif',
},
],
},
{
type: RstNodeType.Section,
text: 'if/else/elif',
data: {
level: 1,
},
},
])

testGenerator(input, `
<p>
See <a href="#if-else-elif">if/else/elif</a>
</p>
<h1 id="if-else-elif">
if/else/elif
</h1>
`, `
See [if/else/elif](#if-else-elif)
# if/else/elif {#if-else-elif}
`)
})
})

0 comments on commit 56d7552

Please sign in to comment.