@contentpilot/read-more 1.0.7 → 2.0.2
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/.github/workflows/npm-publish.yml +20 -17
- package/.prettierrc +7 -0
- package/LICENSE +21 -0
- package/README.md +37 -4
- package/eslint.config.js +26 -0
- package/index.js +199 -86
- package/package.json +21 -5
|
@@ -2,25 +2,28 @@ name: npm-publish
|
|
|
2
2
|
on:
|
|
3
3
|
push:
|
|
4
4
|
branches:
|
|
5
|
-
-
|
|
5
|
+
- main
|
|
6
6
|
jobs:
|
|
7
7
|
npm-publish:
|
|
8
8
|
name: npm-publish
|
|
9
9
|
runs-on: ubuntu-latest
|
|
10
|
+
if: contains(github.event.head_commit.message, 'Release')
|
|
10
11
|
steps:
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
12
|
+
- name: Checkout repository
|
|
13
|
+
uses: actions/checkout@v4
|
|
14
|
+
- name: Setup Node.js
|
|
15
|
+
uses: actions/setup-node@v4
|
|
16
|
+
with:
|
|
17
|
+
node-version: "20.x"
|
|
18
|
+
- name: Publish to NPM
|
|
19
|
+
uses: JS-DevTools/npm-publish@v3
|
|
20
|
+
with:
|
|
21
|
+
package: "./package.json"
|
|
22
|
+
access: public
|
|
23
|
+
tag: latest
|
|
24
|
+
token: ${{ secrets.NPM_AUTH_TOKEN }}
|
|
25
|
+
- name: Create Git tag
|
|
26
|
+
run: |
|
|
27
|
+
VERSION=$(node -p "require('./package.json').version")
|
|
28
|
+
git tag "$VERSION"
|
|
29
|
+
git push origin "$VERSION"
|
package/.prettierrc
ADDED
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Content Pilot
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -10,9 +10,42 @@ Find and convert `<!--more-->` tag in the WordPress Editor into a button that al
|
|
|
10
10
|
|
|
11
11
|
`./node_modules/@contentpilot/read-more/index.js`
|
|
12
12
|
|
|
13
|
-
###
|
|
14
|
-
|
|
15
|
-
|
|
13
|
+
### Usage
|
|
14
|
+
|
|
15
|
+
#### With jQuery (legacy)
|
|
16
|
+
|
|
17
|
+
```javascript
|
|
18
|
+
$(".entry-content").cpReadMore();
|
|
19
|
+
|
|
20
|
+
// With options
|
|
21
|
+
$(".entry-content").cpReadMore({
|
|
22
|
+
target: '[id^="more-"]',
|
|
23
|
+
unwrap: true,
|
|
24
|
+
replace: true,
|
|
25
|
+
moreLabel: "More",
|
|
26
|
+
moreCSS: "button clean",
|
|
27
|
+
lessLabel: "Less",
|
|
28
|
+
lessCSS: "button clean",
|
|
29
|
+
hideClass: "display-none",
|
|
30
|
+
});
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
#### Without jQuery (ES6)
|
|
34
|
+
|
|
35
|
+
Include `index.js` in your site's build/task-runner pipeline — it's a plain
|
|
36
|
+
browser script, not a module to `import`/`require`.
|
|
37
|
+
|
|
38
|
+
```javascript
|
|
39
|
+
// Auto-initializes on elements with class "entry-content".
|
|
40
|
+
|
|
41
|
+
// Or manually:
|
|
42
|
+
const element = document.querySelector(".custom-entry-content");
|
|
43
|
+
new ReadMore(element, {
|
|
44
|
+
target: '[id^="more-"]',
|
|
45
|
+
moreLabel: "Read More",
|
|
46
|
+
lessLabel: "Read Less",
|
|
47
|
+
});
|
|
48
|
+
```
|
|
16
49
|
|
|
17
50
|
### Publish to NPM
|
|
18
51
|
|
|
@@ -20,4 +53,4 @@ To update this repository on NPM using the GitHub action,
|
|
|
20
53
|
|
|
21
54
|
1. Bump the version number in `package.json`
|
|
22
55
|
2. Commit with message pattern of `Release x.x.x`
|
|
23
|
-
3. Push to `master` branch to `origin`
|
|
56
|
+
3. Push to `master` branch to `origin`
|
package/eslint.config.js
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import js from "@eslint/js";
|
|
2
|
+
import prettier from "eslint-config-prettier";
|
|
3
|
+
|
|
4
|
+
export default [
|
|
5
|
+
js.configs.recommended,
|
|
6
|
+
prettier,
|
|
7
|
+
{
|
|
8
|
+
languageOptions: {
|
|
9
|
+
ecmaVersion: "latest",
|
|
10
|
+
sourceType: "module",
|
|
11
|
+
globals: {
|
|
12
|
+
document: "readonly",
|
|
13
|
+
jQuery: "readonly",
|
|
14
|
+
requestAnimationFrame: "readonly",
|
|
15
|
+
setTimeout: "readonly",
|
|
16
|
+
ReadMore: "writable",
|
|
17
|
+
},
|
|
18
|
+
},
|
|
19
|
+
rules: {
|
|
20
|
+
"no-unused-vars": "warn",
|
|
21
|
+
"no-console": "warn",
|
|
22
|
+
"prefer-const": "error",
|
|
23
|
+
"no-var": "error",
|
|
24
|
+
},
|
|
25
|
+
},
|
|
26
|
+
];
|
package/index.js
CHANGED
|
@@ -1,94 +1,207 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
2
|
+
* Read More - JavaScript implementation with jQuery backwards compatibility
|
|
3
3
|
* Find and convert <!--more--> tag in the WordPress Editor into a button that allows visitor to toggle content visibility
|
|
4
|
-
*
|
|
5
|
-
* @version 1.0.7
|
|
6
4
|
*
|
|
7
|
-
* @
|
|
8
|
-
* @
|
|
5
|
+
* @version 2.0.0
|
|
6
|
+
* @package Read More
|
|
7
|
+
* @author Content Pilot, Dallas, Texas
|
|
9
8
|
* @link https://www.contentpilot.com
|
|
10
9
|
* @copyright Copyright © 2018 Content Pilot
|
|
11
|
-
* @license GPL-2.0+
|
|
10
|
+
* @license GPL-2.0+
|
|
12
11
|
*/
|
|
13
|
-
(function ( $ ) {
|
|
14
|
-
|
|
15
|
-
$.fn.cpReadMore = function( options ) {
|
|
16
|
-
|
|
17
|
-
var settings = $.extend({
|
|
18
|
-
target: '[id^="more-"]',
|
|
19
|
-
unwrap: true,
|
|
20
|
-
replace: true,
|
|
21
|
-
moreLabel: 'More',
|
|
22
|
-
moreCSS: 'button clean',
|
|
23
|
-
lessLabel: 'Less',
|
|
24
|
-
lessCSS: 'button clean',
|
|
25
|
-
hideClass: 'display-none'
|
|
26
|
-
}, options );
|
|
27
|
-
|
|
28
|
-
function __button( label, css ) {
|
|
29
|
-
return $('<button type="button" class="read-more '+css+'">'+label+'</button>');
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
function __buttonMore() {
|
|
33
|
-
return __button(
|
|
34
|
-
settings.moreLabel,
|
|
35
|
-
settings.moreCSS
|
|
36
|
-
);
|
|
37
|
-
}
|
|
38
12
|
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
13
|
+
class ReadMore {
|
|
14
|
+
static idCounter = 0;
|
|
15
|
+
|
|
16
|
+
constructor(element, options = {}) {
|
|
17
|
+
this.element = element;
|
|
18
|
+
this.settings = {
|
|
19
|
+
target: '[id^="more-"]',
|
|
20
|
+
unwrap: true,
|
|
21
|
+
replace: true,
|
|
22
|
+
moreLabel: "More",
|
|
23
|
+
moreCSS: "button clean",
|
|
24
|
+
lessLabel: "Less",
|
|
25
|
+
lessCSS: "button clean",
|
|
26
|
+
hideClass: "display-none",
|
|
27
|
+
...options,
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
this.init();
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
createButton(label, css) {
|
|
34
|
+
const button = document.createElement("button");
|
|
35
|
+
button.type = "button";
|
|
36
|
+
button.className = `read-more ${css}`;
|
|
37
|
+
button.textContent = label;
|
|
38
|
+
return button;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
createMoreButton() {
|
|
42
|
+
return this.createButton(this.settings.moreLabel, this.settings.moreCSS);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
createLessButton() {
|
|
46
|
+
return this.createButton(this.settings.lessLabel, this.settings.lessCSS);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
slideUp(element, callback) {
|
|
50
|
+
element.style.height = element.scrollHeight + "px";
|
|
51
|
+
element.style.overflow = "hidden";
|
|
52
|
+
element.style.transition = "height 0.5s ease";
|
|
53
|
+
|
|
54
|
+
requestAnimationFrame(() => {
|
|
55
|
+
element.style.height = "0";
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
setTimeout(() => {
|
|
59
|
+
element.style.display = "none";
|
|
60
|
+
element.style.height = "";
|
|
61
|
+
element.style.overflow = "";
|
|
62
|
+
element.style.transition = "";
|
|
63
|
+
if (callback) {
|
|
64
|
+
callback();
|
|
65
|
+
}
|
|
66
|
+
}, 500);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
slideDown(element, callback) {
|
|
70
|
+
element.style.display = "";
|
|
71
|
+
const height = element.scrollHeight;
|
|
72
|
+
element.style.height = "0";
|
|
73
|
+
element.style.overflow = "hidden";
|
|
74
|
+
element.style.transition = "height 0.5s ease";
|
|
75
|
+
|
|
76
|
+
requestAnimationFrame(() => {
|
|
77
|
+
element.style.height = height + "px";
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
setTimeout(() => {
|
|
81
|
+
element.style.height = "";
|
|
82
|
+
element.style.overflow = "";
|
|
83
|
+
element.style.transition = "";
|
|
84
|
+
if (callback) {
|
|
85
|
+
callback();
|
|
86
|
+
}
|
|
87
|
+
}, 500);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
fadeIn(element) {
|
|
91
|
+
element.style.opacity = "0";
|
|
92
|
+
element.style.display = "";
|
|
93
|
+
element.style.transition = "opacity 0.5s ease";
|
|
94
|
+
|
|
95
|
+
requestAnimationFrame(() => {
|
|
96
|
+
element.style.opacity = "1";
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
setTimeout(() => {
|
|
100
|
+
element.style.opacity = "";
|
|
101
|
+
element.style.transition = "";
|
|
102
|
+
}, 500);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
init() {
|
|
106
|
+
// Remove the wrapping paragraph elements from target elements
|
|
107
|
+
if (this.settings.unwrap) {
|
|
108
|
+
const targets = this.element.querySelectorAll(
|
|
109
|
+
`p > ${this.settings.target}`
|
|
110
|
+
);
|
|
111
|
+
targets.forEach((target) => {
|
|
112
|
+
const parent = target.parentElement;
|
|
113
|
+
if (parent.tagName === "P") {
|
|
114
|
+
parent.replaceWith(target);
|
|
44
115
|
}
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
// Replace target elements with More button
|
|
120
|
+
if (this.settings.replace) {
|
|
121
|
+
const targets = this.element.querySelectorAll(this.settings.target);
|
|
122
|
+
targets.forEach((target) => {
|
|
123
|
+
const moreButton = this.createMoreButton();
|
|
124
|
+
target.replaceWith(moreButton);
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
// Find first instance of read-more button
|
|
129
|
+
const rmBtn = this.element.querySelector("button.read-more");
|
|
130
|
+
|
|
131
|
+
if (!rmBtn) {
|
|
132
|
+
return;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
// Split content at button position
|
|
136
|
+
const rmParent = rmBtn.parentElement;
|
|
137
|
+
if (rmParent && rmParent !== this.element) {
|
|
138
|
+
const parentHtml = rmParent.innerHTML;
|
|
139
|
+
const buttonHtml = rmBtn.outerHTML;
|
|
140
|
+
const splitIdx = parentHtml.indexOf(buttonHtml) + buttonHtml.length;
|
|
141
|
+
|
|
142
|
+
const beforeContent = parentHtml.substring(0, splitIdx);
|
|
143
|
+
const afterContent = parentHtml.substring(splitIdx);
|
|
144
|
+
|
|
145
|
+
if (afterContent.trim()) {
|
|
146
|
+
const contentWrapper = document.createElement("span");
|
|
147
|
+
contentWrapper.className = `read-more-content ${this.settings.hideClass}`;
|
|
148
|
+
contentWrapper.id = `read-more-content-${ReadMore.idCounter++}`;
|
|
149
|
+
contentWrapper.innerHTML = afterContent;
|
|
150
|
+
|
|
151
|
+
rmParent.innerHTML = beforeContent;
|
|
152
|
+
rmParent.appendChild(contentWrapper);
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
// Create and setup Less button
|
|
157
|
+
const contentWrapper = this.element
|
|
158
|
+
.closest(".entry-content")
|
|
159
|
+
?.querySelector(".read-more-content");
|
|
160
|
+
|
|
161
|
+
if (!contentWrapper) {
|
|
162
|
+
return;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
rmBtn.setAttribute("aria-expanded", "false");
|
|
166
|
+
rmBtn.setAttribute("aria-controls", contentWrapper.id);
|
|
167
|
+
|
|
168
|
+
const lessButton = this.createLessButton();
|
|
169
|
+
lessButton.setAttribute("aria-expanded", "true");
|
|
170
|
+
lessButton.setAttribute("aria-controls", contentWrapper.id);
|
|
171
|
+
lessButton.addEventListener("click", () => {
|
|
172
|
+
this.slideUp(contentWrapper, () => {
|
|
173
|
+
this.fadeIn(rmBtn);
|
|
174
|
+
});
|
|
175
|
+
});
|
|
176
|
+
|
|
177
|
+
contentWrapper.appendChild(lessButton);
|
|
178
|
+
|
|
179
|
+
// Setup More button click handler
|
|
180
|
+
rmBtn.addEventListener("click", (e) => {
|
|
181
|
+
e.preventDefault();
|
|
182
|
+
rmBtn.style.display = "none";
|
|
183
|
+
this.slideDown(contentWrapper, () => {
|
|
184
|
+
this.fadeIn(lessButton);
|
|
185
|
+
});
|
|
186
|
+
});
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
// jQuery plugin wrapper for backwards compatibility.
|
|
191
|
+
if (typeof jQuery !== "undefined") {
|
|
192
|
+
jQuery.fn.cpReadMore = function (options) {
|
|
193
|
+
return this.each(function () {
|
|
194
|
+
new ReadMore(this, options);
|
|
195
|
+
});
|
|
196
|
+
};
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
document.addEventListener("DOMContentLoaded", () => {
|
|
200
|
+
// Only auto-init if jQuery plugin wasn't used.
|
|
201
|
+
if (typeof jQuery === "undefined") {
|
|
202
|
+
const contentElements = document.querySelectorAll(".entry-content");
|
|
203
|
+
contentElements.forEach((element) => {
|
|
204
|
+
new ReadMore(element);
|
|
205
|
+
});
|
|
206
|
+
}
|
|
207
|
+
});
|
package/package.json
CHANGED
|
@@ -1,22 +1,38 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@contentpilot/read-more",
|
|
3
|
-
"version": "
|
|
4
|
-
"description": "Find and
|
|
3
|
+
"version": "2.0.2",
|
|
4
|
+
"description": "Find and convert the WordPress <!--more--> read more tag to toggle all content below",
|
|
5
5
|
"main": "index.js",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"scripts": {
|
|
8
|
+
"lint": "eslint index.js",
|
|
9
|
+
"lint:fix": "eslint index.js --fix",
|
|
10
|
+
"format": "prettier --write .",
|
|
11
|
+
"format:check": "prettier --check ."
|
|
12
|
+
},
|
|
6
13
|
"publishConfig": {
|
|
7
14
|
"access": "public"
|
|
8
15
|
},
|
|
9
16
|
"repository": {
|
|
10
17
|
"type": "git",
|
|
11
|
-
"url": "https://github.com/content-pilot/read-more"
|
|
18
|
+
"url": "https://github.com/content-pilot/read-more.git"
|
|
12
19
|
},
|
|
13
20
|
"keywords": [
|
|
14
21
|
"read",
|
|
15
22
|
"more",
|
|
16
23
|
"WordPress",
|
|
17
24
|
"editor",
|
|
18
|
-
"wysiwyg"
|
|
25
|
+
"wysiwyg",
|
|
26
|
+
"Content",
|
|
27
|
+
"Pilot",
|
|
28
|
+
"Content Pilot"
|
|
19
29
|
],
|
|
20
30
|
"author": "Content Pilot",
|
|
21
|
-
"license": "MIT"
|
|
31
|
+
"license": "MIT",
|
|
32
|
+
"devDependencies": {
|
|
33
|
+
"@eslint/js": "^10.0.1",
|
|
34
|
+
"eslint": "^10.8.0",
|
|
35
|
+
"eslint-config-prettier": "^10.1.8",
|
|
36
|
+
"prettier": "^3.9.6"
|
|
37
|
+
}
|
|
22
38
|
}
|