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ight.js';\nimport * as Handlebars from 'handlebars';\n\nimport { Component, ContextAwareRendererComponent } from '../components';\nimport { RendererEvent, MarkdownEvent } from '../events';\nimport { Option } from '../../utils/component';\nimport { ParameterHint } from '../../utils/options/declaration';\n\nconst customMarkedRenderer = new Marked.Renderer();\n\ncustomMarkedRenderer.heading = (text, level, _, slugger) => {\n const slug = slugger.slug(text);\n\n return `\n<a href=\"#${slug}\" id=\"${slug}\" style=\"color: inherit; text-decoration: none;\">\n <h${level}>${text}</h${level}>\n</a>\n`;\n};\n\n/**\n * A plugin that exposes the markdown, compact and relativeURL helper to handlebars.\n *\n * Templates should parse all comments with the markdown handler so authors can\n * easily format their documentation. TypeDoc uses the Marked (https://github.com/markedjs/marked)\n * markdown parser and HighlightJS (https://github.com/isagalaev/highlight.js) to highlight\n * code blocks within markdown sections. Additionally this plugin allows to link to other symbols\n * using double angle brackets.\n *\n * You can use the markdown helper anywhere in the templates to convert content to html:\n *\n * ```handlebars\n * {{#markdown}}{{{comment.text}}}{{/markdown}}\n * ```\n *\n * The compact helper removes all newlines of its content:\n *\n * ```handlebars\n * {{#compact}}\n * Compact\n * this\n * {{/compact}}\n * ```\n *\n * The relativeURL helper simply transforms an absolute url into a relative url:\n *\n * ```handlebars\n * {{#relativeURL url}}\n * ```\n */\n@Component({name: 'marked'})\nexport class MarkedPlugin extends ContextAwareRendererComponent {\n @Option({\n name: 'includes',\n help: 'Specifies the location to look for included documents (use [[include:FILENAME]] in comments).',\n hint: ParameterHint.Directory\n })\n includeSource!: string;\n\n @Option({\n name: 'media',\n help: 'Specifies the location with media files that should be copied to the output directory.',\n hint: ParameterHint.Directory\n })\n mediaSource!: string;\n\n /**\n * The path referenced files are located in.\n */\n private includes?: string;\n\n /**\n * Path to the output media directory.\n */\n private mediaDirectory?: string;\n\n /**\n * The pattern used to find references in markdown.\n */\n private includePattern: RegExp = /\\[\\[include:([^\\]]+?)\\]\\]/g;\n\n /**\n * The pattern used to find media links.\n */\n private mediaPattern: RegExp = /media:\\/\\/([^ \"\\)\\]\\}]+)/g;\n\n /**\n * Create a new MarkedPlugin instance.\n */\n initialize() {\n super.initialize();\n this.listenTo(this.owner, MarkdownEvent.PARSE, this.onParseMarkdown);\n\n const that = this;\n Handlebars.registerHelper('markdown', function(arg: any) { return that.parseMarkdown(arg.fn(this), this); });\n Handlebars.registerHelper('relativeURL', (url: string) => url ? this.getRelativeUrl(url) : url);\n\n Marked.setOptions({\n highlight: (text: any, lang: any) => this.getHighlighted(text, lang),\n renderer: customMarkedRenderer\n });\n }\n\n /**\n * Highlight the synatx of the given text using HighlightJS.\n *\n * @param text The text taht should be highlightes.\n * @param lang The language that should be used to highlight the string.\n * @return A html string with syntax highlighting.\n */\n public getHighlighted(text: string, lang?: string): string {\n try {\n if (lang) {\n return HighlightJS.highlight(lang, text).value;\n } else {\n return HighlightJS.highlightAuto(text).value;\n }\n } catch (error) {\n this.application.logger.warn(error.message);\n return text;\n }\n }\n\n /**\n * Parse the given markdown string and return the resulting html.\n *\n * @param text The markdown string that should be parsed.\n * @param context The current handlebars context.\n * @returns The resulting html string.\n */\n public parseMarkdown(text: string, context: any) {\n if (this.includes) {\n text = text.replace(this.includePattern, (match: string, path: string) => {\n path = Path.join(this.includes!, path.trim());\n if (FS.existsSync(path) && FS.statSync(path).isFile()) {\n const contents = FS.readFileSync(path, 'utf-8');\n if (path.substr(-4).toLocaleLowerCase() === '.hbs') {\n const template = Handlebars.compile(contents);\n return template(context);\n } else {\n return contents;\n }\n } else {\n return '';\n }\n });\n }\n\n if (this.mediaDirectory) {\n text = text.replace(this.mediaPattern, (match: string, path: string) => {\n if (FS.existsSync(Path.join(this.mediaDirectory!, path))) {\n return this.getRelativeUrl('media') + '/' + path;\n } else {\n return match;\n }\n });\n }\n\n const event = new MarkdownEvent(MarkdownEvent.PARSE, text, text);\n\n this.owner.trigger(event);\n return event.parsedText;\n }\n\n /**\n * Triggered before the renderer starts rendering a project.\n *\n * @param event An event object describing the current render operation.\n */\n protected onBeginRenderer(event: RendererEvent) {\n super.onBeginRenderer(event);\n\n delete this.includes;\n if (this.includeSource) {\n const includes = Path.resolve(this.includeSource);\n if (FS.existsSync(includes) && FS.statSync(includes).isDirectory()) {\n this.includes = includes;\n } else {\n this.application.logger.warn('Could not find provided includes directory: ' + includes);\n }\n }\n\n if (this.mediaSource) {\n const media = Path.resolve(this.mediaSource);\n if (FS.existsSync(media) && FS.statSync(media).isDirectory()) {\n this.mediaDirectory = Path.join(event.outputDirectory, 'media');\n FS.copySync(media, this.mediaDirectory);\n } else {\n this.mediaDirectory = undefined;\n this.application.logger.warn('Could not find provided media directory: ' + media);\n }\n }\n }\n\n /**\n * Triggered when [[MarkedPlugin]] parses a markdown string.\n *\n * @param event\n */\n onParseMarkdown(event: MarkdownEvent) {\n event.parsedText = Marked(event.parsedText);\n }\n}\n"]}
\No newline at end of file