Enhance your code's readability with the Code Beautifier Tool for improved clarity

I recently came across a website where they intricately formatted their code with line numbers and highlighting, but I couldn't find any information on how to achieve this effect. I want my code to look just like that, can anyone help me out?

Website Link:

Can someone assist me in achieving this polished code presentation?

<html> 
<head> </head> 
<body> 

<div> This is a div </div>
<p>This is a paragraph.</p>
<span>This is a span.</p>
<ul>
<li>red</li>
<li>green</li>
<li>blue</li>
</ul>
</body> 
</html> 

I am aiming for the same sleek code styling with line numbers seen in the example image linked above. Can you lend a hand with replicating it? My goal is to enhance the appearance of my tutorial by displaying code snippets in a user-friendly way, similar to what was achieved using the Syntax Highlighter tool on the mentioned site.

Answer №1

This site utilizes CodeMirror for code formatting. CodeMirror is an online editor that can be integrated into various projects. It also serves as a tool for showcasing code snippets.

I am currently working on developing a proof of concept for demonstration purposes. Once I have something satisfactory, I will share it here later on.

UPDATE: I have created a general proof of concept for reference. Please feel free to make use of it :)

// array containing all HTML tags sourced from MDN
var tagsArr = ["<\/?a", "<\/?abbr", "<\/?acronym", "<\/?address", "<\/?applet", "<\/?article", "<\/?aside", "... and many more"];

// sorting the tags by length to prevent shorter tags being overlapped
tagsArr.sort(function(a, b) {
  return a.length >= b.length
})

var tagsStr = tagsArr.join("|");

var tagsRegEx = new RegExp(tagsStr, "g");

// array of attributes extracted from W3Schools
var attrsArray = ["baccept", "accept-charset", "accesskey", "action", "align", "alt", "async", "... and more ..."];

attrsArray.sort(function(a, b) {
  return a.length >= b.length
})

var attrsStr = "(\\b" + attrsArray.join("\\b)(?!\")|(\\b") + "\\b)(?!\")";

var attrsRegEx = new RegExp(attrsStr, "g");

var pres = document.querySelectorAll("pre");

pres.forEach((code, idx) => {
  var formatted;
  code = code.innerText;
  formatted = code.replace(tagsRegEx, (match) => {
    if (match.match(/(span)|(form)|(label)|(media)/)) {
      match += "|";
    }
    return `<span| class="tag">${match.replace(/</g, "&lt;").replace(/>/g, "&gt;")}</span|>`;
  })
  formatted = formatted.replace(attrsRegEx, (match) => {
    return `<span| class="attr">${match}</span|>`;
  })
  formatted = formatted.replace(/=("(?!(attr)|(attr-value)|(tag)).*?")/g, (match, $1) => {
    return `=<span| class="attr-value">${$1}</span|>`;
  })
  formatted = formatted.replace(/\|/g, "");
  pres[idx].innerHTML = formatted;
})
body {
  background-color: white;
}

ol {
  border: 1px solid #e5e5e5;
  padding-left: 15px;
}

li:nth-child(even) {
  background-color: #e5e5e5;
}

... additional CSS styles ...

<ol>
  <li>
    <pre>&lt;form&gt;</pre>
  </li>
  
  ... sample HTML code snippets ...
  
</ol>

Answer №2

For those looking to enhance their coding experience, consider downloading Sublime Text 3 or Atom and then installing the Html-css-prettify plugin in Sublime Text 3. It's a great tool that can greatly assist you in your work.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Is it possible to modify the page contents using the htaccess file?

Is there a way to manipulate the content of a page using the htaccess file? Perhaps something similar to a redirect, but instead of directing the user to a new page, it would alter the existing page's content? ...

SystemJS is loading classes that are extending others

