Skip to content

A vanilla Markdown Web Component with optional syntax highlighting support

License

Notifications You must be signed in to change notification settings

vanillawc/wc-markdown

Repository files navigation

GitHub Releases NPM Release Downloads MIT License Published on WebComponents.org Latest Status Release Status

A Vanilla Web Component to bring native Markdown (w/ syntax highlighting) support to the browser

Installation

npm i @vanillawc/wc-markdown

Then import the index.js file at the root of the package.


Usage

Load an external Markdown file using the src attribute

<wc-markdown src="assets/sample.md"></wc-markdown>

Insert the Markdown directly into the body of the element

<wc-markdown>
## Inline Markdown

This some sample markdown.
</wc-markdown>

Note: Since the content is contained within html both the opening < and closing > brackets need to be properly escaped.

Inject Markdown via the 'value' property

<wc-markdown></wc-markdown>
<script>
window.onload = () => {;
  document.querySelector("wc-markdown").value = `
  ## Value Prop Markdown

  This sample is loaded from the \`value\` property of the \`&lt;wc-markdown&gt;\` tag
  `;
};
</script>

Enable syntax highlighting with the highlight attribute

<wc-markdown src="assets/sample2.md" highlight></wc-markdown>

By default, even with highlight enabled the code will appear unstyled. To add style a theme needs to be imported globally. The themes can be found under dist/themes/.

<link rel="stylesheet" href="/node_modules/@vanillawc/wc-markdown/dist/themes/prism-okaidia.css">

Enable additional languages

The core of PrismJS included with this WC only supports the following languages [ html, xml, svg, mathml, css, javascript ]. Support for additional languages can be found in dist/components/.

<script src="/node_modules/@vanillawc/wc-markdown/dist/components/prism-typescript.min.js"></script>

Demo