@samuelbradshaw/highlight-helper-js 0.7.0 → 0.7.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.
@@ -1,5 +1,3 @@
1
- # From https://docs.github.com/en/actions/tutorials/publish-packages/publish-nodejs-packages#publishing-packages-to-the-npm-registry
2
-
3
1
  name: Publish Package to npmjs
4
2
  on:
5
3
  release:
@@ -8,15 +6,29 @@ jobs:
8
6
  build:
9
7
  runs-on: ubuntu-latest
10
8
  permissions:
11
- contents: read
9
+ contents: write
12
10
  id-token: write
13
11
  steps:
14
- - uses: actions/checkout@v5
15
- - uses: actions/setup-node@v4
12
+ - name: Checkout code
13
+ uses: actions/checkout@v5
14
+ - name: Get release version
15
+ id: get_version
16
+ run: echo "RELEASE_VERSION=${{ github.event.release.tag_name }}" >> $GITHUB_OUTPUT
17
+ - name: Update package.json
18
+ run: npm version "${{ steps.get_version.outputs.RELEASE_VERSION }}" --no-git-tag-version
19
+ - name: Commit package.json changes
20
+ uses: stefanzweifel/git-auto-commit-action@v6
21
+ with:
22
+ commit_message: "Update package.json version to ${{ steps.get_version.outputs.RELEASE_VERSION }}"
23
+ branch: main
24
+ - name: Set up Node.js
25
+ uses: actions/setup-node@v4
16
26
  with:
17
27
  node-version: '20.x'
18
28
  registry-url: 'https://registry.npmjs.org'
19
- - run: npm ci
20
- - run: npm publish --provenance --access public
29
+ - name: Install dependencies
30
+ run: npm ci
31
+ - name: Push to npmjs
32
+ run: npm publish --provenance --access public
21
33
  env:
