@contentpilot/read-more 2.0.2 → 2.1.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/LICENSE CHANGED
@@ -1,6 +1,6 @@
1
1
  MIT License
2
2
 
3
- Copyright (c) 2025 Content Pilot
3
+ Copyright (c) 2019 Content Pilot
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
package/README.md CHANGED
@@ -1,18 +1,44 @@
1
1
  # Content Pilot Read More
2
2
 
3
+ [![npm version](https://img.shields.io/npm/v/@contentpilot/read-more.svg)](https://www.npmjs.com/package/@contentpilot/read-more)
4
+
3
5
  Find and convert `<!--more-->` tag in the WordPress Editor into a button that allows visitor to toggle content visibility.
4
6
 
5
- ### Installation
7
+ ## Installation
8
+
9
+ Install the package:
10
+
11
+ ```bash
12
+ npm install @contentpilot/read-more --save-dev
13
+ ```
14
+
15
+ Import the module:
16
+
17
+ ```javascript
18
+ import { ReadMore, init } from "@contentpilot/read-more";
19
+ ```
20
+
21
+ Or include `./node_modules/@contentpilot/read-more/index.js` as a plain
22
+ script.
23
+
24
+ ## Usage
6
25
 
7
- `npm install @contentpilot/read-more --save-dev`
26
+ Without jQuery, it auto-initializes on `.entry-content` on `DOMContentLoaded`.
27
+ With jQuery, nothing runs until you call `.cpReadMore()`.
8
28
 
9
- ### Require JS file in task runner
29
+ **Initialize:**
10
30
 
11
- `./node_modules/@contentpilot/read-more/index.js`
31
+ ```javascript
32
+ import { init } from "@contentpilot/read-more";
12
33
 
13
- ### Usage
34
+ init(".entry-content", {
35
+ more: '<button type="button" class="read-more">Read More</button>',
36
+ less: '<button type="button" class="read-less">Less</button>',
37
+ transitionSpeed: 350,
38
+ });
39
+ ```
14
40
 
15
- #### With jQuery (legacy)
41
+ **jQuery Compatibility:**
16
42
 
17
43
  ```javascript
18
44
  $(".entry-content").cpReadMore();
@@ -30,27 +56,32 @@ $(".entry-content").cpReadMore({
30
56
  });
31
57
  ```
32
58
 
33
- #### Without jQuery (ES6)
59
+ The 1.x options still work exactly as they did in 1.x. Sites using the jQuery
60
+ plugin need no code changes.
34
61
 
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`.
62
+ ### Options
37
63
 
38
- ```javascript
39
- // Auto-initializes on elements with class "entry-content".
64
+ - `target` - Selector of the `<!--more-->` marker (default `[id^="more-"]`)
65
+ - `unwrap` - Remove the `<p>` wrapping the marker (default `true`)
66
+ - `replace` - Replace the marker with the More button (default `true`)
67
+ - `more` - More button HTML; must be a `<button>` with class `read-more`
68
+ - `less` - Less button HTML
69
+ - `hideClass` - Class toggled on the hidden content (default `display-none`)
70
+ - `transitionSpeed` - Slide duration in ms (default `350`)
40
71
 
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
- ```
72
+ **Legacy 1.x options** (used instead of `more`/`less` when present):
73
+
74
+ - `moreLabel` / `lessLabel` - Button labels, may contain HTML (default `More` / `Less`)
75
+ - `moreCSS` / `lessCSS` - Button classes (default `button clean`)
49
76
 
50
- ### Publish to NPM
77
+ ## Publish to NPM
51
78
 
52
79
  To update this repository on NPM using the GitHub action,
53
80
 
54
81
  1. Bump the version number in `package.json`
55
- 2. Commit with message pattern of `Release x.x.x`
56
- 3. Push to `master` branch to `origin`
82
+ 2. Open a PR titled `Release x.x.x`
83
+ 3. Squash and merge it once approved
84
+
85
+ ## License
86
+
87
+ MIT
package/index.esm.js ADDED
@@ -0,0 +1,6 @@
1
+ // ESM entry: re-exports the API index.js exposes on window.
2
+ import "./index.js";
3
+
4
+ const { ReadMore, init } = window.CpReadMore;
5
+
6
+ export { ReadMore, init };
package/index.js CHANGED
@@ -1,207 +1,241 @@
1
1
  /**
2
- * Read More - JavaScript implementation with jQuery backwards compatibility
3
- * Find and convert <!--more--> tag in the WordPress Editor into a button that allows visitor to toggle content visibility
2
+ * Read More
3
+ * Find and convert the WordPress <!--more--> read more tag to toggle all content below
4
4
  *
5
- * @version 2.0.0
6
- * @package Read More
7
- * @author Content Pilot, Dallas, Texas
8
- * @link https://www.contentpilot.com
9
- * @copyright Copyright © 2018 Content Pilot
10
- * @license GPL-2.0+
5
+ * @author Content Pilot
6
+ * @copyright Copyright (c) 2019 Content Pilot
7
+ * @license MIT
11
8
  */
12
9
 
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";
10
+ // Plain script, no leaked globals; bundlers get index.esm.js.
11
+ (() => {
12
+ class ReadMore {
13
+ static idCounter = 0;
14
+
15
+ constructor(element, options = {}) {
16
+ if (element.dataset.cpReadMoreInit) return;
17
+ element.dataset.cpReadMoreInit = "true";
18
+
19
+ this.element = element;
20
+
21
+ // Legacy 1.x options — build more/less HTML the same way the jQuery
22
+ // plugin did (raw string concatenation, labels may contain HTML).
23
+ const legacyKeys = ["moreLabel", "moreCSS", "lessLabel", "lessCSS"];
24
+ const legacySettings = legacyKeys.some((key) => key in options)
25
+ ? {
26
+ more: `<button type="button" class="read-more ${options.moreCSS ?? "button clean"}">${options.moreLabel ?? "More"}</button>`,
27
+ less: `<button type="button" class="read-less ${options.lessCSS ?? "button clean"}">${options.lessLabel ?? "Less"}</button>`,
28
+ }
29
+ : {};
30
+
31
+ this.settings = {
32
+ target: '[id^="more-"]',
33
+ unwrap: true,
34
+ replace: true,
35
+ more: '<button type="button" class="read-more button clean">More</button>',
36
+ less: '<button type="button" class="read-less button clean">Less</button>',
37
+ hideClass: "display-none",
38
+ transitionSpeed: 350,
39
+ ...legacySettings,
40
+ ...options,
41
+ };
42
+
43
+ this.init();
44
+ }
53
45
 
54
- requestAnimationFrame(() => {
55
- element.style.height = "0";
56
- });
46
+ createButtonFromHTML(html) {
47
+ const template = document.createElement("template");
48
+ template.innerHTML = html.trim();
49
+ return template.content.firstElementChild;
50
+ }
57
51
 
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
- }
52
+ createMoreButton() {
53
+ return this.createButtonFromHTML(this.settings.more);
54
+ }
68
55
 
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";
56
+ createLessButton() {
57
+ return this.createButtonFromHTML(this.settings.less);
58
+ }
75
59
 
76
- requestAnimationFrame(() => {
77
- element.style.height = height + "px";
78
- });
60
+ slideUp(element, callback) {
61
+ element.style.height = element.scrollHeight + "px";
62
+ element.style.overflow = "hidden";
63
+ element.style.transition = `height ${this.settings.transitionSpeed}ms ease`;
79
64
 
80
- setTimeout(() => {
81
- element.style.height = "";
82
- element.style.overflow = "";
83
- element.style.transition = "";
84
- if (callback) {
85
- callback();
86
- }
87
- }, 500);
88
- }
65
+ requestAnimationFrame(() => {
66
+ element.style.height = "0";
67
+ });
89
68
 
90
- fadeIn(element) {
91
- element.style.opacity = "0";
92
- element.style.display = "";
93
- element.style.transition = "opacity 0.5s ease";
69
+ element.addEventListener(
70
+ "transitionend",
71
+ (e) => {
72
+ if (e.propertyName !== "height") return;
73
+ element.style.display = "none";
74
+ element.style.height = "";
75
+ element.style.overflow = "";
76
+ element.style.transition = "";
77
+ element.classList.add(this.settings.hideClass);
78
+ if (callback) {
79
+ callback();
80
+ }
81
+ },
82
+ { once: true }
83
+ );
84
+ }
94
85
 
95
- requestAnimationFrame(() => {
96
- element.style.opacity = "1";
97
- });
86
+ slideDown(element, callback) {
87
+ element.classList.remove(this.settings.hideClass);
88
+ element.style.display = "";
89
+ const height = element.scrollHeight;
90
+ element.style.height = "0";
91
+ element.style.overflow = "hidden";
92
+ element.style.transition = `height ${this.settings.transitionSpeed}ms ease`;
98
93
 
99
- setTimeout(() => {
100
- element.style.opacity = "";
101
- element.style.transition = "";
102
- }, 500);
103
- }
94
+ requestAnimationFrame(() => {
95
+ element.style.height = height + "px";
96
+ });
104
97
 
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}`
98
+ element.addEventListener(
99
+ "transitionend",
100
+ (e) => {
101
+ if (e.propertyName !== "height") return;
102
+ element.style.height = "";
103
+ element.style.overflow = "";
104
+ element.style.transition = "";
105
+ if (callback) {
106
+ callback();
107
+ }
108
+ },
109
+ { once: true }
110
110
  );
111
- targets.forEach((target) => {
112
- const parent = target.parentElement;
113
- if (parent.tagName === "P") {
114
- parent.replaceWith(target);
115
- }
116
- });
117
111
  }
118
112
 
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
- }
113
+ fadeIn(element) {
114
+ element.style.opacity = "0";
115
+ element.style.display = "";
116
+ element.style.transition = "opacity 0.5s ease";
127
117
 
128
- // Find first instance of read-more button
129
- const rmBtn = this.element.querySelector("button.read-more");
118
+ requestAnimationFrame(() => {
119
+ element.style.opacity = "1";
120
+ });
130
121
 
131
- if (!rmBtn) {
132
- return;
122
+ setTimeout(() => {
123
+ element.style.opacity = "";
124
+ element.style.transition = "";
125
+ }, 500);
133
126
  }
134
127
 
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;
128
+ init() {
129
+ // Remove the wrapping paragraph elements from target elements
130
+ if (this.settings.unwrap) {
131
+ const targets = this.element.querySelectorAll(
132
+ `p > ${this.settings.target}`
133
+ );
134
+ targets.forEach((target) => {
135
+ const parent = target.parentElement;
136
+ if (parent.tagName === "P") {
137
+ parent.replaceWith(target);
138
+ }
139
+ });
140
+ }
141
141
 
142
- const beforeContent = parentHtml.substring(0, splitIdx);
143
- const afterContent = parentHtml.substring(splitIdx);
142
+ // Replace target elements with More button
143
+ if (this.settings.replace) {
144
+ const targets = this.element.querySelectorAll(this.settings.target);
145
+ targets.forEach((target) => {
146
+ const moreButton = this.createMoreButton();
147
+ target.replaceWith(moreButton);
148
+ });
149
+ }
144
150
 
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;
151
+ // Find first instance of read-more button
152
+ let rmBtn = this.element.querySelector("button.read-more");
150
153
 
151
- rmParent.innerHTML = beforeContent;
152
- rmParent.appendChild(contentWrapper);
154
+ if (!rmBtn) {
155
+ return;
153
156
  }
154
- }
155
157
 
156
- // Create and setup Less button
157
- const contentWrapper = this.element
158
- .closest(".entry-content")
159
- ?.querySelector(".read-more-content");
158
+ // Split content at button position
159
+ const rmParent = rmBtn.parentElement;
160
+ if (rmParent) {
161
+ const parentHtml = rmParent.innerHTML;
162
+ const buttonHtml = rmBtn.outerHTML;
163
+ const splitIdx = parentHtml.indexOf(buttonHtml) + buttonHtml.length;
164
+
165
+ const beforeContent = parentHtml.substring(0, splitIdx);
166
+ const afterContent = parentHtml.substring(splitIdx);
167
+
168
+ if (afterContent.trim()) {
169
+ const contentWrapper = document.createElement("span");
170
+ contentWrapper.className = `read-more-content ${this.settings.hideClass}`;
171
+ contentWrapper.id = `read-more-content-${ReadMore.idCounter++}`;
172
+ contentWrapper.innerHTML = afterContent;
173
+
174
+ rmParent.innerHTML = beforeContent;
175
+ rmParent.appendChild(contentWrapper);
176
+
177
+ // rmParent.innerHTML rewrite above detaches the original button
178
+ // node from the DOM — re-query the live one so click handlers
179
+ // attach to what's actually on the page.
180
+ rmBtn = rmParent.querySelector("button.read-more");
181
+ }
182
+ }
160
183
 
161
- if (!contentWrapper) {
162
- return;
163
- }
184
+ // Create and setup Less button
185
+ const contentWrapper = this.element.querySelector(".read-more-content");
164
186
 
165
- rmBtn.setAttribute("aria-expanded", "false");
166
- rmBtn.setAttribute("aria-controls", contentWrapper.id);
187
+ if (!contentWrapper) {
188
+ return;
189
+ }
190
+
191
+ rmBtn.setAttribute("aria-expanded", "false");
192
+ rmBtn.setAttribute("aria-controls", contentWrapper.id);
167
193
 
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);
194
+ const lessButton = this.createLessButton();
195
+ lessButton.setAttribute("aria-expanded", "true");
196
+ lessButton.setAttribute("aria-controls", contentWrapper.id);
197
+ lessButton.addEventListener("click", () => {
198
+ this.slideUp(contentWrapper, () => {
199
+ this.fadeIn(rmBtn);
200
+ });
174
201
  });
