Skip to content

Commit c21423b

Browse files
authored
Merge pull request microsoft#1841 from rbuckton/fix1840
Fix link emit for xref containing #
2 parents dd25b99 + 1c2cdb8 commit c21423b

File tree

3 files changed

+23
-3
lines changed

3 files changed

+23
-3
lines changed

apps/api-documenter/src/documenters/YamlDocumenter.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -666,7 +666,16 @@ export class YamlDocumenter {
666666
onGetFilenameForApiItem: (apiItem: ApiItem) => {
667667
// NOTE: GitHub's markdown renderer does not resolve relative hyperlinks correctly
668668
// unless they start with "./" or "../".
669-
return `xref:${this._getUid(apiItem)}`;
669+
670+
// To ensure the xref is properly escaped, we first encode the entire xref
671+
// to handle escaping of reserved characters. Then we must replace '#' and '?'
672+
// characters so that they are not interpreted as a querystring or hash.
673+
// We must also backslash-escape unbalanced `(` and `)` characters as the
674+
// markdown spec insists that they are only valid when balanced. To reduce
675+
// the overhead we only support balanced parenthesis with a depth of 1.
676+
return encodeURI(`xref:${this._getUid(apiItem)}`)
677+
.replace(/[#?]/g, s => encodeURIComponent(s))
678+
.replace(/(\([^(]*\))|[()]/g, (s, balanced) => balanced || ('\\' + s));
670679
}
671680
});
672681

build-tests/api-documenter-test/etc/yaml/api-documenter-test/docclass1.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ items:
33
- uid: 'api-documenter-test!DocClass1:class'
44
summary: This is an example class.
55
remarks: >-
6-
[Link to overload 1](xref:api-documenter-test!DocClass1#exampleFunction:member(1))
6+
[Link to overload 1](xref:api-documenter-test!DocClass1%23exampleFunction:member(1))
77
88
9-
[Link to overload 2](xref:api-documenter-test!DocClass1#exampleFunction:member(2))
9+
[Link to overload 2](xref:api-documenter-test!DocClass1%23exampleFunction:member(2))
1010
1111
1212
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"changes": [
3+
{
4+
"packageName": "@microsoft/api-documenter",
5+
"comment": "Properly encode xrefs that contain '#' characters.",
6+
"type": "patch"
7+
}
8+
],
9+
"packageName": "@microsoft/api-documenter",
10+
"email": "ron.buckton@microsoft.com"
11+
}

0 commit comments

Comments
 (0)