@contentpilot/read-more 1.0.7 → 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 ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2019 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
@@ -1,23 +1,87 @@
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
25
+
26
+ Without jQuery, it auto-initializes on `.entry-content` on `DOMContentLoaded`.
27
+ With jQuery, nothing runs until you call `.cpReadMore()`.
6
28
 
7
- `npm install @contentpilot/read-more --save-dev`
29
+ **Initialize:**
8
30
 
9
- ### Require JS file in task runner
31
+ ```javascript
32
+ import { init } from "@contentpilot/read-more";
10
33
 
11
- `./node_modules/@contentpilot/read-more/index.js`
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
+ ```
12
40
 
13
- ### Bind to content area
41
+ **jQuery Compatibility:**
14
42
 
15
- `$('.entry-content').cpReadMore();`
43
+ ```javascript
44
+ $(".entry-content").cpReadMore();
16
45
 
17
- ### Publish to NPM
46
+ // With options
47
+ $(".entry-content").cpReadMore({
48
+ target: '[id^="more-"]',
49
+ unwrap: true,
50
+ replace: true,
51
+ moreLabel: "More",
52
+ moreCSS: "button clean",
53
+ lessLabel: "Less",
54
+ lessCSS: "button clean",
55
+ hideClass: "display-none",
56
+ });
57
+ ```
58
+
59
+ The 1.x options still work exactly as they did in 1.x. Sites using the jQuery
60
+ plugin need no code changes.
61
+
62
+ ### Options
63
+
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`)
71
+
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`)
76
+
77
+ ## Publish to NPM
18
78
 
19
79
  To update this repository on NPM using the GitHub action,
20
80
 
21
81
  1. Bump the version number in `package.json`
22
- 2. Commit with message pattern of `Release x.x.x`
23
- 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,94 +1,241 @@
1
1
  /**
2
- *
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
2
+ * Read More
3
+ * Find and convert the WordPress <!--more--> read more tag to toggle all content below
6
4
  *
7
- * @package Read More
8
- * @author Content Pilot, Dallas, Texas
9
- * @link https://www.contentpilot.com
10
- * @copyright Copyright © 2018 Content Pilot
11
- * @license GPL-2.0+
5
+ * @author Content Pilot
6
+ * @copyright Copyright (c) 2019 Content Pilot
7
+ * @license MIT
12
8
  */
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
9
 
39
- function __buttonLess() {
40
- return __button(
41
- settings.lessLabel,
42
- settings.lessCSS
43
- );
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
+ }
45
+
46
+ createButtonFromHTML(html) {
47
+ const template = document.createElement("template");
48
+ template.innerHTML = html.trim();
49
+ return template.content.firstElementChild;
50
+ }
51
+
52
+ createMoreButton() {
53
+ return this.createButtonFromHTML(this.settings.more);
54
+ }
55
+
56
+ createLessButton() {
57
+ return this.createButtonFromHTML(this.settings.less);
58
+ }
59
+
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`;
64
+
65
+ requestAnimationFrame(() => {
66
+ element.style.height = "0";
67
+ });
68
+
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
+ }
85
+
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`;
93
+
94
+ requestAnimationFrame(() => {
95
+ element.style.height = height + "px";
96
+ });
97
+
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
+ );
111
+ }
112
+
113
+ fadeIn(element) {
114
+ element.style.opacity = "0";
115
+ element.style.display = "";
116
+ element.style.transition = "opacity 0.5s ease";
117
+
118
+ requestAnimationFrame(() => {
119
+ element.style.opacity = "1";
120
+ });
121
+
122
+ setTimeout(() => {
123
+ element.style.opacity = "";
124
+ element.style.transition = "";
125
+ }, 500);
126
+ }
127
+
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
+
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
+ }
150
+
151
+ // Find first instance of read-more button
152
+ let rmBtn = this.element.querySelector("button.read-more");
153
+
154
+ if (!rmBtn) {
155
+ return;
156
+ }
157
+
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");
44
181
  }
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 ));
182
+ }
183
+
184
+ // Create and setup Less button
185
+ const contentWrapper = this.element.querySelector(".read-more-content");
186
+
187
+ if (!contentWrapper) {
188
+ return;
189
+ }
190
+
191
+ rmBtn.setAttribute("aria-expanded", "false");
192
+ rmBtn.setAttribute("aria-controls", contentWrapper.id);
193
+
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
+ });
201
+ });
202
+
203
+ contentWrapper.appendChild(lessButton);
204
+
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
+ });
212
+ });
213
+ }
214
+ }
215
+
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
+ }
224
+
225
+ function init(selector = ".entry-content", options = {}) {
226
+ const contentElements = document.querySelectorAll(selector);
227
+ contentElements.forEach((element) => {
228
+ new ReadMore(element, options);
229
+ });
230
+ }
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,22 +1,50 @@
1
1
  {
2
2
  "name": "@contentpilot/read-more",
3
- "version": "1.0.7",
4
- "description": "Find and convet the WordPress <!--more--> read more tag to toggle all content below",
3
+ "version": "2.1.2",
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
+ ],
18
+ "type": "module",
19
+ "scripts": {
20
+ "lint": "eslint index.js index.esm.js",
21
+ "lint:fix": "eslint index.js index.esm.js --fix",
22
+ "format": "prettier --write .",
23
+ "format:check": "prettier --check ."
24
+ },
6
25
  "publishConfig": {
7
26
  "access": "public"
8
27
  },
9
28
  "repository": {
10
29
  "type": "git",
11
- "url": "https://github.com/content-pilot/read-more"
30
+ "url": "https://github.com/content-pilot/read-more.git"
12
31
  },
13
32
  "keywords": [
14
33
  "read",
15
34
  "more",
16
35
  "WordPress",
17
36
  "editor",
18
- "wysiwyg"
37
+ "wysiwyg",
38
+ "Content",
39
+ "Pilot",
40
+ "Content Pilot"
19
41
  ],
20
42
  "author": "Content Pilot",
21
- "license": "MIT"
43
+ "license": "MIT",
44
+ "devDependencies": {
45
+ "@eslint/js": "^10.0.1",
46
+ "eslint": "^10.8.0",
47
+ "eslint-config-prettier": "^10.1.8",
48
+ "prettier": "^3.9.6"
49
+ }
22
50
  }
@@ -1,26 +0,0 @@
1
- name: npm-publish
2
- on:
3
- push:
4
- branches:
5
- - master
6
- jobs:
7
- npm-publish:
8
- name: npm-publish
9
- runs-on: ubuntu-latest
10
- 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