|
| 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