@contentpilot/read-more 1.0.6 → 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.
@@ -2,25 +2,28 @@ name: npm-publish
2
2
  on:
3
3
  push:
4
4
  branches:
5
- - master
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
- - name: Checkout repository
12
- uses: actions/checkout@master
13
- - name: Set up Node.js
14
- uses: actions/setup-node@v1
15
- with:
16
- node-version: '10.x'
17
- registry-url: 'https://registry.npmjs.org'
18
- - name: Publish if version has been updated
19
- uses: pascalgn/npm-publish-action@1.3.3
20
- with: # All of theses inputs are optional
21
- tag_name: "%s"
22
- commit_pattern: "^Release (\\S+)"
23
- workspace: "."
24
- env: # More info about the environment variables in the README
25
- GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # Leave this as is, it's automatically generated
26
- NPM_AUTH_TOKEN: ${{ secrets.NPM_AUTH_TOKEN }} # You need to set this in your repo settings
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
@@ -0,0 +1,7 @@
1
+ {
2
+ "semi": true,
3
+ "singleQuote": false,
4
+ "tabWidth": 2,
5
+ "trailingComma": "es5",
6
+ "printWidth": 80
7
+ }
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
- ### Bind to content area
14
-
15
- `$('.entry-content').cpReadMore();`
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`
@@ -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.3
6
4
  *
7
- * @package Read More
8
- * @author Content Pilot, Dallas, Texas
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
- function __buttonLess() {
40
- return __button(
41
- settings.lessLabel,
42
- settings.lessCSS
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
- // Remove the wrapping paragraph elements from <span>
47
- if( settings.unwrap ) {
48
- $('p > ' + settings.target).unwrap();
49
- }
50
-
51
- // Replace <span> with <button>
52
- if( settings.replace ) {
53
- $(settings.target).replaceWith( __buttonMore );
54
- }
55
-
56
- // Find first instance of button
57
- var $rmBtn = $(this).find('button.read-more').first();
58
-
59
- // Run if button is found
60
- if ($rmBtn.length) {
61
- var rmBtnOuterHtml = $('<div>').html($rmBtn.clone()).html();
62
- if ($rmBtn.parent()[0] != $(this)) {
63
- var $rmParent = $rmBtn.parent().first();
64
- var rpHtml = $rmParent.html(),
65
- splitIdx = rpHtml.indexOf(rmBtnOuterHtml) + rmBtnOuterHtml.length;
66
- $rmParent.html(rpHtml.substring(0, splitIdx) + "<span class=\"read-more-content " + settings.hideClass + "\">" + rpHtml.substring(splitIdx) + "</span>");
67
- }
68
-
69
- var $showMoreBtn = $(this).parent().find('button.read-more');
70
- var $showLessBtn = $('<button type="button" class="read-less '+settings.lessCSS+'">'+settings.lessLabel+'</button>');
71
-
72
- $showLessBtn.on('click', function() {
73
- $(this).closest('.read-more-content').slideUp('slow', function(){
74
- // $showMoreBtn.fadeIn('slow');
75
- $(this).parent().find( $showMoreBtn ).fadeIn('slow');
76
- });
77
-
78
- });
79
-
80
- $showLessBtn.appendTo($(this).parent().find('.read-more-content').last());
81
-
82
- $showMoreBtn.on('click', function(e) {
83
- e.preventDefault();
84
- var $showMoreBtn = $(this);
85
- $showMoreBtn.hide();
86
- $showMoreBtn.parent().find('.read-more-content').slideDown('slow', function(){
87
- $(this).find( $showLessBtn ).fadeIn('slow');
88
- });
89
-
90
- });
91
- }
92
- };
93
-
94
- }( jQuery ));
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": "1.0.6",
4
- "description": "Find and convet the WordPress <!--more--> read more tag to toggle all content below",
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
  }