aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/utils.mjs
diff options
context:
space:
mode:
Diffstat (limited to 'src/utils.mjs')
-rw-r--r--src/utils.mjs21
1 files changed, 14 insertions, 7 deletions
diff --git a/src/utils.mjs b/src/utils.mjs
index 327bee4..9149ac2 100644
--- a/src/utils.mjs
+++ b/src/utils.mjs
@@ -5,7 +5,7 @@
5 * @param {Function} callback 5 * @param {Function} callback
6 */ 6 */
7export const onRemove = (element, callback) => { 7export const onRemove = (element, callback) => {
8 const parent = element.parentNode 8 const parent = element.parentElement
9 if (!parent) throw new Error('The node must already be attached') 9 if (!parent) throw new Error('The node must already be attached')
10 10
11 const obs = new window.MutationObserver(mutations => { 11 const obs = new window.MutationObserver(mutations => {
@@ -138,35 +138,40 @@ export const insideParent = (childElement, parentElement) => {
138 * replaceTextNodes. 138 * replaceTextNodes.
139 * @description Search current nodes by pattern, and replace them by new node 139 * @description Search current nodes by pattern, and replace them by new node
140 * @todo refactor to smaller methods 140 * @todo refactor to smaller methods
141 * @param {HTMLElement} element 141 * @param {HTMLElement} rootNode
142 * @param {RegExp} pattern 142 * @param {RegExp} pattern
143 * @param {Function} newNode - Create new node by each result of String.prototype.matchAll 143 * @param {Function} newNode - Create new node by each result of String.prototype.matchAll
144 */ 144 */
145export const replaceTextNodes = ( 145export const replaceTextNodes = (
146 element, 146 rootNode,
147 pattern, 147 pattern,
148 newNode = (match) => { 148 addNewNode = (match) => {
149 const link = document.createElement('a') 149 const link = document.createElement('a')
150 link.textContent(match.at(0)) 150 link.textContent(match.at(0))
151 return link 151 return link
152 }, 152 },
153) => { 153) => {
154 const nodeIterator = document.createNodeIterator( 154 const nodeIterator = document.createNodeIterator(
155 element, 155 rootNode,
156 window.NodeFilter.SHOW_TEXT, 156 window.NodeFilter.SHOW_TEXT,
157 node => node.textContent.match(pattern) 157 node => node.textContent.match(pattern) && !node.parentElement.closest('code')
158 ? window.NodeFilter.FILTER_ACCEPT 158 ? window.NodeFilter.FILTER_ACCEPT
159 : window.NodeFilter.FILTER_REJECT, 159 : window.NodeFilter.FILTER_REJECT,
160 ) 160 )
161 161
162 let node = nodeIterator.nextNode() 162 let node = nodeIterator.nextNode()
163 const nodeArray = []
163 while (node) { 164 while (node) {
164 let index = 0 165 let index = 0
165 for (const match of node.textContent.matchAll(pattern)) { 166 for (const match of node.textContent.matchAll(pattern)) {
166 const text = node.textContent.slice(index, match.index) 167 const text = node.textContent.slice(index, match.index)
168 const newNode = addNewNode(match)
169 if (!newNode) continue
170
167 index = match.index + match.at(0).length 171 index = match.index + match.at(0).length
168 node.parentElement.insertBefore(document.createTextNode(text), node) 172 node.parentElement.insertBefore(document.createTextNode(text), node)
169 node.parentElement.insertBefore(newNode(match), node) 173 node.parentElement.insertBefore(newNode, node)
174 nodeArray.push(newNode)
170 } 175 }
171 if (index < node.textContent.length) { 176 if (index < node.textContent.length) {
172 const text = node.textContent.slice(index) 177 const text = node.textContent.slice(index)
@@ -176,6 +181,8 @@ export const replaceTextNodes = (
176 node.parentElement.removeChild(node) 181 node.parentElement.removeChild(node)
177 node = nodeIterator.nextNode() 182 node = nodeIterator.nextNode()
178 } 183 }
184
185 return nodeArray
179} 186}
180 187
181/** 188/**