# across-nodes-search-keywords

> 跨节点/元素，搜索关键词

Latest version **1.0.2** (published 2022-03-10) · MIT license · 0 weekly downloads

## Install

```sh
npm install across-nodes-search-keywords
pnpm add across-nodes-search-keywords
yarn add across-nodes-search-keywords
bun add across-nodes-search-keywords
```

## Health

**Score 20/100 (F)** — status: abandoned.

Positive: esm support; no vulnerabilities.

Warnings: low downloads; no types.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.2 |
| Published | 2022-03-10 |
| First published | 2022-03-10 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | ESM + CommonJS |
| Dependencies | 2 |
| Unpacked size | 104.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | q-jason |
| Maintainers | q-jason |

## Links

- npm: https://www.npmjs.com/package/across-nodes-search-keywords
- Repository: https://github.com/q-jason/across-nodes-search-keywords
- Issues: https://github.com/q-jason/across-nodes-search-keywords/issues
- npm.io page: https://npm.io/package/across-nodes-search-keywords

## Dependencies (2)

- [@babel/runtime](https://npm.io/package/@babel/runtime.md) ^7.17.2
- [@babel/runtime-corejs3](https://npm.io/package/@babel/runtime-corejs3.md) ^7.17.2

## Recent versions

- 1.0.2 (latest) — 2022-03-10
- 1.0.1 — 2022-03-10
- 1.0.0 — 2022-03-10

## README

## across-nodes-search-keywords
> 跨节点/元素，搜索关键词


### Demo
> 请打开控制台

https://q-jason.github.io/across-nodes-search-keywords/docs/

### 安装
```bash
npm i --save across-nodes-search-keywords
```

or

```html
<script src="./dist/across-nodes-search-keywords.js"></script>
```

### 使用
```js
var result = window.acrossNodesSearchKeywords({
  el: document.querySelector('body'),
  keywords: 'any text'
})

/**
 result 解析
 {
    // el 元素下的所有文本内容
    "fullText": "巴拉巴拉",
    // 搜索的关键词
    "keywords": "巴拉巴拉",
    // 匹配到的结果
    "matchResults": [
        // 代表一条匹配到的关键词数据
        {
            // 关键词出现的开始位置
            "startIndex": 266,
            // 关键词出现的结束位置
            "endIndex": 601,
            // 已经被 splitText 处理后的 nodeText 节点
            "textNodes": [
                {},
                {}
            ]
        }
    ]
}
 **/
```

### 注意
> 方法本身仅用 splitText 方法切分 nodeText 节点，并返回响应数据，除此之外不做任何其他处理 <br/>
> 需要高亮等逻辑需手动处理，如下：
```javascript
result.matchResults.forEach(function (item) {
  item.textNodes.forEach(function (node) {
    var font = document.createElement('font')
    font.innerHTML = node.textContent
    node.parentNode.replaceChild(font, node)
  })
})
```

---
_Source: https://npm.io/package/across-nodes-search-keywords · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
