@brianmcallister/highlight-text

中文文档
2.0.4发布于 2020/12/25MITCommonJS零依赖近 30 天下载 1.2万

Hightlight words in text

原始使用文档
文档目录6 节
  1. Table of contents
  2. Demo
  3. Installation
  4. Usage
  5. API
  6. highlightText

@brianmcallister/highlight-text

codecov CircleCI npm version

Highlight words in some text

highlight-text makes highlighting words and characters in a string quick and easy. This library is primarily used for adding a visual indicator to any text in a web page, much like the default search feature in your browser.

This is a great way to highlight search results collected from an input field within your application, or a range of possible use cases:

  • User configured search terms in a chat application
  • Administrator configured announcements
  • Visual design elements

By default, the library will accept a string and some sub strings to search for. It will return all of the found strings surrounded by <mark> tags, which can then be styled to match your branding. This is configurable, in case you want to use any arbitrary string or HTML tag.

Table of contents

Demo

Hosted demo: https://highlight-text.netlify.com/

You can also run the demo locally. To get started:

git clone [email protected]:brianmcallister/highlight-text.git
cd highlight-text/demo
npm i
npm start
⇡ Top

Installation

npm install @brianmcallister/highlight-text
⇡ Top

Usage

import highlightText from '@brianmcallister/highlight-text';

highlightText('some text', ['om', 'ex']);
// #=> 's<mark>om</mark>e t<mark>ex</mark>t'
⇡ Top

API

highlightText

This is the default export. Use this function to highlight words in text.

highlightText(text: string, words: string[]): string;
⇡ Top

版本与发布记录

@brianmcallister/highlight-text 共发布 13 个版本,首次发布于 2016/12/20;过去 12 个月发布了 0 个版本。

版本发布日期距今
2.0.4latest2020/12/255 年前
2.0.32020/04/056 年前
2.0.22020/04/056 年前
2.0.12019/11/176 年前
2.0.02019/11/106 年前
2.0.0-beta.3next2019/11/106 年前

关于 @brianmcallister/highlight-text 的常见问题

@brianmcallister/highlight-text 是什么?

Hightlight words in text

如何安装 @brianmcallister/highlight-text?

在项目目录运行 npm install @brianmcallister/highlight-text,或使用 pnpm add / yarn add / bun add。

@brianmcallister/highlight-text 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/brianmcallister__highlight-text 是否存在。

@brianmcallister/highlight-text 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@brianmcallister/highlight-text 可以商用吗?

@brianmcallister/highlight-text 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@brianmcallister/highlight-text 的下载量有多少?

根据 npm 官方下载统计,@brianmcallister/highlight-text 在 2026/10/09 – 2026/11/07 这 30 天内被下载 11,596 次,日均约 387 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@brianmcallister/highlight-text 还在维护吗?

最新版本 2.0.4 发布于 2020/12/25(5 年前);至今共发布 13 个版本,其中过去 12 个月发布了 0 个;首次发布于 2016/12/20。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。