Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/xan105/web-component-markdown

Web-component to render markdown into html with syntax highlighting
https://github.com/xan105/web-component-markdown

browser esbuild esm markdown web-component

Last synced: 3 days ago
JSON representation

Web-component to render markdown into html with syntax highlighting

Awesome Lists containing this project

README

        

About
=====

Web-component to load an external markdown file (.md) and render it into sanitized HTML.

- Syntax highlighting
- Table of contents
- Copy code to clipboard
- Light DOM CSS styling
- Optional JavaScript API

📦 Scoped `@xan105` packages are for my own personal use but feel free to use them.

[![Published on webcomponents.org](https://img.shields.io/badge/webcomponents.org-published-blue.svg?style=flat-square)](https://www.webcomponents.org/element/@xan105/markdown)

Example
=======

Import and define the Web-component:

```js
import { Markdown } from "/path/to/md.js"
customElements.define("mark-down", Markdown);
```

HTML:

```html

```

Optional JavaScript API:

```js
const el = document.querySelector("mark-down");
el.addEventListener("load", ()=>{
console.log("loading...");
});
el.addEventListener("success", ()=>{
console.log("ok");
});
el.addEventListener("failure", ({detail})=>{
console.error(detail.error);
});

//auto rendering (default)
el.integrity = "sha384-0xABCD...";
el.src = "/path/to/md";

//manual rendering
el.manual = true;
el.src = "/path/to/md";
el.render().catch((err)=>{
console.error(err);
});

//Table of contents
querySelector("#toc").innerHTML = el.headings.toHTML({ depth: 4 });

el.addEventListener("intersect", ({detail})=>{
//Do something when a heading (h1, h2, ...) has entered the top of the viewport
querySelector(`#toc a[href="#${detail.id}"]`).classList.add("active");
});
```

Install
=======

```
npm i @xan105/markdown
```

💡 The bundled library and its minified version can be found in the `./dist` folder.

### Via importmap

Create an importmap and add it to your html:

```html

{
"imports": {
"@xan105/markdown": "./path/to/node_modules/@xan105/markdown/dist/md.min.js"
}
}