Skip to content

Commit

Permalink
feat: support jsx syntax highlight
Browse files Browse the repository at this point in the history
  • Loading branch information
jmjuanes committed Sep 26, 2023
1 parent 7ac2091 commit 64b0161
Showing 1 changed file with 22 additions and 2 deletions.
24 changes: 22 additions & 2 deletions codecake.js
Original file line number Diff line number Diff line change
Expand Up @@ -242,14 +242,35 @@ const languages = {
],
},
javascript: {
aliases: ["js"],
aliases: ["js", "jsx"],
rules: [
{regex: /^(\/\*(?:.|\s)*?\*\/|\/\/(?:.)*)/, token: "comment"},
{regex: /^(\'(?:.)*?\')|^(\"(?:.)*?\")|^(\`(?:.|\s)*?\`)/, token: "string"},
{regex: new RegExp(`^\\b(${jsKeywords.join("|")})\\b`), token: "keyword"},
{regex: /^\b(true|false|null)\b/, token: "constant"},
{regex: /^([+-]?([0-9]*[.])?[0-9]+)/, token: "number"},
{regex: /^([{}[\](\):;\\.,])/, token: "punctuation"},
{
regex: /^(<(?:=>|[^>])+(?:\/)?>)/,
rules: [
{
regex: /^(<\/?[\w]+)/,
rules: [
{regex: /^(<)/, token: "punctuation"},
{regex: /^([\w]+)/, token: "tag"},
],
},
{
regex: /^([\w\.\-\_]+=(?:"[^"]*"|\{[^\}]*}))/,
rules: [
{regex: /^([\w\.\-\_]+)/, token: "attr"},
{regex: /^(=)/, token: "punctuation"},
{regex: /^("(?:.)*?"|\{(?:.)*?})/, token: "string"},
],
},
{regex: /^(>)/, token: "punctuation"},
],
},
{regex: /^([?!&@~\/\-+*%=<>|])/, token: "operator"},
{
regex: /^([a-zA-Z][\w]*\s*\()/,
Expand Down Expand Up @@ -339,7 +360,6 @@ const _highlight = (code, rules) => {
return text;
};

// Highlight the provided string
export const highlight = (code, language = "javascript") => {
return _highlight(code, languages[language]?.rules || []);
};

0 comments on commit 64b0161

Please sign in to comment.