UNPKG

5.02 kBJavaScriptView Raw
1"use strict";
2var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3 var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4 if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5 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;
6 return c > 3 && r && Object.defineProperty(target, key, r), r;
7};
8var MarkedLinksPlugin_1;
9Object.defineProperty(exports, "__esModule", { value: true });
10const Util = require("util");
11const components_1 = require("../components");
12const events_1 = require("../events");
13const component_1 = require("../../utils/component");
14const declaration_1 = require("../../utils/options/declaration");
15let MarkedLinksPlugin = MarkedLinksPlugin_1 = class MarkedLinksPlugin extends components_1.ContextAwareRendererComponent {
16 constructor() {
17 super(...arguments);
18 this.brackets = /\[\[([^\]]+)\]\]/g;
19 this.inlineTag = /(?:\[(.+?)\])?\{@(link|linkcode|linkplain)\s+((?:.|\n)+?)\}/gi;
20 this.warnings = [];
21 }
22 initialize() {
23 super.initialize();
24 this.listenTo(this.owner, {
25 [events_1.MarkdownEvent.PARSE]: this.onParseMarkdown,
26 [events_1.RendererEvent.END]: this.onEndRenderer
27 }, undefined, 100);
28 }
29 replaceBrackets(text) {
30 return text.replace(this.brackets, (match, content) => {
31 const split = MarkedLinksPlugin_1.splitLinkText(content);
32 return this.buildLink(match, split.target, split.caption);
33 });
34 }
35 replaceInlineTags(text) {
36 return text.replace(this.inlineTag, (match, leading, tagName, content) => {
37 const split = MarkedLinksPlugin_1.splitLinkText(content);
38 const target = split.target;
39 const caption = leading || split.caption;
40 const monospace = tagName === 'linkcode';
41 return this.buildLink(match, target, caption, monospace);
42 });
43 }
44 buildLink(original, target, caption, monospace) {
45 let attributes = '';
46 if (this.urlPrefix.test(target)) {
47 attributes = ' class="external"';
48 }
49 else {
50 let reflection;
51 if (this.reflection) {
52 reflection = this.reflection.findReflectionByName(target);
53 }
54 else if (this.project) {
55 reflection = this.project.findReflectionByName(target);
56 }
57 if (reflection && reflection.url) {
58 if (this.urlPrefix.test(reflection.url)) {
59 target = reflection.url;
60 attributes = ' class="external"';
61 }
62 else {
63 target = this.getRelativeUrl(reflection.url);
64 }
65 }
66 else {
67 const fullName = (this.reflection || this.project).getFullName();
68 this.warnings.push(`In ${fullName}: ${original}`);
69 return original;
70 }
71 }
72 if (monospace) {
73 caption = '<code>' + caption + '</code>';
74 }
75 return Util.format('<a href="%s"%s>%s</a>', target, attributes, caption);
76 }
77 onParseMarkdown(event) {
78 event.parsedText = this.replaceInlineTags(this.replaceBrackets(event.parsedText));
79 }
80 onEndRenderer(event) {
81 if (this.listInvalidSymbolLinks && this.warnings.length > 0) {
82 this.application.logger.write('');
83 this.application.logger.warn('[MarkedLinksPlugin]: Found invalid symbol reference(s) in JSDocs, ' +
84 'they will not render as links in the generated documentation.');
85 for (let warning of this.warnings) {
86 this.application.logger.write(' ' + warning);
87 }
88 }
89 }
90 static splitLinkText(text) {
91 let splitIndex = text.indexOf('|');
92 if (splitIndex === -1) {
93 splitIndex = text.search(/\s/);
94 }
95 if (splitIndex !== -1) {
96 return {
97 caption: text.substr(splitIndex + 1).replace(/\n+/, ' '),
98 target: text.substr(0, splitIndex)
99 };
100 }
101 else {
102 return {
103 caption: text,
104 target: text
105 };
106 }
107 }
108};
109__decorate([
110 component_1.Option({
111 name: 'listInvalidSymbolLinks',
112 help: 'Emits a list of broken symbol [[navigation]] links after documentation generation',
113 type: declaration_1.ParameterType.Boolean
114 })
115], MarkedLinksPlugin.prototype, "listInvalidSymbolLinks", void 0);
116MarkedLinksPlugin = MarkedLinksPlugin_1 = __decorate([
117 components_1.Component({ name: 'marked-links' })
118], MarkedLinksPlugin);
119exports.MarkedLinksPlugin = MarkedLinksPlugin;
120//# sourceMappingURL=MarkedLinksPlugin.js.map
\No newline at end of file