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 🙏

© 2026 – Pkg Stats / Ryan Hefner

@socketry/syntax

v0.3.3

Published

A modern, framework-agnostic syntax highlighter using Web Components

Downloads

18

Readme

@socketry/syntax

A modern, framework-agnostic syntax highlighter using Web Components. This is a reimplementation of jQuery.Syntax without jQuery dependencies.

Features

  • 🎨 Modern Web Components - Uses autonomous custom elements
  • 📦 Dynamic Loading - Loads language definitions on-demand
  • 🔒 No Dependencies - Pure JavaScript, no jQuery required
  • 🎯 Framework Agnostic - Works with React, Vue, vanilla JS, etc.
  • 🌲 Clean Architecture - Well-structured classes with clear responsibilities
  • 🔐 Private Fields - Modern JavaScript with proper encapsulation

Installation

npm install @socketry/syntax

Usage

Automatic Upgrade

The easiest way to use Syntax is to let it automatically upgrade existing <code> elements:

<script type="module">
	import Syntax from '@socketry/syntax';
	
	// Automatically upgrade all code blocks with language classes
	document.addEventListener('DOMContentLoaded', async function() {
		await Syntax.highlight();
	});
</script>

<!-- Standard code blocks - will be automatically upgraded -->
<pre><code class="language-javascript">
const hello = "world";
console.log(hello);
</code></pre>

<p>Inline code: <code class="language-javascript">const x = 1;</code></p>

By default, Syntax.highlight() will:

  • Find all <code> elements with class names matching language-* e.g., language-javascript, language-python.
  • Replace them with <syntax-code> web components.
  • Automatically detect if they're inside <pre> tags for proper wrapping behavior.

You can customize the selector:

// Only upgrade specific elements:
await Syntax.highlight({
	selector: 'code.highlight'
});

Manual Web Components

You can also use the <syntax-code> web component directly:

<script type="module">
	import {Syntax} from '@socketry/syntax';
	
	// Disable automatic upgrade, just register the component:
	await Syntax.highlight({upgradeAll: false});
</script>

<!-- Use the web component directly -->
<syntax-code language="javascript">
	const hello = "world"; console.log(hello);
</syntax-code>

<!-- Inside a <pre> tag for block display with line wrapping -->
<pre><syntax-code language="python" wrap>
def greet(name):
    print(f"Hello, {name}!")
</syntax-code></pre>

Programmatic Usage

import Syntax from '@socketry/syntax';
import registerJavaScript from '@socketry/syntax/Language/javascript.js';

// Create a Syntax instance
const syntax = new Syntax();
registerJavaScript(syntax);

// Get the JavaScript language and process code:
const language = await syntax.getLanguage('javascript');
const code = 'const x = 10;';
const html = await language.process(code);
document.body.appendChild(html);

The wrap Attribute

The <syntax-code> element automatically detects whether it's inside a <pre> tag:

  • Inside <pre>: Sets wrap attribute, enables line wrapping with proper indentation.
  • Standalone: No wrap attribute, uses horizontal scrolling for long lines.

You can manually control this:

<!-- Force wrapping even outside <pre> -->
<syntax-code language="javascript" wrap>
const reallyLongLine = "This will wrap instead of scroll";
</syntax-code>

<!-- Disable wrapping even inside <pre> -->
<pre><syntax-code language="javascript">
const code = "This will scroll horizontally";
</syntax-code></pre>

Command Line Tool

A simple CLI tool is included to inspect the AST (Abstract Syntax Tree) of parsed code:

node bin/syntax-ast.js <language> <code>

Examples:

# Parse JavaScript
node bin/syntax-ast.js javascript "const x = 1;"

# Parse Markdown
node bin/syntax-ast.js markdown '`inline code`'

# Parse Python
node bin/syntax-ast.js python "def foo(): pass"

Output shows all matched tokens with their type, position, length, and text:

Language: javascript
Code: "const x = 1;"

Matches: 3
────────────────────────────────────────────────────────────────────────────────
[keyword] @0..5 (5 chars)
  Text: "const"
[operator] @8..9 (1 chars)
  Text: "="
[constant] @10..11 (1 chars)
  Text: "1"

This is useful for:

  • Debugging language definitions
  • Understanding how code is tokenized
  • Testing pattern matching
  • Developing new language support