Skip to content

Commit 0cfa0c9

Browse files
committed
first commit
0 parents  commit 0cfa0c9

File tree

8 files changed

+1040
-0
lines changed

8 files changed

+1040
-0
lines changed

.github/workflows/ci.yml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
name: ci
2+
3+
on: [push, pull_request]
4+
5+
jobs:
6+
build:
7+
runs-on: ubuntu-latest
8+
steps:
9+
- uses: actions/checkout@v2
10+
- uses: actions/setup-node@v1
11+
with:
12+
node-version: 12
13+
- run: yarn --frozen-lockfile
14+
- run: yarn build

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2020 justjavac
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# typedoc-plugin-deno
2+
3+
[![Build Status](https://github.com/denodev/typedoc-plugin-deno/workflows/ci/badge.svg?branch=master)](https://github.com/denodev/typedoc-plugin-deno/actions)
4+
[![license](https://img.shields.io/github/license/denodev/typedoc-plugin-deno)](https://github.com/denodev/typedoc-plugin-deno/blob/master/LICENSE)
5+
[![NPM version](https://img.shields.io/npm/v/typedoc-plugin-deno.svg)](https://www.npmjs.com/package/typedoc-plugin-deno)
6+
7+
This plugin is used by [Deno Api Simplified Chinese Site](https://github.com/denodev/typedoc).
8+
9+
This plugin allows specific typedoc `@*_i18n` to be processed as multi-language.
10+
11+
Example:
12+
13+
```ts
14+
/** This comment _supports_ [Markdown](https://marked.js.org/).
15+
*
16+
* @_i18n 这是一个中文注释,_支持_ [Markdown](https://marked.js.org/)。
17+
*
18+
* @param text Comment for parameter ´text´.
19+
* @param_i18n text 参数 ´text´ 的中文注释。
20+
*/
21+
function doSomething(target: any, text: string): number;
22+
```
23+
24+
By default, this plugin processes the following tags:
25+
26+
- [x] `@_i18n`
27+
- [ ] `@param_i18n` need help
28+
- [ ] `@typeParam_i18n` need help
29+
- [ ] `@return(s)_i18n` need help
30+
- [ ] `@event_i18n` need help
31+
32+
### Installing
33+
34+
Typedoc has the ability to discover and auto-load typedoc plugins found in node_modules.
35+
36+
```bash
37+
npm install --save typedoc-plugin-deno
38+
```
39+
40+
### License
41+
42+
typedoc-plugin-deno is released under the MIT License. See the bundled [LICENSE](./LICENSE) file for details.

index.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
var plugin = require('./typedoc-plugin-deno');
2+
module.exports = function (PluginHost) {
3+
var app = PluginHost.owner;
4+
app.converter.addComponent('deno', plugin.DenoPlugin);
5+
};

package.json

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
{
2+
"name": "typedoc-plugin-deno",
3+
"version": "1.0.0",
4+
"main": "index.js",
5+
"scripts": {
6+
"build": "tsc",
7+
"prepublishOnly": "npm run build"
8+
},
9+
"husky": {
10+
"hooks": {
11+
"pre-commit": "pretty-quick --staged"
12+
}
13+
},
14+
"keywords": [
15+
"typedocplugin",
16+
"typedoc",
17+
"deno"
18+
],
19+
"homepage": "https://github.com/denodev/typedoc-plugin-deno",
20+
"repository": {
21+
"type": "git",
22+
"url": "git://github.com/denodev/typedoc-plugin-deno.git"
23+
},
24+
"bugs": {
25+
"url": "https://github.com/denodev/typedoc-plugin-deno/issues"
26+
},
27+
"license": "MIT",
28+
"dependencies": {
29+
"lodash": "^4.17.15",
30+
"semver": "^7.3.2"
31+
},
32+
"peerDependencies": {
33+
"typedoc": "^0.17.4"
34+
},
35+
"devDependencies": {
36+
"@types/lodash": "^4.14.149",
37+
"@types/node": "^13.11.1",
38+
"@types/semver": "^7.1.0",
39+
"husky": "^4.2.5",
40+
"prettier": "^2.0.4",
41+
"pretty-quick": "^2.0.1",
42+
"typedoc": "^0.17.4",
43+
"typescript": "^3.8.3"
44+
}
45+
}

tsconfig.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"compilerOptions": {
3+
"moduleResolution": "node",
4+
"allowJs": true,
5+
"module": "umd",
6+
"target": "es2015",
7+
"outDir": "./",
8+
"experimentalDecorators": true
9+
},
10+
"files": ["typedoc-plugin-deno.ts"],
11+
"exclude": ["node_modules"]
12+
}

typedoc-plugin-deno.ts

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
import * as ts from 'typescript';
2+
import { trim } from 'lodash';
3+
import { Component, ConverterComponent } from 'typedoc/dist/lib/converter/components';
4+
import { Context } from 'typedoc/dist/lib/converter/context';
5+
import { Converter } from 'typedoc/dist/lib/converter/converter';
6+
import { Comment, CommentTag } from 'typedoc/dist/lib/models';
7+
import { Reflection } from 'typedoc/dist/lib/models/reflections/abstract';
8+
import {
9+
ParameterReflection,
10+
TypeParameterReflection,
11+
SignatureReflection,
12+
DeclarationReflection,
13+
} from 'typedoc/dist/lib/serialization/schema';
14+
15+
@Component({ name: 'deno' })
16+
export class DenoPlugin extends ConverterComponent {
17+
initialize() {
18+
this.listenTo(this.owner, {
19+
[Converter.EVENT_CREATE_DECLARATION]: this.onDeclaration,
20+
[Converter.EVENT_CREATE_SIGNATURE]: this.onSignature,
21+
[Converter.EVENT_CREATE_PARAMETER]: this.onParameter,
22+
[Converter.EVENT_CREATE_TYPE_PARAMETER]: this.TypeParameter,
23+
});
24+
}
25+
26+
/**
27+
* Triggered when the converter has created a parameter reflection.
28+
* @todo Maybe not usefull
29+
*
30+
* @param context The context object describing the current state the converter is in.
31+
* @param reflection The reflection that is currently processed.
32+
* @param node The node that is currently processed if available.
33+
*/
34+
private onParameter(context: Context, reflection: ParameterReflection, node?) {
35+
if (!reflection.comment) return;
36+
const comment = reflection.comment;
37+
}
38+
39+
/**
40+
* Triggered when the converter has created a type parameter reflection.
41+
* @todo Maybe not usefull
42+
*
43+
* @param context The context object describing the current state the converter is in.
44+
* @param reflection The reflection that is currently processed.
45+
* @param node The node that is currently processed if available.
46+
*/
47+
private TypeParameter(context: Context, reflection: Reflection, node?) {
48+
if (!reflection.comment) return;
49+
const comment = reflection.comment;
50+
}
51+
52+
/**
53+
* Triggered when the converter has created a declaration reflection.
54+
*
55+
* @param context The context object describing the current state the converter is in.
56+
* @param reflection The reflection that is currently processed.
57+
* @param node The node that is currently processed if available.
58+
*/
59+
private onSignature(context: Context, reflection: Reflection, node?) {
60+
if (!reflection.comment) return;
61+
this.onDeclaration(context, reflection, node);
62+
63+
if (!reflection.comment.tags) return;
64+
const tags = reflection.comment.tags as [CommentTag & I18nTag];
65+
let i = 0;
66+
let c = tags.length;
67+
while (i < c - 1) {
68+
if (tags[i].tagName + '_i18n' === tags[i + 1].tagName) {
69+
tags[i].text_i18n = tags[i + 1].text.replace(tags[i].paramName, '').trim();
70+
tags.splice(i + 1, 1);
71+
c--;
72+
}
73+
i++;
74+
}
75+
}
76+
77+
/**
78+
* Triggered when the converter has created a declaration reflection.
79+
*
80+
* @param context The context object describing the current state the converter is in.
81+
* @param reflection The reflection that is currently processed.
82+
* @param node The node that is currently processed if available.
83+
*/
84+
private onDeclaration(context: Context, reflection: Reflection, node?) {
85+
if (!reflection.comment) return;
86+
87+
const comment = reflection.comment as Comment & I18nComment;
88+
comment.text = trim(comment.text, '\n');
89+
90+
if (!comment.tags) return;
91+
92+
const i18nTag = comment.tags.find((x) => (x.tagName = '_i18n'));
93+
94+
if (i18nTag === undefined) return;
95+
96+
const i18n = trim(i18nTag.text, '\n');
97+
const firstLF = i18n.indexOf('\n');
98+
99+
if (firstLF === -1) {
100+
comment.shortText_i18n = i18n;
101+
comment.text_i18n = '';
102+
} else {
103+
comment.shortText_i18n = i18n.substr(0, firstLF);
104+
comment.text_i18n = i18n.substr(firstLF + 1);
105+
}
106+
107+
removeTags(comment, '_i18n');
108+
if (isEmptyComment(comment)) {
109+
delete reflection.comment;
110+
}
111+
}
112+
}
113+
114+
function isEmptyComment(comment: Comment) {
115+
return !comment || (!comment.text && !comment.shortText && (!comment.tags || comment.tags.length === 0));
116+
}
117+
118+
interface I18nTag {
119+
text_i18n: string;
120+
}
121+
122+
interface I18nComment extends I18nTag {
123+
shortText_i18n: string;
124+
}
125+
126+
/**
127+
* Remove all tags with the given name from the given comment instance.
128+
*
129+
* @param comment The comment that should be modified.
130+
* @param tagName The name of the that that should be removed.
131+
*/
132+
function removeTags(comment: Comment | undefined, tagName: string) {
133+
if (!comment || !comment.tags) {
134+
return;
135+
}
136+
137+
let i = 0,
138+
c = comment.tags.length;
139+
while (i < c) {
140+
if (comment.tags[i].tagName === tagName) {
141+
comment.tags.splice(i, 1);
142+
c--;
143+
} else {
144+
i++;
145+
}
146+
}
147+
}

0 commit comments

Comments
 (0)