diff --git a/src/code/colorful.js b/src/code/colorful.js index 4f671ec..e07806d 100644 --- a/src/code/colorful.js +++ b/src/code/colorful.js @@ -9,7 +9,12 @@ const TAG_START_EXP = /<([\w\-]+)([\w\W]*?)>/g const TAG_END_EXP = /<\/([\w\-]+)>/g const TAG_ATTR_EXP = /[@a-zA-Z\-.]+=(["'])[^"]+\1|[@a-zA-Z\-.]+=[a-zA-Z0-9]+|[@a-zA-Z\-.]+/g const TAG_CM_EXP = //g -// const TAGS = 'a,b,i' +const KEYWOWRD1 = /\b(var|const|let|function|for|switch|with|if|else|export|import|async|await|break|continue|return|class|try|catch|throw|new|while|this|super|default|case|debugger|delete|do|goto|in|public|private|protected|package|typeof)\b/g +const KEYWOWRD2 = /\b\s(=|-|\+|\/|\*|<|>|%)\s\b/g +const KEYWOWRD3 = /(\+\=|-=|\/=|\*=|--|\+\+|==|===)/g +const STR = /(['"`])(.*?)\1/g +const NUM = /\b(\d+)\b/g +const FN = /([\.\s])([a-zA-Z$][\da-zA-Z_]*)(\(.*?\))/g export function colorHtml(code) { code = code @@ -54,3 +59,19 @@ export function colorCss(code) { .replace(/&/g, '&') return code } + +export function colorJs(code) { + code = code + .replace(FN, '$1[fn]$2[/fn]$3') + .replace(KEYWOWRD1, '[key]$1[/key]') + .replace(KEYWOWRD2, '[key] $1 [/key]') + .replace(KEYWOWRD3, '[key]$1[/key]') + .replace(STR, '[str]$1$2$1[/str]') + .replace(NUM, '[num]$1[/num]') + + return code + .replace(/\[(\/?)key\]/g, (m, s) => (s ? '' : '')) + .replace(/\[(\/?)str\]/g, (m, s) => (s ? '' : '')) + .replace(/\[(\/?)num\]/g, (m, s) => (s ? '' : '')) + .replace(/\[(\/?)fn\]/g, (m, s) => (s ? '' : '')) +} diff --git a/src/code/index.wc b/src/code/index.wc index 4c28ce1..0977a75 100644 --- a/src/code/index.wc +++ b/src/code/index.wc @@ -15,10 +15,10 @@