/** * Copyright Aquera Inc 2023 * * This source code is licensed under the BSD-3-Clause license found in the * LICENSE file in the root directory of this source tree. */ import { html } from 'lit'; import { customElement, property, state } from 'lit/decorators.js'; import { unsafeHTML } from 'lit/directives/unsafe-html.js'; import { Marked } from 'marked'; import { styles } from './nile-markdown.css'; import NileElement from '../internal/nile-element'; import type { CSSResultGroup, PropertyValues, TemplateResult } from 'lit'; /** * Nile markdown component. * * @tag nile-markdown */ /** * @summary Renders markdown content as HTML in the browser using GitHub Flavored Markdown. * @status experimental * * Content can be provided either through the `value` property or through a * ` * * ``` * * Leading whitespace common to all lines is stripped, so the markdown can be * indented to match the surrounding HTML without rendering as a code block. * * All instances share a single Marked parser. Use `NileMarkdown.getMarked()` * to customize it (extensions, renderers, etc.) and `NileMarkdown.updateAll()` * to re-render existing instances after changing the configuration. * * WARNING: The markdown is converted to HTML without sanitization. Do not * render unsanitized user input, as this can expose users to XSS attacks. * * @event nile-markdown-rendered - Emitted after the markdown has been parsed and rendered. * * @csspart base - The component's base wrapper containing the rendered HTML. */ @customElement('nile-markdown') export class NileMarkdown extends NileElement { static styles: CSSResultGroup = styles; /** The shared Marked instance used by every nile-markdown on the page. */ private static marked: Marked | undefined; /** All connected instances, used by `updateAll()`. */ private static instances = new Set(); /** * Returns the shared Marked instance so it can be configured with custom * options, extensions, or renderers. Changes affect all instances; call * `NileMarkdown.updateAll()` afterwards to re-render existing ones. */ static getMarked(): Marked { if (!NileMarkdown.marked) { NileMarkdown.marked = new Marked({ gfm: true, async: false }); } return NileMarkdown.marked; } /** Re-renders every connected nile-markdown instance. */ static updateAll(): void { NileMarkdown.instances.forEach(instance => instance.renderMarkdown()); } /** * The markdown to render. Takes precedence over a * `