175
- });
176
202
 
177
- contentWrapper.appendChild(lessButton);
203
+ contentWrapper.appendChild(lessButton);
178
204
 
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);
205
+ // Setup More button click handler
206
+ rmBtn.addEventListener("click", (e) => {
207
+ e.preventDefault();
208
+ rmBtn.style.display = "none";
209
+ this.slideDown(contentWrapper, () => {
210
+ this.fadeIn(lessButton);
211
+ });
185
212
  });
186
- });
213
+ }
187
214
  }
188
- }
189
215
 
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
- }
216
+ // jQuery plugin wrapper for backwards compatibility.
217
+ if (typeof jQuery !== "undefined") {
218
+ jQuery.fn.cpReadMore = function (options) {
219
+ return this.each(function () {
220
+ new ReadMore(this, options);
221
+ });
222
+ };
223
+ }
198
224
 
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");
225
+ function init(selector = ".entry-content", options = {}) {
226
+ const contentElements = document.querySelectorAll(selector);
203
227
  contentElements.forEach((element) => {
204
- new ReadMore(element);
228
+ new ReadMore(element, options);
205
229
  });
206
230
  }
207
- });
231
+
232
+ document.addEventListener("DOMContentLoaded", () => {
233
+ // Only auto-init if jQuery plugin wasn't used.
234
+ if (typeof jQuery === "undefined") {
235
+ init();
236
+ }
237
+ });
238
+
239
+ // Exposed for index.esm.js.
240
+ window.CpReadMore = { ReadMore, init };
241
+ })();
package/package.json CHANGED
@@ -1,12 +1,24 @@
1
1
  {
2
2
  "name": "@contentpilot/read-more",
3
- "version": "2.0.2",
3
+ "version": "2.1.2",
4
4
  "description": "Find and convert the WordPress <!--more--> read more tag to toggle all content below",
5
5
  "main": "index.js",
6
+ "module": "index.esm.js",
7
+ "exports": {
8
+ ".": {
9
+ "import": "./index.esm.js",
10
+ "default": "./index.js"
11
+ },
12
+ "./index.js": "./index.js"
13
+ },
14
+ "files": [
15
+ "index.js",
16
+ "index.esm.js"
17
+ ],
6
18
  "type": "module",
7
19
  "scripts": {
8
- "lint": "eslint index.js",
9
- "lint:fix": "eslint index.js --fix",
20
+ "lint": "eslint index.js index.esm.js",
21
+ "lint:fix": "eslint index.js index.esm.js --fix",
10
22
  "format": "prettier --write .",
11
23
  "format:check": "prettier --check ."
12
24
  },
@@ -1,29 +0,0 @@
1
- name: npm-publish
2
- on:
3
- push:
4
- branches:
5
- - main
6
- jobs:
7
- npm-publish:
8
- name: npm-publish
9
- runs-on: ubuntu-latest
10
- if: contains(github.event.head_commit.message, 'Release')
11
- steps:
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 DELETED
@@ -1,7 +0,0 @@
1
- {
2
- "semi": true,
3
- "singleQuote": false,
4
- "tabWidth": 2,
5
- "trailingComma": "es5",
6
- "printWidth": 80
7
- }
package/eslint.config.js DELETED
@@ -1,26 +0,0 @@
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
- ];