data-bionic-reading 1.0.3 → 1.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -2
- package/dist/reading.esm.js +1 -1
- package/dist/reading.min.js +1 -1
- package/package.json +2 -2
- package/src/index.js +27 -25
package/README.md
CHANGED
|
@@ -3,6 +3,8 @@
|
|
|
3
3
|
Helps developers add support for [bionic reading](https://bionic-reading.com) to
|
|
4
4
|
their content 📚
|
|
5
5
|
|
|
6
|
+

|
|
7
|
+
|
|
6
8
|
## Install
|
|
7
9
|
|
|
8
10
|
### With a CDN
|
|
@@ -52,8 +54,6 @@ document.addEventListener('DOMContentLoaded', bionicReading())
|
|
|
52
54
|
</article>
|
|
53
55
|
```
|
|
54
56
|
|
|
55
|
-
And that's it.
|
|
56
|
-
|
|
57
57
|
## Stats
|
|
58
58
|
|
|
59
59
|

|
package/dist/reading.esm.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
function l(){let r=document.
|
|
1
|
+
function l(){let r=[...document.querySelectorAll("[data-bionic-reading]")];r.length&&r.forEach(i=>{[...i.querySelectorAll("h1, h2, h3, h4, h5, p, a, li, blockquote")].forEach(e=>{e.style.fontWeight=400;let a=e.innerText.split(" ").map(t=>{let n=t.length;if(n===1)return`<strong>${t}</strong>`;let c=Math.ceil(n/2);return t.split("").map((o,s)=>s<c?`<strong>${o}</strong>`:o).join("")});e.innerHTML=a.join(" ")})})}var A=l;export{A as default};
|
package/dist/reading.min.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
(()=>{function i(){let r=document.
|
|
1
|
+
(()=>{function i(){let r=[...document.querySelectorAll("[data-bionic-reading]")];r.length&&r.forEach(l=>{[...l.querySelectorAll("h1, h2, h3, h4, h5, p, a, li, blockquote")].forEach(e=>{e.style.fontWeight=400;let a=e.innerText.split(" ").map(t=>{let n=t.length;if(n===1)return`<strong>${t}</strong>`;let c=Math.ceil(n/2);return t.split("").map((o,s)=>s<c?`<strong>${o}</strong>`:o).join("")});e.innerHTML=a.join(" ")})})}document.addEventListener("DOMContentLoaded",i());})();
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "data-bionic-reading",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.4",
|
|
4
4
|
"description": "Helps developers add support for bionic reading to their content 📚",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"Bionic Reading"
|
|
@@ -11,6 +11,6 @@
|
|
|
11
11
|
"build": "node scripts/build.js"
|
|
12
12
|
},
|
|
13
13
|
"devDependencies": {
|
|
14
|
-
"esbuild": "^0.
|
|
14
|
+
"esbuild": "^0.18.17"
|
|
15
15
|
}
|
|
16
16
|
}
|
package/src/index.js
CHANGED
|
@@ -1,41 +1,43 @@
|
|
|
1
1
|
export default function () {
|
|
2
|
-
const
|
|
2
|
+
const bionicTargets = [...document.querySelectorAll('[data-bionic-reading]')]
|
|
3
3
|
|
|
4
|
-
if (!
|
|
4
|
+
if (!bionicTargets.length) {
|
|
5
5
|
return
|
|
6
6
|
}
|
|
7
7
|
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
8
|
+
bionicTargets.forEach((bionicTarget) => {
|
|
9
|
+
const articleElements = [
|
|
10
|
+
...bionicTarget.querySelectorAll(
|
|
11
|
+
'h1, h2, h3, h4, h5, p, a, li, blockquote'
|
|
12
|
+
),
|
|
13
|
+
]
|
|
13
14
|
|
|
14
|
-
|
|
15
|
-
|
|
15
|
+
articleElements.forEach((contentElement) => {
|
|
16
|
+
contentElement.style.fontWeight = 400
|
|
16
17
|
|
|
17
|
-
|
|
18
|
-
|
|
18
|
+
const elementText = contentElement.innerText
|
|
19
|
+
const elementTextArray = elementText.split(' ')
|
|
19
20
|
|
|
20
|
-
|
|
21
|
-
|
|
21
|
+
const elementTextArrayWithBold = elementTextArray.map((textWord) => {
|
|
22
|
+
const wordLength = textWord.length
|
|
22
23
|
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
24
|
+
if (wordLength === 1) {
|
|
25
|
+
return `<strong>${textWord}</strong>`
|
|
26
|
+
}
|
|
26
27
|
|
|
27
|
-
|
|
28
|
+
const wordLengthHalf = Math.ceil(wordLength / 2)
|
|
28
29
|
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
30
|
+
const wordArray = textWord.split('')
|
|
31
|
+
const wordArrayWithBold = wordArray.map((wordLetter, letterIndex) => {
|
|
32
|
+
return letterIndex < wordLengthHalf
|
|
33
|
+
? `<strong>${wordLetter}</strong>`
|
|
34
|
+
: wordLetter
|
|
35
|
+
})
|
|
36
|
+
|
|
37
|
+
return wordArrayWithBold.join('')
|
|
34
38
|
})
|
|
35
39
|
|
|
36
|
-
|
|
40
|
+
contentElement.innerHTML = elementTextArrayWithBold.join(' ')
|
|
37
41
|
})
|
|
38
|
-
|
|
39
|
-
contentElement.innerHTML = elementTextArrayWithBold.join(' ')
|
|
40
42
|
})
|
|
41
43
|
}
|