To apply syntax highlighting to your CSS code and make it look like the VSCode Dark+ theme, you'll need to modify your highlightCSS function to wrap different parts of your CSS code with <span> elements and assign them specific classes. Then, you can style these classes using CSS to match the VSCode Dark+ theme colors.
Here's how you can do it:
1. Modify the highlightCSS Function
Update your highlightCSS function to:
- Use regular expressions to find different parts of the CSS code (selectors, properties, values, braces, comments).
- Wrap these parts with
<span> elements and assign appropriate class names.
- Replace the original text content with the new highlighted HTML.
function highlightCSS(element) {
const code = element.textContent;
// Regular expression to match CSS syntax elements
const regex = /\/\*[\s\S]*?\*\/|("[^"]*"|'[^']*')|(\.\w+|\#\w+|\w+(\s*\w+)*)(?=\s*\{)|([\w-]+)(?=\s*:)|(:)(?=\s*[^;]+;)|({|})|([^{}\s]+)(?=;)/g;
const highlighted = code.replace(regex, (match, comment, string, selector, propName, colon, brace, value) => {
if (comment) {
return `<span class="comment">${comment}</span>`;
}
if (string) {
return `<span class="string">${string}</span>`;
}
if (selector) {
return `<span class="selector">${selector}</span>`;
}
if (propName) {
return `<span class="property">${propName}</span>`;
}
if (colon) {
return `<span class="colon">${colon}</span>`;
}
if (brace) {
return `<span class="brace">${brace}</span>`;
}
if (value) {
return `<span class="value">${value}</span>`;
}
return match;
});
// Replace the content of the element with the highlighted HTML
element.innerHTML = highlighted;
}
// Find the <pre> element containing your CSS code
const element = document.querySelector('pre');
// Apply syntax highlighting
highlightCSS(element);
2. Apply CSS Styles to Match VSCode Dark+ Theme
Now, define the CSS styles for the classes used in the function to match the VSCode Dark+ theme:
/* VSCode Dark+ Theme Colors */
.selector {
color: #d7ba7d; /* Light brown */
}
.property {
color: #9cdcfe; /* Light blue */
}
.value {
color: #ce9178; /* Light orange */
}
.brace {
color: #d4d4d4; /* Light gray */
}
.comment {
color: #6a9955; /* Green */
}
.string {
color: #ce9178; /* Light orange */
}
.colon {
color: #d4d4d4; /* Light gray */
}
/* Optional: Style the <pre> element to look like code in VSCode */
pre {
background-color: #1e1e1e; /* Dark background */
color: #d4d4d4; /* Default text color */
padding: 16px;
border-radius: 4px;
overflow: auto;
font-family: 'Consolas', 'Courier New', monospace;
line-height: 1.5;
}
3. Example HTML Structure
Make sure your HTML structure is set up correctly:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>CSS Highlighter</title>
<!-- Include the CSS styles -->
<style>
/* Paste the CSS styles from step 2 here */
</style>
</head>
<body>
<pre>
/* Example CSS Code */
.container {
display: flex;
justify-content: center;
align-items: center;
}
.button:hover {
background-color: #ff0000;
}
</pre>
<!-- Include the script -->
<script>
// Paste the JavaScript code from step 1 here
</script>
</body>
</html>
Final Result
With the above setup, your CSS code inside the <pre> element should now be syntax-highlighted to resemble the VSCode Dark+ theme.
Additional Tips
- Edge Cases: The regular expression provided is a basic one and might not cover all edge cases of CSS syntax. You might need to adjust it to handle more complex CSS features.
- Using Libraries: If you find that handling all the syntax variations is too complex, consider using a library like Prism.js or Highlight.js, which have built-in support for syntax highlighting and themes that mimic VSCode's appearance.
Using Prism.js as an Alternative
If you'd like a more robust solution without reinventing the wheel, you can use Prism.js with the VSCode Dark+ theme.
Steps:
-
Include Prism.js and the theme CSS
<link href="https://cdnjs.cloudflare.com/ajax/libs/prism/1.29.0/themes/prism-vsc-dark-plus.min.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.29.0/prism.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.29.0/components/prism-css.min.js"></script>
-
Use the Appropriate Markup
<pre><code class="language-css">
/* Example CSS Code */
.container {
display: flex;
justify-content: center;
align-items: center;
}
.button:hover {
background-color: #ff0000;
}
</code></pre>
-
Prism.js will Automatically Highlight the Code
No additional JavaScript is needed. Prism.js will parse the code inside <code> tags with the class language-css and apply syntax highlighting.
By following these steps, you should be able to apply syntax highlighting to your CSS code and have it look like the VSCode Dark+ theme.
Feel free to ask if you need further assistance or have any questions!