forked from asgerjensen/typedoc-plugin-external-module-map
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathplugin.js
159 lines (158 loc) · 7.58 KB
/
plugin.js
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
157
158
159
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
(function (factory) {
if (typeof module === "object" && typeof module.exports === "object") {
var v = factory(require, exports);
if (v !== undefined) module.exports = v;
}
else if (typeof define === "function" && define.amd) {
define(["require", "exports", "path", "fs", "marked", "typedoc/dist/lib/converter/components", "typedoc/dist/lib/converter/converter", "typedoc/dist/lib/converter/plugins/CommentPlugin", "typedoc/dist/lib/models/comments", "typedoc/dist/lib/utils/options"], factory);
}
})(function (require, exports) {
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const path = require("path");
const fs = require("fs");
const marked = require("marked");
const components_1 = require("typedoc/dist/lib/converter/components");
const converter_1 = require("typedoc/dist/lib/converter/converter");
const CommentPlugin_1 = require("typedoc/dist/lib/converter/plugins/CommentPlugin");
const comments_1 = require("typedoc/dist/lib/models/comments");
const options_1 = require("typedoc/dist/lib/utils/options");
// tslint:disable-next-line ban-types
// ReflectionKind["Package"] = 1337;
// declare module "typedoc/dist/lib/models/reflections/abstract" {
// export enum ReflectionKind {
// "Package" = 1337
// }
// }
marked.setOptions({
renderer: new marked.Renderer(),
highlight: function (code) {
return require('highlight.js').highlightAuto(code).value;
},
pedantic: false,
gfm: true,
tables: true,
breaks: false,
sanitize: false,
smartLists: true,
smartypants: false,
});
/**
* This plugin allows you to provide a mapping regexp between your source folder structure, and the module that should be
* reported in typedoc. It will match the first capture group of your regex and use that as the module name.
*
* Based on https://github.com/christopherthielen/typedoc-plugin-external-module-name
*
*
*/
let ExternalModuleMapPlugin = class ExternalModuleMapPlugin extends components_1.ConverterComponent {
initialize() {
this.modules = new Set();
this.options = this.application.options;
this.listenTo(this.owner, {
[converter_1.Converter.EVENT_BEGIN]: this.onBegin,
[converter_1.Converter.EVENT_CREATE_DECLARATION]: this.onDeclarationBegin,
[converter_1.Converter.EVENT_RESOLVE_BEGIN]: this.onBeginResolve,
});
}
/**
* Triggered when the converter begins converting a project.
*
* @param context The context object describing the current state the converter is in.
*/
onBegin(context) {
this.moduleRenames = [];
this.options.read({}, options_1.OptionsReadMode.Prefetch);
this.externalmap = (this.options.getValue('external-modulemap'));
if (!!this.externalmap) {
try {
console.log("INFO: applying regexp ", this.externalmap, " to calculate module names");
this.mapRegEx = new RegExp(this.externalmap);
this.isMappingEnabled = true;
console.log("INFO: Enabled", this.isMappingEnabled);
}
catch (e) {
console.log("WARN: external map not recognized. Not processing.", e);
}
}
}
onDeclarationBegin(context, reflection, node) {
if (!node || !this.isMappingEnabled)
return;
var fileName = node.fileName;
let match = this.mapRegEx.exec(fileName);
/*
*/
if (null != match) {
console.log(' Mapping ', fileName, ' ==> ', match[1]);
this.modules.add(match[1]);
this.moduleRenames.push({
renameTo: match[1],
reflection: reflection
});
}
}
/**
* Triggered when the converter begins resolving a project.
*
* @param context The context object describing the current state the converter is in.
*/
onBeginResolve(context) {
let projRefs = context.project.reflections;
let refsArray = Object.keys(projRefs).reduce((m, k) => { m.push(projRefs[k]); return m; }, []);
// Process each rename
this.moduleRenames.forEach(item => {
let renaming = item.reflection;
// Find an existing module that already has the "rename to" name. Use it as the merge target.
let mergeTarget = refsArray.filter(ref => ref.kind === renaming.kind && ref.name === item.renameTo)[0];
// If there wasn't a merge target, just change the name of the current module and exit.
if (!mergeTarget) {
renaming.name = item.renameTo;
return;
}
if (!mergeTarget.children) {
mergeTarget.children = [];
}
// Since there is a merge target, relocate all the renaming module's children to the mergeTarget.
let childrenOfRenamed = refsArray.filter(ref => ref.parent === renaming);
childrenOfRenamed.forEach((ref) => {
// update links in both directions
//console.log(' merging ', mergeTarget, ref);
ref.parent = mergeTarget;
mergeTarget.children.push(ref);
});
// Now that all the children have been relocated to the mergeTarget, delete the empty module
// Make sure the module being renamed doesn't have children, or they will be deleted
if (renaming.children)
renaming.children.length = 0;
CommentPlugin_1.CommentPlugin.removeReflection(context.project, renaming);
});
this.modules.forEach((name) => {
let ref = refsArray.filter(ref => ref.name === name)[0];
let root = ref.originalName.replace(new RegExp(`${name}.*`, 'gi'), name);
try {
// tslint:disable-next-line ban-types
Object.defineProperty(ref, "kindString", {
get() { return "Package"; },
set(newValue) { return "Package"; },
});
let readme = fs.readFileSync(path.join(root, 'README.md'));
ref.comment = new comments_1.Comment("", marked(readme.toString()));
}
catch (e) {
console.error(`No README found for module "${name}"`);
}
});
}
};
ExternalModuleMapPlugin = __decorate([
components_1.Component({ name: 'external-module-map' })
], ExternalModuleMapPlugin);
exports.ExternalModuleMapPlugin = ExternalModuleMapPlugin;
});