npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

monaco-jsx-syntax-highlight

v1.2.2

Published

Highlight the jsx or tsx syntax for monaco editor

Readme

monaco-jsx-syntax-highlight

npm version npm downloads

Support monaco jsx/tsx syntax highlight

Monaco only support the jsx syntax checker

Live demo

Installing

$ npm install monaco-jsx-syntax-highlight

Use

The main part of this package is a worker for analysing jsx syntax So we have to way to init the Controller class

Use blob create worker

import { MonacoJsxSyntaxHighlight, getWorker } from 'monaco-jsx-syntax-highlight'

const controller = new MonacoJsxSyntaxHighlight(getWorker(), monaco)

When using getWorker return value as Worker, we can custom the typescript compile source file url(for the purpose of speeding up load time)

If do not set, the default source is https://cdnjs.cloudflare.com/ajax/libs/typescript/4.6.4/typescript.min.js

const controller = new MonacoJsxSyntaxHighlight(getWorker(), monaco, {
    customTypescriptUrl: 'https://xxx/typescript.min.js'
})

Use js worker file

If your browser do not support to use blob worker, you can download the worker file and save it in your project

  • web worker has same-origin policy
import { MonacoJsxSyntaxHighlight } from 'monaco-jsx-syntax-highlight'

const controller = new MonacoJsxSyntaxHighlight('https://xxxx', monaco)

Controller

Remember, when this editor is disposed(editor.dispose), we should invoke the dispose function returned by the highlighterBuilder too

  • highlighter: send latest content to worker for analysing
  • dispose: remove event listener of the worker
// editor is the result of monaco.editor.create
const { highlighter, dispose } = monacoJsxSyntaxHighlight.highlighterBuilder(
  { editor: editor }
)

// init hightlight
highlighter()

editor.onDidChangeModelContent(() => {
  // content change, highlight
  highlighter()
})
interface HighlighterConfig {
  /**
   * max jsx tag order loop value
   * @default 3
   */
  jsxTagCycle: number
  /**
   * open console to log some error information
   * @default false
   */
  enableConsole?: boolean
}

type HighlighterBuilder = (context: {
    editor: any;
    filePath?: string;
}, config?: HighlighterConfig) => {
    highlighter: (code?: string) => void;
    dispose: () => void;
}

Highlight class

Use css class to highlight the jsx syntax

  • 'jsx-tag-angle-bracket': <>/>
  • 'jsx-tag-attribute-key': the attribute key
  • 'jsx-expression-braces': the braces of attribute value
  • 'jsx-text': the text in jsx tag content
  • 'jsx-tag-name': the tag name of jsx tag
  • 'jsx-tag-order-xxx': the tag order class

FAQ

monaco do not check the jsx syntax

You can try below config code

PS: the file name must end with jsx or tsx

monaco.languages.typescript.typescriptDefaults.setCompilerOptions({
    jsx: monaco.languages.typescript.JsxEmit.Preserve,
    target: monaco.languages.typescript.ScriptTarget.ES2020,
    esModuleInterop: true
})

const model = monaco.editor.createModel(
  'const test: number = 666',
  'typescript',
  monaco.Uri.parse('index.tsx')
)

editor.current = monaco.editor.create(editorElement.current)
editor.current.setModel(model)