Sling Academy
Home/JavaScript/Designing Custom Syntax Highlighting with Basic JavaScript String Parsing

Designing Custom Syntax Highlighting with Basic JavaScript String Parsing

Last updated: December 12, 2024

Syntax highlighting is an essential feature in code editors and development environments that helps programmers by making code more readable. By coloring keywords, strings, and other code constructs, syntax highlighting helps developers identify different parts of code at a glance. In this article, we'll walk you through designing a custom syntax highlighter using basic JavaScript string parsing techniques.

Understanding the Basics: What is Syntax Highlighting?

Syntax highlighting involves the decoration of source code in a way that fragments the code into tokens and styles these tokens with different colors. For example, keywords might be blue, strings green, and comments gray. This feature assists developers in deciphering code structures and logic without delving deeply into the source code.

Setting Up Your Project

Before diving into the implementation of a syntax highlighting system, let's set up a basic HTML page where our JavaScript code will run.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Custom Syntax Highlighting</title>
    <style>
        .keyword { color: blue; }
        .string { color: green; }
        .comment { color: gray; font-style: italic; }
    </style>
</head>
<body>
    <pre><code id="code-block">
    // Sample JavaScript code
    function greet() {
        var msg = "Hello, World!";
        console.log(msg);
    }
    greet();
    </code></pre>
</body>
</html>

Parsing Code with JavaScript

For our syntax highlighter, we need to break down the string containing the code and apply styles to different parts of it using a simple text parsing approach. Let’s explore how to differentiate and style JavaScript keywords, strings, and comments.

JavaScript Code

document.addEventListener('DOMContentLoaded', function() {
    const codeBlock = document.getElementById('code-block');
    const codeText = codeBlock.textContent;

    // Token definitions
    const keywords = /\b(function|var|const|let|if|else|for|return|while)\b/g;
    const stringPattern = /(".*?"|'.*?')/g;
    const commentPattern = /\/\/.*$/gm;

    // Apply transformations
    let highlightedCode = codeText 
        .replace(commentPattern, '$&')
        .replace(stringPattern, '$&')
        .replace(keywords, '$&');

    codeBlock.innerHTML = highlightedCode;
});

In this script:

  • Regular expressions are used to match code components:
    • keywords captures reserved words like function and var.
    • stringPattern captures text within quotes (either single or double).
    • commentPattern identifies lines starting with //.
  • The replace method applies <span> tags around each identifiable structure, letting the CSS defined in the HTML file style these appropriately.

Final Notes

This simple syntax highlighter script provides a starting point for more complex parsing tasks, accommodating additional programming constructs for more comprehensive highlighting. It's worthwhile to note that parsing code using regex in languages that require more nuance can become complicated. Hence, for a fully-fledged highlighter supporting multiple programming languages, consider libraries like Prism.js or highlight.js, which have extensive support for various languages and themes.

Now you have a basic but functional custom syntax highlighter that can be expanded upon and customized further!

Next Article: Improving Text-Based Input Validation Logic Using JavaScript Strings

Previous Article: Transitioning Between Different Encodings Manually with JavaScript Strings

Series: JavaScript Strings

JavaScript

You May Also Like

  • Handle Zoom and Scroll with the Visual Viewport API in JavaScript
  • Improve Security Posture Using JavaScript Trusted Types
  • Allow Seamless Device Switching Using JavaScript Remote Playback
  • Update Content Proactively with the JavaScript Push API
  • Simplify Tooltip and Dropdown Creation via JavaScript Popover API
  • Improve User Experience Through Performance Metrics in JavaScript
  • Coordinate Workers Using Channel Messaging in JavaScript
  • Exchange Data Between Iframes Using Channel Messaging in JavaScript
  • Manipulating Time Zones in JavaScript Without Libraries
  • Solving Simple Algebraic Equations Using JavaScript Math Functions
  • Emulating Traditional OOP Constructs with JavaScript Classes
  • Smoothing Out User Flows: Focus Management Techniques in JavaScript
  • Creating Dynamic Timers and Counters with JavaScript
  • Implement Old-School Data Fetching Using JavaScript XMLHttpRequest
  • Load Dynamic Content Without Reloading via XMLHttpRequest in JavaScript
  • Manage Error Handling and Timeouts Using XMLHttpRequest in JavaScript
  • Handle XML and JSON Responses via JavaScript XMLHttpRequest
  • Make AJAX Requests with XMLHttpRequest in JavaScript
  • Customize Subtitle Styling Using JavaScript WebVTT Integration