打造个性化代码高亮组件

简介: 打造个性化代码高亮组件

效果

预览

预览地址:http://120.79.163.94//JYeontuComponents/#/codeHeightLightView

关键代码实现

主要是通过正则表达式来进行匹配替换。

找出嵌套标签
findTag(text) {
    let l = [],
        r = [];
    let res = [];
    for (let i = 0; i < text.length; i++) {
        if (text[i] == "<") {
            if (text[i + 1] == "!") continue;
            if (text[i + 1] == "/") {
                let j = i + 2;
                while (text[j] != ">") {
                    j++;
                }
                res.push(text.slice(l.pop(), j + 1));
            } else {
                l.push(i);
            }
        }
    }
    return res;
}
标签名高亮
replaceDfs(tagReg, textCode) {
    let res = textCode.replace(
        tagReg,
        (s1, s2, s3, s4, s5, s6, s7, s8, s9, s10) => {
            let res = "";
            if (s4 == undefined) return "<span>" + s1 + "</span>";
            res += "<span><<span>" + this.getColor("tagWord", s4) + " ";
            let text = s6.match(/>(.*)</);
            if (text && text.length > 1) {
                text = text[1];
            } else {
                text = "";
            }
            s6 = s6
                .split(/>.*<\//)[0]
                .replace(/ *= */g, "=")
                .split(" ");
            for (let i = 0; i < s6.length; i++) {
                if (s6[i] !== "") {
                    let t = s6[i].split("=");
                    if (t.length == 2) {
                        res += this.getColor("attrWord", t[0]);
                        res += " = ";
                        res += this.getColor("attrValue", t[1]);
                        if (i < s6.length - 1) res += " ";
                    } else {
                        t = s6[i].split(":");
                        if (t.length == 2) {
                            res += this.getColor("attrWord", t[0]);
                            res += ":";
                            res += this.getColor("attrValue", t[1]);
                            if (i < s6.length - 1) res += " ";
                        }
                    }
                }
            }
            res +=
                "<span>>" +
                text +
                "<</span>/" +
                this.getColor("tagWord", s4) +
                "<span>></span>";
            return res;
        }
    );
    return res;
}
关键字高亮
//字符串
let regStr = "('|\")(.*)('|\")";
showCode = showCode.replace(
    new RegExp(regStr, "g"),
    "<span style='color : " + colors.strWord + "'>$1$2$3</span>"
);
//js关键字
let keyWord = [...this.jsKeyWord];
keyWord = keyWord.concat([...this.jsKeyObj]);
for (let i = 0; i < keyWord.length; i++) {
    let regKeyWord =
        "((([\\t|\\r|\\n| ])*(" + keyWord[i] + "))( |,|\\.|\\(|;))";
    showCode = showCode.replace(
        new RegExp(regKeyWord, "g"),
        "<span style='color : " + colors.keyWord + "'>$2</span>$5"
    );
}
//自定义关键字
let keyWords = [...this.keyWords];
for (let i = 0; i < keyWords.length; i++) {
    let regKeyWord = "(" + keyWords[i].value + ")";
    showCode = showCode.replace(
        new RegExp(regKeyWord, "g"),
        "<span style='color : " +
            keyWords[i].color +
            " !important;'>$1</span>"
    );
}
//js方法
let functions = /([a-zA-Z0-9_]+)\([A-Za-z,0-9]*\)/g;
let functionKeyWord = showCode.match(functions) || [];
functionKeyWord = functionKeyWord
    .map(item => {
        return item.slice(0, item.indexOf("("));
    })
    .sort((a, b) => {
        return b.length - a.length;
    });
for (let i = 0; i < functionKeyWord.length; i++) {
    let regFunctionKeyWord = "(" + functionKeyWord[i] + ")";
    showCode = showCode.replace(
        new RegExp(regFunctionKeyWord, "g"),
        "<span style='color : " +
            colors.functionkeyWord +
            "'>$1</span>"
    );
}
//内置方法
let methodKeyWord = [...methodKeyWord];
for (let i = 0; i < methodKeyWord.length; i++) {
    let regMethodKeyWord = "(" + methodKeyWord[i] + ")";
    showCode = showCode.replace(
        new RegExp(regMethodKeyWord, "g"),
        "<span style='color : " +
            colors.methodkeyWord +
            "'>$1</span>"
    );
}
//变量名
let varReg = /([a-zA-Z]+):/g;
showCode = showCode.replace(
    varReg,
    "<span style='color : " + colors.varWord + "'>$1</span>:"
);
//注释
showCode = showCode.replace(
    /(\/\/.*)|(\/\*.*([\r\n].*)*\*\/)/g,
    "<span style='color :" + colors.note + "'>$1$2</span>"
);

npm引入

组件以上传到npm,可以直接install使用,0.1.4以上版本加入了该组件。

1.安装
npm i @jyeontu/jvuewheel -S
2.引入
//引入组件库
import jvuewheel from '@jyeontu/jvuewheel'
//引入样式
import '@jyeontu/jvuewheel/lib/jvuewhell.css'
Vue.use(jvuewheel);
3.vue中引用组件
<j-code-height-light :code = "code" 
        :keyWords = "keyWords" 
        :color = "color">
</j-code-height-light>

资源地址

组件使用文档

文档地址:http://120.79.163.94/JYeontuComponents/#/homePage

源码地址

目前该组件还存在一些未知bug,有兴趣的可以一起来改造。

Gitee地址:https://gitee.com/zheng_yongtao/jyeontu-component-warehouse/tree/master/JYeontuComponentWarehouse

目录
相关文章
|
弹性计算 负载均衡 网络协议
负载均衡(SLB)使用最佳实践
负载均衡(Server Load Balancer,下文简称 SLB)的引入,可以降低单台云服务器 ECS(下文简称 ECS)出现异常时对业务的冲击,提升业务的可用性。同时,结合弹性伸缩服务,通过动态调整后端服务器,可以快速对业务进行弹性调整(扩容或缩容),以快速应对业务的发展。
15655 0
|
前端开发 Java 开发者
深入理解Spring Boot中的@Service注解
【4月更文挑战第22天】在 Spring Boot 应用开发中,@Service 注解扮演着特定的角色,主要用于标识服务层组件。本篇技术博客将全面探讨 @Service 注解的概念,并提供实际的应用示例,帮助开发者理解如何有效地使用这一注解来优化应用的服务层架构
3828 1
|
运维 监控 Oracle
Java小史:JDK现状
简单说一下现在主流的JDK
4925 0
Java小史:JDK现状
|
JSON 网络协议 安全
阿里云公共DNS安全传输服务介绍(DoH/DoT)
传统的DNS查询和应答采用大都采用UDP明文传输,存在网络监听、DNS劫持、中间设备干扰的风险。为了应对以上挑战,阿里公共DNS对外提供支持DoH/DoT的云服务,为广大的互联网用户提供快速、稳定和安全的DNS解析,欢迎大家使用!
阿里云公共DNS安全传输服务介绍(DoH/DoT)
|
NoSQL Redis Docker
使用Docker Compose工具进行容器编排的教程
以上就是使用Docker Compose进行容器编排的基础操作。这能帮你更有效地在本地或者在服务器上部署和管理多容器应用。
1035 11
|
Linux 虚拟化 iOS开发
VMware Workstation 17.6.4 发布 - 领先的免费桌面虚拟化软件
VMware Workstation 17.6.4 Pro for Windows & Linux - 领先的免费桌面虚拟化软件
9202 3
|
Java 应用服务中间件 Linux
Tomcat运行日志字符错乱/项目启动时控制台日志乱码问题
总结: 通过以上几种方法,概括如下:指定编码格式、设置JVM的文件编码、修改控制台输出编码、修正JSP页面编码和设置过滤器。遵循这些步骤,你可以依次排查和解决Tomcat运行日志字符错乱及项目启动时控制台日志乱码问题。希望这些建议能对你的问题提供有效的解决方案。
2552 16
|
机器学习/深度学习 资源调度 算法
杜教筛
【6月更文挑战第9天】
331 2
在Vue3项目中使用 vue3-seamless-scroll 无缝滚动插件
本文介绍了如何在Vue3项目中使用`vue3-seamless-scroll`插件实现无缝滚动效果,并提供了详细的示例代码和运行效果。
9280 0
|
机器学习/深度学习 存储 C++
[蓝桥杯] 树状数组与线段树问题(C/C++)
[蓝桥杯] 树状数组与线段树问题(C/C++)
442 0