打造个性化代码高亮组件

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

效果

预览

预览地址: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

目录
相关文章
|
缓存 安全 Java
|
数据库连接 数据库
kettle开发篇-流查询
kettle开发篇-流查询
486 0
|
机器学习/深度学习 人工智能 自然语言处理
通义灵码:新体验与最佳实践
随着AI技术的发展,阿里巴巴推出的通义灵码作为先进的大模型,提升了自然语言处理能力,并在智能对话、多语言支持及自定义训练等方面带来全新体验。文章介绍了通义灵码在客服系统、智能写作及教育领域的应用案例,展示了其提升效率和创造价值的能力。
539 1
|
7月前
|
人工智能 物联网 数据处理
“一丹一世界”三等奖 | 曙光_麦橘超然 创作分享
“一丹一世界”三等奖 | 曙光_麦橘超然 创作分享
178 0
|
iOS开发 MacOS Windows
electron-updater实现electron全量版本更新
electron-updater实现electron全量版本更新
2184 9
electron-updater实现electron全量版本更新
|
安全 Java 编译器
springboot 整合表达式计算引擎 Aviator 使用示例详解
本文详细介绍了Google Aviator 这款高性能、轻量级的 Java 表达式求值引擎
1653 6
|
安全 量子技术 芯片
光量子计算:实现通用量子计算机的路径
【10月更文挑战第3天】光量子计算作为量子计算的重要技术路径,利用光学原理处理量子信息,以光子作为量子比特,通过操控光子状态实现高效量子运算。其具备高速、高精度、易集成及低环境要求等优势,是实现通用量子计算机的关键。目前全球量子计算正处于快速发展阶段,中美等国在该领域取得显著进展,光量子计算有望在量子通信、量子化学等多个领域发挥重要作用,推动科技进步。
|
存储 安全 数据安全/隐私保护
CRISPR技术:基因编辑的伦理与应用
【9月更文挑战第27天】CRISPR/Cas9基因编辑技术以其高精度和效率革新了生命科学,应用于基因功能研究、遗传病治疗及作物改良,同时引发伦理争议。本文探讨其原理、人类胚胎编辑的伦理议题、个人基因隐私保护及生态影响,并展望其在医学、农业和科研领域的潜力。
|
JSON 前端开发 JavaScript
socket.io即时通信前端配合Node案例
本文介绍了如何使用socket.io库在Node.js环境下实现一个简单的即时通信前端配合案例,包括了服务端和客户端的代码实现,以及如何通过socket.io进行事件的发送和监听来实现实时通信。
304 2
|
边缘计算 开发框架 人工智能
C#/.NET/.NET Core优秀项目和框架2024年8月简报
C#/.NET/.NET Core优秀项目和框架2024年8月简报
231 0