-
Notifications
You must be signed in to change notification settings - Fork 1
/
typedoc-plugin-deno.ts
156 lines (129 loc) · 4.25 KB
/
typedoc-plugin-deno.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import { trim } from 'lodash';
import { Component, ConverterComponent } from 'typedoc/dist/lib/converter/components';
import { Context } from 'typedoc/dist/lib/converter/context';
import { Converter } from 'typedoc/dist/lib/converter/converter';
import { Comment, CommentTag, SignatureReflection } from 'typedoc/dist/lib/models';
import { Reflection, ReflectionKind } from 'typedoc/dist/lib/models/reflections/abstract';
@Component({ name: 'deno' })
export class DenoPlugin extends ConverterComponent {
initialize() {
this.listenTo(this.owner, {
[Converter.EVENT_RESOLVE_END]: this.onEndResolve,
});
}
/** Triggered when the converter has finished resolving a project. */
private onEndResolve(context: Context) {
for (let signature of Object.values(context.project.reflections)) {
const comment = signature.comment as Comment & I18nComment;
if (!comment) {
continue;
}
processi18nLineByLine(comment);
if (!isSignature(signature)) {
continue;
}
if (comment && comment.hasTag('returns_i18n')) {
comment.returns_i18n = comment.getTag('returns_i18n')!.text;
comment.removeTags('returns_i18n');
}
if (!signature.parameters) {
continue;
}
for (let parameter of signature.parameters) {
const tag = getI18nParamTag(comment, parameter.name);
if (!tag) {
continue;
}
(parameter.comment as Comment & I18nComment).text_i18n = tag.text.replace(parameter.name, '').trim();
(parameter.comment as Comment & I18nComment).shortText_i18n = '';
}
comment.removeTags('param_i18n');
if (!signature.typeParameters) {
continue;
}
for (let parameter of signature.typeParameters) {
const tag = getI18nTypeParamTag(comment, parameter.name);
if (!tag) {
continue;
}
(parameter.comment as Comment & I18nComment).text_i18n = tag.text.replace(parameter.name, '').trim();
(parameter.comment as Comment & I18nComment).shortText_i18n = '';
}
comment.removeTags('typeparam_i18n');
comment.removeTags('template_i18n');
}
}
}
function processi18nLineByLine(comment: Comment & I18nComment): void {
if (!comment) {
return;
}
comment.text = trim(comment.text, '\n');
if (!comment.tags) {
return;
}
const tags = comment.tags;
comment.text_line_i18n = [];
comment.text_line_en = [];
comment.shortText_i18n = '';
let i = 0;
let c = tags.length;
while (i < c) {
const text = trim(tags[i].text, '\n');
if (tags[i].tagName === 'i18n' && text !== '') {
const [one, two, ...others] = text.split('\n\n');
comment.text_line_i18n.push(one);
comment.text_line_en.push(two || '');
for (let line of others) {
comment.text_line_en.push(line);
comment.text_line_i18n.push('');
}
}
if (tags[i].tagName === 'i18n') {
// Remove tag
tags.splice(i, 1);
c--;
} else {
// Process next tag
i++;
}
}
if (comment.text_line_i18n.length === 0) {
return;
}
if (comment.text === '') {
// shortText has been translated
comment.shortText_i18n = comment.text_line_i18n.shift();
comment.text = comment.text_line_en.join('\n\n');
} else {
comment.text_line_en.unshift(comment.text);
comment.text = comment.text_line_en.join('\n\n');
}
comment.text_line_i18n.push('');
}
function isSignature(reflection: Reflection): reflection is SignatureReflection {
return reflection.kindOf(ReflectionKind.SomeSignature);
}
function getI18nParamTag(comment: Comment, paramName?: string): CommentTag | undefined {
if (!comment.tags) {
return undefined;
}
return comment.tags.find((tag) => tag.tagName === 'param_i18n' && tag.text.startsWith(paramName));
}
function getI18nTypeParamTag(comment: Comment, paramName?: string): CommentTag | undefined {
if (!comment.tags) {
return undefined;
}
return comment.tags.find(
(tag) => (tag.tagName === 'typeparam_i18n' || tag.tagName === 'template_i18n') && tag.text.startsWith(paramName),
);
}
interface I18nTag {
text_i18n: string;
}
interface I18nComment extends I18nTag {
shortText_i18n: string;
text_line_i18n: string[];
text_line_en: string[];
returns_i18n: string;
}