)` would send the * conversation off-site. A site that wants images adds `img` back through the filter below. */ final class Markdown { private ConverterInterface $converter; public function __construct(?ConverterInterface $converter = null) { if ($converter === null) { $env = new Environment(['html_input' => 'strip', 'allow_unsafe_links' => false, 'max_nesting_level' => 50]); $env->addExtension(new CommonMarkCoreExtension()); $env->addExtension(new TableExtension()); $env->addExtension(new StrikethroughExtension()); $env->addExtension(new AutolinkExtension()); $converter = new MarkdownConverter($env); } $this->converter = $converter; } public function toHtml(string $markdown): string { $html = (string) $this->converter->convert($markdown); return wp_kses($html, self::allowedTags()); } /** * The wp_kses allowlist, after the `alpaca_bot/render/allowed_tags` filter, which is the * seam for a site that wants (say) `` or a `title` on links. * * @return array> */ public static function allowedTags(): array { $attrs = ['class' => true]; $tags = ['p' => [], 'br' => [], 'strong' => [], 'em' => [], 'del' => [], 'code' => $attrs, 'pre' => $attrs, 'blockquote' => [], 'ul' => [], 'ol' => ['start' => true], 'li' => [], 'h1' => [], 'h2' => [], 'h3' => [], 'h4' => [], 'h5' => [], 'h6' => [], 'hr' => [], 'a' => ['href' => true, 'rel' => true, 'target' => true], 'table' => [], 'thead' => [], 'tbody' => [], 'tr' => [], 'th' => ['align' => true], 'td' => ['align' => true]]; /** * Filters the wp_kses allowlist every rendered reply passes through (the chat screen, the * shortcodes, the REST view route). Add a tag or an attribute the model's markdown should * be allowed to keep (`kbd`, a `title` on links), or remove one to have it stripped. The * shape is wp_kses's: tag => attribute => true. * * @since 0.5.0 * @param array> $tags the default allowlist */ return (array) apply_filters('alpaca_bot/render/allowed_tags', $tags); } }