Markdown

<wa-markdown> Experimental Since 3.4

Markdown elements render markdown content as HTML directly in the browser, making it easy to display user-generated content or documentation without a server-side build step.

The markdown component turns raw markdown into rendered HTML using the Marked library. Indentation is handled automatically. You can nest your markdown at any depth to match the surrounding HTML structure and the common leading whitespace will be stripped before parsing.

Since content is rendered client-side, it won't be visible to search engine crawlers or available before JavaScript loads. This makes it a poor fit for SEO-critical content like landing pages and blog posts. It's best suited for prototyping, dashboards, admin panels, and other contexts where search indexing isn't a concern.

Do not use this component with unsanitized user input. Markdown is parsed as-is without sanitization, so rendering untrusted content can lead to XSS vulnerabilities.

Examples

Link to This Section

Providing content

Link to This Section

Markdown must go inside a <script type="text/markdown"> element, which must be a direct child of the markdown component. (The script is required to prevent the browser from parsing the content.) The rendered output is placed in the light DOM where it inherits your page's styles.

<wa-markdown>
  <script type="text/markdown">
    Markdown with `<angle brackets>` and **formatting** that won't get parsed as HTML.
  </script>
</wa-markdown>

The Marked library is used under the hood to render markdown. Marked supports GitHub Flavored Markdown (GFM) and the CommonMark specification. This includes headings, bold, italic, links, images, lists, blockquotes, code blocks, tables, task lists, strike-through, and auto-links. For a full breakdown of supported syntax, see the Marked documentation.

Whitespace normalization

Link to This Section

Indentation inside the script is automatically normalized before the markdown parser sees it. This lets you indent your content to match the surrounding HTML without it being treated as a code block. The normalization process:

  1. Converts leading tabs to spaces based on the configured tab stop width (default: 4)
  2. Trims blank lines from the start and end
  3. Determines the smallest indentation shared by all non-empty lines
  4. Removes that common prefix from every line

This means you can write markdown at any indentation level and it will render correctly. Even if the content is indented by 8 spaces to match the HTML structure, the output renders as though it had no extra indentation at all.

For tab-indented source files, adjust the tab stop width with the tab-size attribute.

<wa-markdown tab-size="2">
  <script type="text/markdown">
    ...
  </script>
</wa-markdown>

Formatting features

Link to This Section

All standard markdown formatting supported by Marked is available, including headings, lists, blockquotes, code blocks, links, and images.

Configuring Marked

Link to This Section

All <wa-markdown> instances share a single Marked instance. You can access it through any <wa-markdown> element's marked property. After making changes, call WaMarkdown.updateAll() to re-render every connected instance.

The Marked instance is shared across all <wa-markdown> elements. If you want every instance on the page to pick up the new configuration, call WaMarkdown.updateAll() instead of renderMarkdown() on a single element.

Writing a custom Marked plugin

Link to This Section

Custom Marked extensions can be applied through any element's marked property. The example below adds support for ==highlight== syntax, wrapping matched text in <mark> tags.

Updating content dynamically

Link to This Section

The component parses and renders automatically when the script element is first slotted in. It does not watch for subsequent changes to the script's content. To re-render after modifying the source, update the script's textContent and call renderMarkdown().


Update content

Importing

Link to This Section

If you're using the autoloader or a hosted project, components load on demand — no manual import needed. To cherry-pick a component manually, use one of the following snippets.

CDN npm Self-Hosted React

Import this component directly from the CDN:

import 'https://ka-f.webawesome.com/[email protected]/components/markdown/markdown.js';

After installing Web Awesome via npm, import this component:

import '@awesome.me/webawesome/dist/components/markdown/markdown.js';

If you're self-hosting Web Awesome, import this component from your server:

import './webawesome/dist/components/markdown/markdown.js';

To import this component for React 18 or below, use the following code:

import WaMarkdown from '@awesome.me/webawesome/dist/react/markdown/index.js';

Attributes & Properties

Link to This Section

Learn more about attributes and properties.

Methods

Link to This Section

Learn more about methods.

Need a hand? Report a bug Ask for help
    No results