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

https://github.com/stagas/code-syntax

Code syntax highlight Web Component
https://github.com/stagas/code-syntax

custom-element highlight syntax web-component

Last synced: about 1 year ago
JSON representation

Code syntax highlight Web Component

Awesome Lists containing this project

README

          


code-syntax

Code syntax highlight Web Component



npm i code-syntax

pnpm add code-syntax

yarn add code-syntax

## Examples

# web


    Try it live

    # view source example/web.ts

    ```ts
    import 'plenty-themes/cobalt2.css'
    import 'plenty-themes/laser.css'
    import 'code-syntax/themes/default.css'

    import { CodeSyntaxElement, languages } from 'code-syntax'
    import js from 'code-syntax/languages/js'

    customElements.define('code-syntax', CodeSyntaxElement)

    languages.js = js // can also be import(code-syntax.) directly

    document.body.innerHTML = `
    class Vector {
    x: number
    y: number
    constructor(x: number, y: number) {
    this.x = x
    this.y = y
    }
    add(v: Vector): Vector {
    return new Vector(this.x + v.x, this.y + v.y)
    }
    }

    `
    ```


## API

# CodeSyntaxElement src/element.ts#L45

# language  =  $.String src/element.ts#L48

    undefined | string

# onTextContent src/element.ts#L56

    # (textContent)

      # textContent


        string


      (textContent)  =>


        void

# onmounted # onunmounted # root  =  ... src/element.ts#L46

    ShadowRoot

# syntax src/element.ts#L51 # theme  =  $.String src/element.ts#L49

    undefined | string

# mounted($) src/element.ts#L58

mounted($)  =>

    void
# PatchOptions src/util.ts#L3 # SyntaxDefinition src/syntax.ts#L6
# RegExpMap src/syntax.ts#L3 # RegExpMapped src/syntax.ts#L4 # SyntaxOrImport src/syntax.ts#L10 # languages  =  {} src/element.ts#L8 # compile(def, keys) – Compiles a syntax definition. src/syntax.ts#L24


    ```js
    const r = await compile({
    foo: /[a-z]/,
    bar: /[0-9]/,
    })
    ```


    # def – The syntax definition to compile. Can be a promise returned by import().

    # keys  =  ...

      Set<string>

    compile(def, keys)  =>


# patchElements(elements, Partial<PatchOptions>) src/util.ts#L8 # patchPreCodeElements(opts) src/util.ts#L27

    # opts


    patchPreCodeElements(opts)  =>


      void
# syntax(regexp, s) – Syntax highlights a string as html with the given syntax. src/syntax.ts#L68


    ```js
    const regexp = await compile({
    foo: /[a-z]+/,
    bar: [
    /[0-9]+/,
    {
    bar: /[0-5]+/,
    },
    ],
    })
    const html = syntax(regexp, 'hello 123 789 world')
    ```


    # regexp – The syntax definition returned by compile()
    # s – The string to highlight.

      string

    syntax(regexp, s)  =>


      string

## Credits

- [html-escaper](https://npmjs.org/package/html-escaper) by [Andrea Giammarchi](https://github.com/WebReflection) – fast and safe way to escape and unescape &<>'" chars
- [sigl](https://npmjs.org/package/sigl) by [stagas](https://github.com/stagas) – Web framework

## Contributing

[Fork](https://github.com/stagas/code-syntax/fork) or [edit](https://github.dev/stagas/code-syntax) and submit a PR.

All contributions are welcome!

## License

MIT © 2022 [stagas](https://github.com/stagas)