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

https://github.com/andreruffert/syntax-highlight-element

πŸ‘“ Syntax Highlighting using the CSS Custom Highlight API
https://github.com/andreruffert/syntax-highlight-element

custom-elements syntax-highlighting web-components

Last synced: 1 day ago
JSON representation

πŸ‘“ Syntax Highlighting using the CSS Custom Highlight API

Awesome Lists containing this project

README

        

# <syntax-highlight> element

> Syntax Highlighting using the CSS Custom Highlight API.

[![Test status](https://img.shields.io/github/actions/workflow/status/andreruffert/syntax-highlight-element/test.yml?label=Test&logo=github&color=32A9C3&labelColor=1B3C4A)](https://github.com/andreruffert/syntax-highlight-element/actions/workflows/test.yml)
[![npm version](https://img.shields.io/npm/v/syntax-highlight-element?color=32A9C3&labelColor=1B3C4A)](https://www.npmjs.com/package/syntax-highlight-element)
[![npm downloads](https://img.shields.io/npm/dm/syntax-highlight-element?logo=npm&color=32A9C3&labelColor=1B3C4A)](https://www.npmjs.com/package/syntax-highlight-element)

The code is highlighted without having to wrap a bunch of `` elements around each token, thanks to [Prism][prism_github]'s tokenizer and the [CSS Custom Highlight API][MDN_CSS_Custom_Highlight_API].






Screenshot of the <syntax-highlight> element demo in the browser with DevTools open




## Install

Install via npm

```shell
npm install syntax-highlight-element
```

## Usage

### JavaScript

Import as ES module

```js
import 'syntax-highlight-element';
```

Or via CDN

```html

```

### HTML

```html

// ...

```

### CSS

Make sure to load a theme e.g. `syntax-highlight-element/themes/prettylights.css`.

Or via CDN

```html

```

Currently there are only limited [themes](https://github.com/andreruffert/syntax-highlight-element/tree/main/src/themes) available/converted. You can always create your own theme. Contributions are also very welcome.

## Attributes

* `language` The code language. The default is `plaintext`. Default suported languages `html|css|js`.
* `content-selector` A CSS selector to specify the content element. The default is the element itself.

## Configuration
```js
/**
* @typedef Config
* @type {object}
* @property {string[]} [languages=['markup', 'css', 'javascript']] - Language grammars to highlight.
* @property {{ [key: string]: string[] }} languageTokens - Language specific token types.
*/
window.she = window.she || {};
/** @type {Config} */
window.she.config = {};
```

Full list of all [languages supported](https://prismjs.com/#supported-languages) by the prism tokenizer.

## Browser Support


Baseline limited availability wordmark

- [CSS Custom Highlight API][caniuse_mdn-api_highlight]

## Credits

* [Bramus Van Damme][bramus_github] and his [blog post][bramus_blog_post] where I first read about using the [CSS Custom Highlight API][MDN_CSS_Custom_Highlight_API] for syntax highlighting.
* [Prism][prism_github]'s tokenizer.

## License

Distributed under the MIT license. See LICENSE for details.

Β© [AndrΓ© Ruffert](https://andreruffert.com)

[prism_github]: https://github.com/PrismJS/prism
[bramus_github]: https://github.com/bramus
[bramus_blog_post]: https://www.bram.us/2024/02/18/custom-highlight-api-for-syntax-highlighting
[MDN_CSS_Custom_Highlight_API]: https://developer.mozilla.org/en-US/docs/Web/API/CSS_Custom_Highlight_API
[caniuse_mdn-api_highlight]: https://caniuse.com/mdn-api_highlight