当前位置: 首页 > news >正文

自动生成链接

最近做项目遇到了一个需求就是需要根据后台传的关键字和地址自动去生成链接,但是我的项目是服务器端渲染的,所有页面内容都是不固定的,等于是后台传什么数据我就根据他传的数据去决定渲染什么组件啊,那这根据不可能去依赖v-html,而且v-html本身就有很多的弊端啊,所以我在查看了很多自动生成链接的组件之后,决定自己封装一个生成链接的组件,下面是我的组件的内容啊

<template><span><template v-for="(part, index) in parts" :key="index"><NuxtLinkv-if="keywordLinks[part]":to="keywordLinks[part]"class="text-[var(--text-link-color-primary)] cursor-pointer"target="_blank">{{ part }}</NuxtLink><span v-else>{{ part }}</span></template></span>
</template><script setup>
const props = defineProps({text: String,keywordLinks: {type: Object,default: () => ({})}
})// 存储所有匹配:{ text: keyword, link, index, length }
const matches = []// 对每个关键词构造带单词边界的正则,查找第一次完整词匹配
for (const [keyword, link] of Object.entries(props.keywordLinks)) {// 转义关键词中的特殊字符(防止如 '.'、'+' 等破坏正则)const escapedKeyword = keyword.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')// 使用单词边界 \b,确保是完整词匹配const regex = new RegExp(`\\b${escapedKeyword}\\b`, 'i') // i 表示不区分大小写(可选)const match = regex.exec(props.text)if (match) {matches.push({text: keyword, // 原样保留关键词用于后续比对link: link,index: match.index,length: match[0].length})}
}// 按照在原文中出现的位置排序(保证顺序正确)
matches.sort((a, b) => a.index - b.index)// 构建 parts:按原始顺序切分文本
const parts = []
let lastIndex = 0matches.forEach(match => {// 添加匹配前的普通文本if (match.index > lastIndex) {parts.push(props.text.slice(lastIndex, match.index))}// 添加关键词本身(后续模板中会判断是否为链接)parts.push(match.text)lastIndex = match.index + match.length
})// 添加最后剩余的文本
if (lastIndex < props.text?.length) {parts.push(props.text.slice(lastIndex))
}
</script>
<style scoped></style>

这个组件呢会根据接受的关键词和文字去匹配,如果匹配到了就给他提出来,方便渲染的时候判断是否生成链接,他这个匹配的规则是把关键词当做一个整体去全部匹配,如果遇到了多个匹配结果支取第一个,并且英文单词不能拆开匹配,下面是一个使用的例子:

//这里只展示关键部分的代码,看得懂就行<AutoLinkText :text="text" :keywordLinks="keywordLinks"></AutoLinkText>const keywordLinks = {"perfectly responds": "https://www.baidu.com","Mobile": "https://www.baidu.com","a": "https://www.baidu.com"
}const text = "Mobile conveyor belt bridges do precisely what their name suggests. These portable conveyor systems flexibly bridge the gap between the excavation site and the bench conveyors. Their modular design makes them indispensable on-site; they increase the miners’ operating range and connect bench conveyors of different heights."

下面就是效果,蓝色部分就是添加了链接的部分

有类似需求的小伙伴可以参考一下


文章转载自:

http://GEj2Cxso.wmrgp.cn
http://1fJnGFwA.wmrgp.cn
http://jkGuI5Cz.wmrgp.cn
http://wQYp9UNB.wmrgp.cn
http://b8YNM396.wmrgp.cn
http://7qKZWMoP.wmrgp.cn
http://Tkzyb8r2.wmrgp.cn
http://WX6gmjTP.wmrgp.cn
http://rYcPqYX9.wmrgp.cn
http://3qNVLFHu.wmrgp.cn
http://N28y76F7.wmrgp.cn
http://H2NuM3Il.wmrgp.cn
http://1SrH9O97.wmrgp.cn
http://UIKPWqnT.wmrgp.cn
http://HIdIpHW2.wmrgp.cn
http://ZEvw5kBK.wmrgp.cn
http://MbHYUKNN.wmrgp.cn
http://SDy3MQ8g.wmrgp.cn
http://QguGxpJP.wmrgp.cn
http://mkUn6FZ5.wmrgp.cn
http://FN0A5pAm.wmrgp.cn
http://oepIXhMK.wmrgp.cn
http://E6hAEnD6.wmrgp.cn
http://TFXMPVph.wmrgp.cn
http://BGjTxQHB.wmrgp.cn
http://Kg3SuWqt.wmrgp.cn
http://d7V7LVTC.wmrgp.cn
http://ij6e46Bp.wmrgp.cn
http://xxM0dKIy.wmrgp.cn
http://Pw0KAb1g.wmrgp.cn
http://www.dtcms.com/a/380658.html

相关文章:

  • LeetCode 3258.统计满足K约束的子字符串数量 I
  • “量子能量泵”:一种基于并联电池与电容阵的动态直接升压架构
  • fastapi 使用本地资源自定义swagger文档
  • Vue FullPage.js 完整使用指南:Vue 3 官方全屏滚动解决方案
  • ARM IRQ中断
  • Ruoyi-vue-plus-5.x第八篇文件管理与存储: 8.2 OSS云存储集成
  • 解决:NVIDIA-SMI couldn‘t find libnvidia-ml.so library in your system.
  • 【LLM】VLLM:容器运行 ModelScope 模型
  • HarmonyOS 应用开发深度解析:基于 Stage 模型与 ArkUI 的跨组件状态共享最佳实践
  • TOGAF——战术性调整,战略性变更
  • 【计算机 UTF-8 转换为本地编码的含义】
  • 当人工智能遇上知识检索:RAG技术的深度解析与实践探索
  • 在线商城管理系统功能清单的系统设计
  • SLAM 系统设计是如何保证前端(tracking/VO)和后端(优化/BA/图优化)如何同步实时性思路汇总思考
  • 代码随想录二刷之“动态规划”~GO
  • zynq arm全局计时器和私有定时器
  • TCP套接字的使用
  • 红日靶场(三)——个人笔记
  • Linux 进程和线程基础知识解析
  • MySQL 查询不正确身份证号的方法
  • 淘宝商品详情 API 的安全强化与生态协同创新路径
  • 全志A133 android10 secure boot 安全启动
  • 储能电站的监控运维软件推荐,降低运营成本
  • 麒麟v10系统内存不足
  • fpga图像处理
  • 使用netstat 获取各Oracle数据库实例对应应用IP地址脚本
  • QT M/V架构开发实战:QAbstractItemModel介绍
  • PHP 与 WebAssembly 的 “天然隔阂”
  • QML 的第一步
  • IP验证学习之env集成编写