data-bionic-reading 1.0.0
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 +62 -0
- package/builds/cdn.js +3 -0
- package/builds/module.js +3 -0
- package/dist/reading.esm.js +1 -0
- package/dist/reading.min.js +1 -0
- package/package.json +16 -0
- package/scripts/build.js +19 -0
- package/src/index.js +39 -0
package/README.md
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
# Bionic Reading
|
|
2
|
+
|
|
3
|
+
Helps developers add support for [bionic reading](https://bionic-reading.com) to
|
|
4
|
+
their content 📚
|
|
5
|
+
|
|
6
|
+
## Install
|
|
7
|
+
|
|
8
|
+
### With a CDN
|
|
9
|
+
|
|
10
|
+
```html
|
|
11
|
+
<script
|
|
12
|
+
defer
|
|
13
|
+
src="https://unpkg.com/data-bionic-reading@latest/dist/reading.min.js"
|
|
14
|
+
></script>
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
### With a Package Manager
|
|
18
|
+
|
|
19
|
+
```shell
|
|
20
|
+
yarn add -D data-bionic-reading
|
|
21
|
+
npm install -D data-bionic-reading
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
```js
|
|
25
|
+
import bionicReading from 'data-bionic-reading'
|
|
26
|
+
|
|
27
|
+
document.addEventListener('DOMContentLoaded', bionicReading())
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
## Example
|
|
31
|
+
|
|
32
|
+
```html
|
|
33
|
+
<article data-bionic-reading>
|
|
34
|
+
<h1>Welcome to my website, it is a great website!</h1>
|
|
35
|
+
|
|
36
|
+
<p>
|
|
37
|
+
Lorem ipsum dolor sit amet consectetur, adipisicing elit. Perferendis,
|
|
38
|
+
dignissimos exercitationem! Ratione natus explicabo, maiores enim
|
|
39
|
+
reprehenderit perspiciatis ipsum deserunt?
|
|
40
|
+
</p>
|
|
41
|
+
|
|
42
|
+
<p>
|
|
43
|
+
Lorem ipsum dolor sit amet <a href="#">consectetur adipisicing</a> elit.
|
|
44
|
+
Rerum, possimus.
|
|
45
|
+
</p>
|
|
46
|
+
|
|
47
|
+
<ul>
|
|
48
|
+
<li>Lorem ipsum dolor sit amet.</li>
|
|
49
|
+
|
|
50
|
+
<li>Lorem ipsum, dolor sit amet consectetur adipisicing elit.</li>
|
|
51
|
+
</ul>
|
|
52
|
+
</article>
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
And that's it.
|
|
56
|
+
|
|
57
|
+
## Stats
|
|
58
|
+
|
|
59
|
+

|
|
60
|
+

|
|
61
|
+

|
|
62
|
+

|
package/builds/cdn.js
ADDED
package/builds/module.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
function l(){let t=document.querySelector("[data-bionic-reading]");if(!t)return;[...t.querySelectorAll("h1, h2, h3, h4, h5, h6, p, li, a")].forEach(e=>{e.style.fontWeight=400;let i=e.innerText.split(" ").map(r=>{let n=r.length;if(n===1)return`<strong>${r}</strong>`;let a=Math.floor(n/2);return r.split("").map((o,s)=>s<a?`<strong>${o}</strong>`:o).join("")});e.innerHTML=i.join(" ")})}var y=l;export{y as default};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
(()=>{function i(){let r=document.querySelector("[data-bionic-reading]");if(!r)return;[...r.querySelectorAll("h1, h2, h3, h4, h5, h6, p, li, a")].forEach(e=>{e.style.fontWeight=400;let l=e.innerText.split(" ").map(t=>{let n=t.length;if(n===1)return`<strong>${t}</strong>`;let a=Math.floor(n/2);return t.split("").map((o,s)=>s<a?`<strong>${o}</strong>`:o).join("")});e.innerHTML=l.join(" ")})}document.addEventListener("DOMContentLoaded",i());})();
|
package/package.json
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "data-bionic-reading",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Helps developers add support for bionic reading to their content 📚",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"Bionic Reading"
|
|
7
|
+
],
|
|
8
|
+
"module": "dist/reading.esm.js",
|
|
9
|
+
"unpkg": "dist/reading.min.js",
|
|
10
|
+
"scripts": {
|
|
11
|
+
"build": "node scripts/build.js"
|
|
12
|
+
},
|
|
13
|
+
"devDependencies": {
|
|
14
|
+
"esbuild": "^0.16.2"
|
|
15
|
+
}
|
|
16
|
+
}
|
package/scripts/build.js
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
buildPlugin({
|
|
2
|
+
entryPoints: ["builds/cdn.js"],
|
|
3
|
+
outfile: "dist/reading.min.js",
|
|
4
|
+
});
|
|
5
|
+
|
|
6
|
+
buildPlugin({
|
|
7
|
+
entryPoints: ["builds/module.js"],
|
|
8
|
+
outfile: "dist/reading.esm.js",
|
|
9
|
+
platform: "neutral",
|
|
10
|
+
mainFields: ["main", "module"],
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
function buildPlugin(buildOptions) {
|
|
14
|
+
return require("esbuild").buildSync({
|
|
15
|
+
...buildOptions,
|
|
16
|
+
minify: true,
|
|
17
|
+
bundle: true,
|
|
18
|
+
});
|
|
19
|
+
}
|
package/src/index.js
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
export default function () {
|
|
2
|
+
const articleElement = document.querySelector("[data-bionic-reading]");
|
|
3
|
+
|
|
4
|
+
if (!articleElement) {
|
|
5
|
+
return;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
const articleElements = [
|
|
9
|
+
...articleElement.querySelectorAll("h1, h2, h3, h4, h5, h6, p, li, a"),
|
|
10
|
+
];
|
|
11
|
+
|
|
12
|
+
articleElements.forEach((contentElement) => {
|
|
13
|
+
contentElement.style.fontWeight = 400;
|
|
14
|
+
|
|
15
|
+
const elementText = contentElement.innerText;
|
|
16
|
+
const elementTextArray = elementText.split(" ");
|
|
17
|
+
|
|
18
|
+
const elementTextArrayWithBold = elementTextArray.map((textWord) => {
|
|
19
|
+
const wordLength = textWord.length;
|
|
20
|
+
|
|
21
|
+
if (wordLength === 1) {
|
|
22
|
+
return `<strong>${textWord}</strong>`;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
const wordLengthHalf = Math.floor(wordLength / 2);
|
|
26
|
+
|
|
27
|
+
const wordArray = textWord.split("");
|
|
28
|
+
const wordArrayWithBold = wordArray.map((wordLetter, letterIndex) => {
|
|
29
|
+
return letterIndex < wordLengthHalf
|
|
30
|
+
? `<strong>${wordLetter}</strong>`
|
|
31
|
+
: wordLetter;
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
return wordArrayWithBold.join("");
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
contentElement.innerHTML = elementTextArrayWithBold.join(" ");
|
|
38
|
+
});
|
|
39
|
+
}
|