22
- NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
34
+ NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
package/README.md CHANGED
@@ -15,6 +15,7 @@ An HTML demo page that shows basic functionality can be found here: [Highlight H
15
15
  ### Documentation:
16
16
  - [Known issues](#known-issues)
17
17
  - [Getting started](#getting-started)
18
+ - [Installation options](#installation-options)
18
19
  - [Methods, options, and custom events](#methods-options-and-custom-events)
19
20
  - [Methods](#methods)
20
21
  - [Options](#options)
@@ -42,7 +43,24 @@ In the source of demo.html, you’ll see CSS styles, followed by the HTML body,
42
43
  3. Sets up logic to call Highlight Helper methods when buttons on the demo page are tapped, and
43
44
  4. Sets up listeners to respond to custom event messages that come back from Highlight Helper.
44
45
 
45
- All of the available methods, options, and custom events are documented below.
46
+
47
+ ### <a name="installation-options"></a>Installation options
48
+
49
+ You can download highlight-helper.js from GitHub and reference it from your HTML file:
50
+ ```html
51
+ <script src="scripts/highlight-helper.js"></script>
52
+ ```
53
+
54
+ Or you can load it from [jsDelivr](https://www.jsdelivr.com/package/gh/samuelbradshaw/highlight-helper-js):
55
+ ```html
56
+ <script src="https://cdn.jsdelivr.net/gh/samuelbradshaw/highlight-helper-js@main/highlight-helper.min.js"></script>
57
+ ```
58
+
59
+ Or you can install it using [npm](https://www.npmjs.com/package/@samuelbradshaw/highlight-helper-js):
60
+ ```bash
61
+ % cd /your/project/folder
62
+ % npm i @samuelbradshaw/highlight-helper-js
63
+ ```
46
64
 
47
65
 
48
66
  ## <a name="methods-options-and-custom-events"></a>Methods, options, and custom events
@@ -3,6 +3,8 @@
3
3
  * https://github.com/samuelbradshaw/highlight-helper-js
4
4
  */
5
5
 
6
+ 'use strict';
7
+
6
8
  function Highlighter(options = hhDefaultOptions) {
7
9
  for (const key of Object.keys(hhDefaultOptions)) {
8
10
  options[key] = options[key] ?? hhDefaultOptions[key];
@@ -231,6 +233,7 @@ function Highlighter(options = hhDefaultOptions) {
231
233
  range.endContainer.splitText(range.endOffset);
232
234
  const textNodeIter = document.createNodeIterator(range.commonAncestorContainer, NodeFilter.SHOW_TEXT);
233
235
  const relevantTextNodes = [];
236
+ let node;
234
237
  while (node = textNodeIter.nextNode()) {
235
238
  if (range.intersectsNode(node) && node !== range.startContainer && node.textContent !== '' && !node.parentElement.closest('rt') && node.parentElement.closest(options.paragraphSelector)) relevantTextNodes.push(node);
236
239
  if (node === range.endContainer) break;
@@ -239,7 +242,7 @@ function Highlighter(options = hhDefaultOptions) {
239
242
  for (let tn = 0; tn < relevantTextNodes.length; tn++) {
240
243
  const textNode = relevantTextNodes[tn];
241
244
  if (textNode.parentElement.dataset.highlightId) {
242
- overlappingHighlightIds.add(textNode.parentElement.dataset.highlightId)
245
+ overlappingHighlightIds.add(textNode.parentElement.dataset.highlightId);
243
246
  }
244
247
  const styledMark = document.createElement('mark');
245
248
  styledMark.dataset.highlightId = highlightId;
@@ -260,6 +263,7 @@ function Highlighter(options = hhDefaultOptions) {
260
263
  } else {
261
264
  // Draw highlights with Custom Highlight API
262
265
  if (options.drawingMode === 'highlight-api' && supportsHighlightApi) {
266
+ let highlightObj;
263
267
  if (CSS.highlights.has(highlightId)) {
264
268
  highlightObj = CSS.highlights.get(highlightId);
265
269
  highlightObj.clear();
@@ -302,6 +306,7 @@ function Highlighter(options = hhDefaultOptions) {
302
306
  template.innerHTML = htmlString;
303
307
  let htmlElement = template.content.firstChild;
304
308
 
309
+ let node;
305
310
  const textNodeIter = document.createNodeIterator(htmlElement, NodeFilter.SHOW_TEXT);
306
311
  while (node = textNodeIter.nextNode()) node.parentNode.removeChild(node);
307
312
  range.insertNode(htmlElement);
@@ -323,8 +328,8 @@ function Highlighter(options = hhDefaultOptions) {
323
328
  // Create a new highlight, or update an existing highlight when it changes
324
329
  this.createOrUpdateHighlight = (attributes = {}, triggeredByUserAction = true) => {
325
330
  let highlightId = attributes.highlightId ?? activeHighlightId ?? options.highlightIdFunction();
326
- appearanceChanges = [];
327
- boundsChanges = [];
331
+ const appearanceChanges = [];
332
+ const boundsChanges = [];
328
333
 
329
334
  let isNewHighlight, oldHighlightInfo;
330
335
  if (highlightsById.hasOwnProperty(highlightId)) {
@@ -541,7 +546,7 @@ function Highlighter(options = hhDefaultOptions) {
541
546
  // Get info for specified highlights, or all highlights on the page
542
547
  this.getHighlightInfo = (highlightIds = Object.keys(highlightsById), paragraphId = null) => {
543
548
  let filteredHighlights = []
544
- for (highlightId of highlightIds) {
549
+ for (const highlightId of highlightIds) {
545
550
  const highlightInfo = highlightsById[highlightId];
546
551
  if (!paragraphId || paragraphId === highlightInfo.startParagraphId) {
547
552
  filteredHighlights.push(highlightInfo);
@@ -1272,6 +1277,7 @@ function Highlighter(options = hhDefaultOptions) {
1272
1277
  // Get line positions (bottom edge of each line)
1273
1278
  let linePositions = new Set();
1274
1279
  let lineWalker = document.createTreeWalker(paragraph, NodeFilter.SHOW_TEXT);
1280
+ let textNode;
1275
1281
  while (textNode = lineWalker.nextNode()) {
1276
1282
  const computedStyle = window.getComputedStyle(textNode.parentElement);
1277
1283
  const lineHeight = parseInt((computedStyle.lineHeight === 'normal' ? computedStyle.fontSize : computedStyle.lineHeight).replace('px', ''));
@@ -1302,8 +1308,8 @@ function Highlighter(options = hhDefaultOptions) {
1302
1308
  mergedRect.width = rect.width;
1303
1309
  }
1304
1310
  // Process then remove rects that apply to the current line
1305
- minLeft = Math.min(mergedRect.x, rect.x);
1306
- maxRight = Math.max(mergedRect.right, rect.right);
1311
+ const minLeft = Math.min(mergedRect.x, rect.x);
1312
+ const maxRight = Math.max(mergedRect.right, rect.right);
1307
1313
  mergedRect.width = maxRight - minLeft;
1308
1314
  mergedRect.x = minLeft;
1309
1315
  unmergedRects.splice(r, 1); r--;
@@ -1447,3 +1453,6 @@ let hhDefaultOptions = {
1447
1453
  }
1448
1454
 
1449
1455
  console.info('Highlighter loaded');
1456
+
1457
+ // Make Highlighter available to ES module (highlight-helper.mjs)
1458
+ window.Highlighter = Highlighter;
@@ -0,0 +1,3 @@
1
+ await import('./highlight-helper.js');
2
+ const Highlighter = window.Highlighter;
3
+ export { Highlighter };
package/package.json CHANGED
@@ -1,8 +1,9 @@
1
1
  {
2
2
  "name": "@samuelbradshaw/highlight-helper-js",
3
- "version": "0.7.0",
3
+ "version": "0.7.2",
4
4
  "description": "JavaScript tool that enables highlighting or underlining text in an HTML page.",
5
- "main": "highlight-helper.js",
5
+ "type": "module",
6
+ "main": "highlight-helper.mjs",
6
7
  "repository": {
7
8
  "type": "git",
8
9
  "url": "git+https://github.com/samuelbradshaw/highlight-helper-js.git"