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AE,IAAI,CAAC,MAAM,CAAC,CAAC,EAAE,UAAU,CAAC;aACrC,CAAC;SACL;aAAM;YACH,OAAO;gBACH,OAAO,EAAE,IAAI;gBACb,MAAM,EAAE,IAAI;aACf,CAAC;SACL;IACL,CAAC;CACJ,CAAA;AA5IG;IALC,kBAAM,CAAC;QACJ,IAAI,EAAE,wBAAwB;QAC9B,IAAI,EAAE,mFAAmF;QACzF,IAAI,EAAE,2BAAa,CAAC,OAAO;KAC9B,CAAC;iEAC+B;AAhBxB,iBAAiB;IAD7B,sBAAS,CAAC,EAAC,IAAI,EAAE,cAAc,EAAC,CAAC;GACrB,iBAAiB,CA4J7B;AA5JY,8CAAiB","sourcesContent":["import * as Util from 'util';\n\nimport { Reflection } from '../../models/reflections/abstract';\nimport { Component, ContextAwareRendererComponent } from '../components';\nimport { MarkdownEvent, RendererEvent } from '../events';\nimport { Option } from '../../utils/component';\nimport { ParameterType } from '../../utils/options/declaration';\n\n/**\n * A plugin that builds links in markdown texts.\n */\n@Component({name: 'marked-links'})\nexport class MarkedLinksPlugin extends ContextAwareRendererComponent {\n /**\n * Regular expression for detecting bracket links.\n */\n private brackets: RegExp = /\\[\\[([^\\]]+)\\]\\]/g;\n\n /**\n * Regular expression for detecting inline tags like {@link ...}.\n */\n private inlineTag: RegExp = /(?:\\[(.+?)\\])?\\{@(link|linkcode|linkplain)\\s+((?:.|\\n)+?)\\}/gi;\n\n @Option({\n name: 'listInvalidSymbolLinks',\n help: 'Emits a list of broken symbol [[navigation]] links after documentation generation',\n type: ParameterType.Boolean\n })\n listInvalidSymbolLinks!: boolean;\n\n private warnings: string[] = [];\n\n /**\n * Create a new MarkedLinksPlugin instance.\n */\n initialize() {\n super.initialize();\n this.listenTo(this.owner, {\n [MarkdownEvent.PARSE]: this.onParseMarkdown,\n [RendererEvent.END]: this.onEndRenderer\n }, undefined, 100);\n }\n\n /**\n * Find all references to symbols within the given text and transform them into a link.\n *\n * This function is aware of the current context and will try to find the symbol within the\n * current reflection. It will walk up the reflection chain till the symbol is found or the\n * root reflection is reached. As a last resort the function will search the entire project\n * for the given symbol.\n *\n * @param text The text that should be parsed.\n * @returns The text with symbol references replaced by links.\n */\n private replaceBrackets(text: string): string {\n return text.replace(this.brackets, (match: string, content: string): string => {\n const split = MarkedLinksPlugin.splitLinkText(content);\n return this.buildLink(match, split.target, split.caption);\n });\n }\n\n /**\n * Find symbol {@link ...} strings in text and turn into html links\n *\n * @param text The string in which to replace the inline tags.\n * @return The updated string.\n */\n private replaceInlineTags(text: string): string {\n return text.replace(this.inlineTag, (match: string, leading: string, tagName: string, content: string): string => {\n const split = MarkedLinksPlugin.splitLinkText(content);\n const target = split.target;\n const caption = leading || split.caption;\n const monospace = tagName === 'linkcode';\n\n return this.buildLink(match, target, caption, monospace);\n });\n }\n\n /**\n * Format a link with the given text and target.\n *\n * @param original The original link string, will be returned if the target cannot be resolved..\n * @param target The link target.\n * @param caption The caption of the link.\n * @param monospace Whether to use monospace formatting or not.\n * @returns A html link tag.\n */\n private buildLink(original: string, target: string, caption: string, monospace?: boolean): string {\n let attributes = '';\n if (this.urlPrefix.test(target)) {\n attributes = ' class=\"external\"';\n } else {\n let reflection: Reflection | undefined;\n if (this.reflection) {\n reflection = this.reflection.findReflectionByName(target);\n } else if (this.project) {\n reflection = this.project.findReflectionByName(target);\n }\n\n if (reflection && reflection.url) {\n if (this.urlPrefix.test(reflection.url)) {\n target = reflection.url;\n attributes = ' class=\"external\"';\n } else {\n target = this.getRelativeUrl(reflection.url);\n }\n } else {\n const fullName = (this.reflection || this.project)!.getFullName();\n this.warnings.push(`In ${fullName}: ${original}`);\n return original;\n }\n }\n\n if (monospace) {\n caption = '<code>' + caption + '</code>';\n }\n\n return Util.format('<a href=\"%s\"%s>%s</a>', target, attributes, caption);\n }\n\n /**\n * Triggered when [[MarkedPlugin]] parses a markdown string.\n *\n * @param event\n */\n onParseMarkdown(event: MarkdownEvent) {\n event.parsedText = this.replaceInlineTags(this.replaceBrackets(event.parsedText));\n }\n\n /**\n * Triggered when [[Renderer]] is finished\n */\n onEndRenderer(event: RendererEvent) {\n if (this.listInvalidSymbolLinks && this.warnings.length > 0) {\n this.application.logger.write('');\n this.application.logger.warn('[MarkedLinksPlugin]: Found invalid symbol reference(s) in JSDocs, ' +\n 'they will not render as links in the generated documentation.');\n\n for (let warning of this.warnings) {\n this.application.logger.write(' ' + warning);\n }\n }\n }\n\n /**\n * Split the given link into text and target at first pipe or space.\n *\n * @param text The source string that should be checked for a split character.\n * @returns An object containing the link text and target.\n */\n static splitLinkText(text: string): { caption: string; target: string; } {\n let splitIndex = text.indexOf('|');\n if (splitIndex === -1) {\n splitIndex = text.search(/\\s/);\n }\n\n if (splitIndex !== -1) {\n return {\n caption: text.substr(splitIndex + 1).replace(/\\n+/, ' '),\n target: text.substr(0, splitIndex)\n };\n } else {\n return {\n caption: text,\n target: text\n };\n }\n }\n}\n"]}
\No newline at end of file