Skip to content

Commit aa30fc8

Browse files
authored
Merge pull request #58 from webdoc-js/feature/inline-code-tags
[Feature]: Resolve {@code } tags
2 parents 62eac48 + 5c53fe1 commit aa30fc8

File tree

4 files changed

+56
-20
lines changed

4 files changed

+56
-20
lines changed

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

Lines changed: 9 additions & 6 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
)
@@ -51,26 +51,29 @@ exports.signaturePlugin = {
5151
if (doc.returns) {
5252
signature += ` → {${
5353
(doc.returns || [])
54-
.map((returns) =>
55-
(returns.dataType ? linker.linkTo(returns.dataType) : ""))
54+
.map((returns) => (returns.dataType ?
55+
linker.linkTo(returns.dataType, undefined, {htmlSafe: false}) :
56+
""))
5657
.join(", ")
5758
}} `;
5859
}
5960
break;
6061
case "PropertyDoc":
6162
if (doc.dataType) {
62-
signature += ": " + linker.linkTo(doc.dataType);
63+
signature += ": " + linker.linkTo(doc.dataType, undefined, {htmlSafe: false});
6364
}
6465
break;
6566
case "ClassDoc":
6667
if (doc.extends) {
6768
signature += ` extends ${
68-
(doc.extends || []).map((superClass) => linker.linkTo(superClass)).join(", ")
69+
(doc.extends || [])
70+
.map((superClass) => linker.linkTo(superClass, undefined, {htmlSafe: false})).join(", ")
6971
}`;
7072
}
7173
if (doc.implements) {
7274
signature += `\nimplements ${
73-
(doc.implements || []).map((ifc) => linker.linkTo(ifc)).join(", ")
75+
(doc.implements || [])
76+
.map((ifc) => linker.linkTo(ifc, undefined, {htmlSafe: false})).join(", ")
7477
}`;
7578
}
7679
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));

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

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,46 +11,52 @@ describe("@webdoc/template-library.TemplateTagsResolver", function() {
1111
mockTemplateRenderer.installPlugin("linker", LinkerPlugin);
1212
mockTagsResolver.attachTo({renderer: mockTemplateRenderer});
1313

14+
it("{@code Sample}", function() {
15+
expect(mockTagsResolver.runCodeTag("--{@code Sample}--"))
16+
.to.equal("--<code>Sample</code>--");
17+
});
18+
1419
it("{@link <DOC_PATH>}", function() {
15-
expect(mockTagsResolver.runLink("--{@link <DOC_PATH>}--"))
20+
expect(mockTagsResolver.runLinkTag("--{@link <DOC_PATH>}--"))
1621
.to.equal("--<DOC_PATH>--");
1722
});
1823

1924
it("{@link <DOC_PATH> <NAME>}", function() {
20-
expect(mockTagsResolver.runLink("--{@link <DOC_PATH> <NAME>}--"))
25+
expect(mockTagsResolver.runLinkTag("--{@link <DOC_PATH> <NAME>}--"))
2126
.to.equal("--<NAME>--");
2227
});
2328

2429
it("{@link <DOC_PATH>|<NAME>}", function() {
25-
expect(mockTagsResolver.runLink("--{@link <DOC_PATH>|<NAME>}--"))
30+
expect(mockTagsResolver.runLinkTag("--{@link <DOC_PATH>|<NAME>}--"))
2631
.to.equal("--<NAME>--");
2732
});
2833

2934
it("{@link https://github.com/webdoc-js/webdoc}", function() {
30-
expect(mockTagsResolver.runLink("--{@link https://github.com/webdoc-js/webdoc}--"))
35+
expect(mockTagsResolver.runLinkTag("--{@link https://github.com/webdoc-js/webdoc}--"))
3136
.to.equal("--<a href=\"https://github.com/webdoc-js/webdoc\">" +
3237
"https://github.com/webdoc-js/webdoc</a>--");
3338
});
3439

3540
it("{@link https://github.com/webdoc-js/webdoc|LINK_NAME}", function() {
36-
expect(mockTagsResolver.runLink("--{@link https://github.com/webdoc-js/webdoc|LINK_NAME}--"))
41+
expect(mockTagsResolver.runLinkTag("--{@link https://github.com/webdoc-js/webdoc|LINK_NAME}--"))
3742
.to.equal("--<a href=\"https://github.com/webdoc-js/webdoc\">" +
3843
"LINK_NAME</a>--");
3944
});
4045

4146
it("{@link https://github.com/webdoc-js/webdoc LINK NAME}", function() {
42-
expect(mockTagsResolver.runLink("--{@link https://github.com/webdoc-js/webdoc LINK NAME}--"))
47+
expect(mockTagsResolver.runLinkTag("--{@link https://github.com/webdoc-js/webdoc LINK NAME}--"))
4348
.to.equal("--<a href=\"https://github.com/webdoc-js/webdoc\">" +
4449
"LINK NAME</a>--");
4550
});
4651

4752
it("[LINK_TEXT]{@link DOC_PATH}", function() {
48-
expect(mockTagsResolver.runLink("--[LINK_TEXT]{@link <DOC_PATH>}--"))
53+
expect(mockTagsResolver.runLinkTag("--[LINK_TEXT]{@link <DOC_PATH>}--"))
4954
.to.equal("--LINK_TEXT--");
5055
});
5156

5257
it("[LINK_TEXT]{@link https://github.com/webdoc-js/webdoc}", function() {
53-
expect(mockTagsResolver.runLink("--[LINK_TEXT]{@link https://github.com/webdoc-js/webdoc}--"))
58+
expect(
59+
mockTagsResolver.runLinkTag("--[LINK_TEXT]{@link https://github.com/webdoc-js/webdoc}--"))
5460
.to.equal("--<a href=\"https://github.com/webdoc-js/webdoc\">" +
5561
"LINK_TEXT</a>--");
5662
});

0 commit comments

Comments
 (0)