In my Angular2 application, I have two classes where one extends the other. The first class is defined in the file course.ts (loaded as js) export class Course { id:string; } The second class is in schoolCourse.ts (also loaded as js) import {Cours ...

Using react hooks, I am refreshing the product image by replacing it with the thumbnail image

I'm currently working on an e-commerce platform that resembles Amazon. In the product detail page, I want the right side image to update when I click on a thumbnail image on the left side. The issue I'm facing is that upon first loading, the def ...

What's the best way to dynamically show Bootstrap collapse panels in a loop with AngularJS ng-repeat?

Currently, I am utilizing angularJS and attempting to include a bootstrap collapsible-panel within a loop. The code that I have written is causing all the panel bodies to be displayed beneath the first panel header. I need each body to be shown below i ...

Issue with the _.filter function in lodash library when used in a Node.js environment

My goal is to remove rows from a CSV file that already contain email addresses found in Mailchimp. Although I attempted to achieve this with a function, it seems that the number of elements returned is not accurate: async function testDeleteEmails(listID, ...

Struggling to access YouTube account via Google sign-in using Puppeteer framework

I am facing an issue with my puppeteer code where I am unable to proceed past the email page after clicking next due to some bot protection by Google advising me to "Try using a different browser...etc". Is there a way to bypass this using puppeteer? I h ...

Tips on incorporating an item into an array solely when a specific condition is met

Consider the following arrow function that creates an array: const myFunction = () => ["a", "b", "c"]; I am looking to modify this function to add another element if a specific argument is true. For example, I want it to look like this: const myFunc ...

I'm having trouble getting the gutter padding to show up in one specific content block on my Bootstrap webpage. How can I troubleshoot and resolve this

Currently, I am delving into the realm of Bootstrap 5 and have recently put together a simplistic webpage using the row/column structure suggested by Bootstrap. However, while my other columns seem to be cooperating and displaying the gutter padding as exp ...

Integrate SVG directly into the three.js module to eliminate the need for an additional HTTP request

Currently, I am developing a small website that features a simple 3D animation. The model is extruded from an SVG file loaded using SVGLoader. My goal is to enhance the loading speed by including the SVG as text in my threejs code, eliminating the need for ...

Generating real-time post previews in a web application using .NET MVC 3, similar to the functionality seen on StackOverflow

Looking to create a real-time preview of a form submission, like the one on Stack Overflow. Currently working with .NET MVC 3. Does anyone have suggestions or guidance on how to achieve this? Thank you. - Yohan ...

Show the button only if the checkbox is selected

Can someone help me figure out how to show a button on the header bar once the checkbox is selected in a listview? Any assistance would be greatly appreciated. I have tried implementing this on my Codepen: `http://codepen.io/Hin/pen/KpGJZX` ...

Tips for determining whether the current request is an AJAX request in MVC3 view using JavaScript

I have a div with a maximum height and overflow set to auto. When the size of the div overflows, a scroll bar is automatically added. However, I have also set $("#itemsdiv").scrollTop(10000); so that the scroll bar is always at the bottom. Now, I want t ...

Error occurred within node while attempting to create a directory using mkdirsync

I wrote some code. try{ var Storage = multer.diskStorage({ destination: function (req, file, callback) { fs.mkdirSync('/home/data/' + file.originalname, { recursive: true }, (error) => { ...

When utilizing the multipart/form-data content type in a form, the files being sent are transmitted as blank

I am currently developing a VueJS component that functions as a form to collect user information and enable multiple image uploads. The form is set with enctype="multipart/form-data" and exhibits the following structure : <template> <div> ...

Utilizing Angular to intercept AJAX requests, verifying internet connectivity before proceeding with sending the request

In my Angular (with Ionic) app, I have this code snippet: my_app.factory('connectivityInterceptorService', ['$q', '$rootScope', function ($q, $rootScope) { var connectivityInterceptorServiceFactory = {}; var _request ...

Utilizing AJAX for remote execution of JavaScript code

Suppose there is a JavaScript page located at myaddress/service.js on a server. The code in this .js file looks something like: nsBob = { a: function(someParam) {...perform actions and return result}, b: function() {...perform actions and return result} ...

Sending intricate JavaScript object to the controller. The array of objects is consistently empty

I am facing an issue with passing an object to my C# controller. While all the properties are getting populated correctly, the list I have always ends up with a count of 0. I've tried setting the header to content-type/json and using Json.stringify. F ...

Navigate the orbit control camera in a circular motion around the target object

Just starting out in the world of threejs. I've managed to create a cube with different colors on each face. This cube can be rotated using OrbitControl, and I have 6 buttons in Dat.GUI that control the camera position. My goal is to click "Animate t ...

ASP.NET failing to execute Javascript

Can you take a look at this code and help me figure out why the alert is not working on the webpage? The console.WriteLine statement below it is running fine, but the alert isn't appearing. private void PublishLoop() { while (Running ...

Sending a POST request from a React child component to an Express server

I recently started working on a project using the MERN stack, incorporating react-router and redux. Within my application, I have integrated a <Navbar /> component along with a <SearchBar> component. To manage the react side of things, I utili ...