monogon
Version:
Modern syntax highlighting for developer tooling
153 lines (108 loc) • 3.6 kB
Markdown
2kb `<code>` block with syntax highlighting meant for the dev environment.
Zero dependencies, support for [most frameworks](
Using the CSS Highlight, meaning [low browser support](https://caniuse.com/mdn-api_highlight).

> [!IMPORTANT]
> Monogon is meant for **developer tooling** and supports _very few_ languages. Think config files.
>
> If you don't need editable code snippets, look at [Shiki](https://github.com/shikijs/shiki), it's amazing.
>
> If you need editable snippets, [Codemirror](https://github.com/codemirror/dev/) might be worth a look.
>
> No SSR support for now.
| Language | property | syntax | formatting |
| --------- | --------- | ------ | ---------- |
| JSON | json | ✅ | ✅ |
| YAML | yaml | ✅ | ❌ |
| CSS | css | ❓ | ❌ |
| plaintext | plaintext | - | - |
Import it
```js
import 'monogon';
```
Use in your HTML as a web component.
```html
<monogon-code lang="json" content='{ "names": ["ian", "ana"], size: "2kb", status: null }'>Click me</monogon-code>
<script>
document.querySelector('monogon-code').addEventListener('input', (event) => {
console.log(event.target.value);
});
</script>
```
```js
import MonogonCode from 'monogon/vue';
const content = ref('{ "json": ["this", "is", "json"], "how": 42 }')
const handleInput = (e) => { content.value = e.target.value }
<MonogonCode :content="content" @input="handleInput">Click me</MonogonCode>
```
```jsx
import MonogonCode from 'monogon/react';
const content = '{ "json": ["this", "is", "json"], "how": 42 }';
const handleInput = (e) => { console.log(e.target.value) }
<MonogonCode content={content} lang="json" onInput={handleInput} />
```
```jsx
import MonogonCode from 'monogon/preact';
const content = '{ "json": ["this", "is", "json"], "how": 42 }';
const handleInput = (e) => { console.log(e.target.value) }
<MonogonCode content={content} lang="json" onInput={handleInput} />
```
There is no native support yet for Svelte, so the web component needs to be used.
```jsx
import 'monogon';
<monogon-code {content} lang="json" oninput={handleInput}></monogon-code>
```
```jsx
import MonogonCode from 'monogon/solid';
const handleInput = (e) => { console.log(e.target.value) }
<MonogonCode content={content} lang="json" onInput={handleInput} />
```
For a read-only code block just add the `readonly` prop/attribute.
Monogon comes with a VSCode-like dark and light theme. This theme can be changed by changing the css properties on `monogon-code`.
The simplest way to do so is:
```css
monogon-el.custom-theme-name {
--mng-background:
/* ...other changes, see lit below */
}
@media (prefers-color-scheme: dark) {
monogon-el.custom-theme-name {
--mng-background:
/* ...other changes, see lit below */
}
}
```
Full list of css properties used:
```css
.default-theme {
/* Generic */
--mng-text:
--mng-background:
/* Proper highlighting */
--mng-string:
--mng-key:
--mng-number:
--mng-boolean:
--mng-null:
--mng-comment:
--mng-curly-brackets:
--mng-square-brackets:
--mng-keyword:
--mng-dashes:
}
```
SSR is not yet supported, you need to load it in the browser during page load.
---
Made with 🍕 in Amsterdam.