UNPKG

@connectv/marked

Version:
61 lines 3.15 kB
import { InlineLexer } from 'marked'; export class InlineProcessor { constructor(tokens, renderer, options) { this.renderer = renderer; this.options = options; this.lexer = new InlineLexer(tokens.links); } process(block) { const renderer = this.renderer; return this.buildFrom(renderer.create("span", { _innerHTML: this.lexer.output(block) })); } children(src) { const l = []; src.childNodes.forEach(node => l.push(node)); return l; } buildFrom(src) { const renderer = this.renderer; const res = renderer.create("fragment", null); this.children(src).forEach(node => { if (node instanceof Text && this.options.Text) { renderer.render(renderer.create(this.options.Text, null, node.textContent /* istanbul ignore next */ || '')) .on(res); } else if (node instanceof HTMLAnchorElement && this.options.Link) { const options = {}; if (node.hasAttribute('href')) options['href'] = node.getAttribute('href') /* istanbul ignore next */ || ''; if (node.hasAttribute('title')) options['title'] = node.getAttribute('title') /* istanbul ignore next */ || ''; renderer.render(renderer.create(this.options.Link, Object.assign({}, options), this.buildFrom(node))).on(res); } else if (node instanceof HTMLElement && node.tagName === 'EM' && this.options.Em) { renderer.render(renderer.create(this.options.Em, null, this.buildFrom(node))).on(res); } else if (node instanceof HTMLElement && node.tagName === 'STRONG' && this.options.Strong) { renderer.render(renderer.create(this.options.Strong, null, this.buildFrom(node))).on(res); } else if (node instanceof HTMLElement && node.tagName === 'DEL' && this.options.Del) { renderer.render(renderer.create(this.options.Del, null, this.buildFrom(node))).on(res); } else if (node instanceof HTMLElement && node.tagName === 'CODE' && this.options.CodeSpan) { renderer.render(renderer.create(this.options.CodeSpan, null, this.buildFrom(node))).on(res); } else if (node instanceof HTMLImageElement && this.options.Image) { const options = {}; if (node.hasAttribute('src')) options['src'] = node.getAttribute('src') /* istanbul ignore next */ || ''; if (node.hasAttribute('title')) options['title'] = node.getAttribute('title') /* istanbul ignore next */ || ''; if (node.hasAttribute('alt')) options['alt'] = node.getAttribute('alt') /* istanbul ignore next */ || ''; renderer.render(renderer.create(this.options.Image, Object.assign({}, options))).on(res); } else renderer.render(node).on(res); }); return res; } } //# sourceMappingURL=inline.js.map