Skip to content

Commit b29d7ac

Browse files
committed
Resolve {@code } in TemplateTagsResolver pipeline, non-htmlSafe links inside <pre> signatures
1 parent 62eac48 commit b29d7ac

File tree

3 files changed

+39
-11
lines changed

3 files changed

+39
-11
lines changed

packages/webdoc-default-template/helper/renderer-plugins/signature.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ exports.signaturePlugin = {
4141
.map((param) =>
4242
param.identifier +
4343
(param.dataType ?
44-
": " + linker.linkTo(param.dataType) :
44+
": " + linker.linkTo(param.dataType, undefined, {htmlSafe: false}) :
4545
""
4646
),
4747
)
@@ -52,25 +52,26 @@ exports.signaturePlugin = {
5252
signature += ` → {${
5353
(doc.returns || [])
5454
.map((returns) =>
55-
(returns.dataType ? linker.linkTo(returns.dataType) : ""))
55+
(returns.dataType ? linker.linkTo(returns.dataType, undefined, {htmlSafe: false}) : ""))
5656
.join(", ")
5757
}} `;
5858
}
5959
break;
6060
case "PropertyDoc":
6161
if (doc.dataType) {
62-
signature += ": " + linker.linkTo(doc.dataType);
62+
signature += ": " + linker.linkTo(doc.dataType, undefined, {htmlSafe: false});
6363
}
6464
break;
6565
case "ClassDoc":
6666
if (doc.extends) {
6767
signature += ` extends ${
68-
(doc.extends || []).map((superClass) => linker.linkTo(superClass)).join(", ")
68+
(doc.extends || [])
69+
.map((superClass) => linker.linkTo(superClass, undefined, {htmlSafe: false})).join(", ")
6970
}`;
7071
}
7172
if (doc.implements) {
7273
signature += `\nimplements ${
73-
(doc.implements || []).map((ifc) => linker.linkTo(ifc)).join(", ")
74+
(doc.implements || []).map((ifc) => linker.linkTo(ifc, undefined, {htmlSafe: false})).join(", ")
7475
}`;
7576
}
7677
break;

packages/webdoc-template-library/src/pipeline-elements/TemplateTagsResolver.js

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import type {TemplatePipeline, TemplatePipelineElement} from "../TemplatePipeline";
44
import type {TemplateRenderer} from "../TemplateRenderer";
55

6+
const CODE_PATTERN = /{@code ([^}]*)}/g;
7+
68
const LINK_PATTERN = /{@link ([^|\s}]*)([\s|])?([^}]*)}/g;
79

810
/**
@@ -28,12 +30,33 @@ export class TemplateTagsResolver implements TemplatePipelineElement<{}> {
2830
}
2931

3032
run(input: string, pipelineData: any): string {
31-
input = this.runLink(input);
33+
input = this.runCodeTag(input);
34+
input = this.runLinkTag(input);
35+
36+
return input;
37+
}
38+
39+
runCodeTag(input: string): string {
40+
const codePattern = CODE_PATTERN;
41+
42+
let codeMatch = codePattern.exec(input);
43+
44+
while (codeMatch) {
45+
const code = codeMatch[1];
46+
const startIndex = codeMatch.index;
47+
const endIndex = codeMatch.index + codeMatch[0].length;
48+
49+
input = input.slice(0, startIndex) +
50+
`<code>${code}</code>` +
51+
input.slice(endIndex);
52+
53+
codeMatch = codePattern.exec(input);
54+
}
3255

3356
return input;
3457
}
3558

36-
runLink(input: string): string {
59+
runLinkTag(input: string): string {
3760
const linkPattern = LINK_PATTERN;
3861
let linkMatch = linkPattern.exec(input);
3962

packages/webdoc-template-library/src/template-plugins/LinkerPlugin.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ export type LinkOptions = {
88
fragmentId?: string,
99
linkMap?: Map<string, string>,
1010
monospace?: boolean,
11-
shortenName?: boolean
11+
shortenName?: boolean,
12+
htmlSafe?: boolean
1213
};
1314

1415
export type LinkerDocumentRecord = {
@@ -183,6 +184,7 @@ function LinkerPluginShell() {
183184
* monospace font.
184185
* @param {boolean} options.shortenName - Indicates whether to extract the short name from the
185186
* longname and display the short name in the link text. Ignored if `linkText` is specified.
187+
* @param {boolean}[options.htmlSafe=true]
186188
* @return {string} the HTML link, or the link text if the link is not available.
187189
*/
188190
linkTo(docPath: any, linkText: string = docPath, options: LinkOptions = {}) {
@@ -193,9 +195,11 @@ function LinkerPluginShell() {
193195
return `<a href=${encodeURI(this.queryCache.get(docPath) || "")}>${linkText}</a>`;
194196
}
195197
if (isDataType(docPath)) {
196-
let link = docPath.template
197-
.replace(/</g, "&lt;")
198-
.replace(/>/g, "&gt;");
198+
let link = docPath.template;
199+
200+
if (options.htmlSafe !== false) {
201+
link = link.replace(/</g, "&lt;").replace(/>/g, "&gt;");
202+
}
199203

200204
for (let i = 1; i < docPath.length; i++) {
201205
link = link.replace(`%${i}`, this.linkTo(docPath[i], docPath[i], options));

0 commit comments

Comments
 (0)