From 59076f2c5486607f0b0d4382e9dc5811f97cbb41 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AE=87=E5=A4=A9?= Date: Sun, 25 Apr 2021 19:15:53 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E4=BB=A3=E7=A0=81=E5=9D=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/code/colorful.js | 23 ++++++++++++++++++++++- src/code/index.wc | 29 +++++++++++++++++++++-------- src/form/button.wc | 5 ++--- src/form/checkbox.wc | 25 +++++++++++++++++++------ src/markd/core.js | 1 + 5 files changed, 65 insertions(+), 18 deletions(-) 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 @@