bedazzlr 0.2.9 → 0.3.1

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.
Files changed (3) hide show
  1. package/README.md +1 -1
  2. package/lib/index.js +55 -49
  3. package/package.json +6 -7
package/README.md CHANGED
@@ -1,3 +1,3 @@
1
1
  ## bedazzlr
2
2
 
3
- Syntax highlighter for `<code />` blocks on webpages powered by BABLR
3
+ Documentation at https://docs.bablr.org/reference/bedazzlr
package/lib/index.js CHANGED
@@ -1,22 +1,22 @@
1
- /* global document window console requestAnimationFrame */
1
+ /* global document window console requestAnimationFrame IntersectionObserver */
2
2
 
3
3
  import emptyStack from '@iter-tools/imm-stack';
4
4
  import classNames from 'classnames';
5
- import { buildModule } from 'bablr/enhanceable';
5
+ import { streamParse } from 'bablr';
6
6
 
7
7
  import { CloseNodeTag, LiteralTag, OpenNodeTag, ReferenceTag } from '@bablr/agast-helpers/symbols';
8
8
  import { printSelfClosingNodeTag } from '@bablr/agast-helpers/print';
9
- import { buildOpenNodeTag, nodeFlags, tokenFlags } from '@bablr/agast-helpers/builders';
9
+ import { buildOpenNodeTag, nodeFlags, tokenFlags, parseTag } from '@bablr/agast-helpers/builders';
10
10
  import {
11
- buildTreeNodeMatcher,
12
- buildNodeFlags,
13
- buildTreeNodeMatcherOpen,
14
- buildPropertyMatcher,
15
- buildBoundNodeMatcher,
16
- buildReferenceMatcher,
17
- } from '@bablr/helpers/builders';
18
- import { buildEmbeddedMatcher } from '@bablr/agast-vm-helpers/builders';
19
- import { getStreamIterator, hoistTrivia, StreamIterable, wait } from '@bablr/agast-helpers/stream';
11
+ continue_,
12
+ getStreamIterator,
13
+ hoistTrivia,
14
+ StreamIterable,
15
+ wait,
16
+ } from '@bablr/agast-helpers/stream';
17
+ import { deepFreezeRecord, freezeRecord } from '@bablr/agast-helpers/object';
18
+ import { buildEmbeddedCallable } from '@bablr/agast-vm-helpers/builders';
19
+ import { m } from '@bablr/helpers/grammar';
20
20
 
21
21
  let anchorStyle =
22
22
  'display: inline-block; position: relative; vertical-align: top; pointer-events: none;';
@@ -131,17 +131,21 @@ const chunkify = (chunkSize, str) => {
131
131
  return new StreamIterable(__chunkify(chunkSize, str));
132
132
  };
133
133
 
134
- let { streamParse } = buildModule();
135
-
136
- function* __highlightCode(el, language, matcher, options = {}) {
134
+ function* __highlightCode(el, language, matcher, options = freezeRecord({})) {
137
135
  let { chunkSize, bablr: bablrOptions = {} } = options;
138
136
  let input = chunkSize ? chunkify(chunkSize, el.innerText) : el.innerText;
139
137
  let tags = getStreamIterator(
140
138
  hoistTrivia(
141
- streamParse(language, matcher, input, null, {
142
- ...bablrOptions,
143
- holdShiftedNodes: true,
144
- }),
139
+ streamParse(
140
+ language,
141
+ matcher,
142
+ input,
143
+ null,
144
+ deepFreezeRecord({
145
+ ...bablrOptions,
146
+ holdShiftedNodes: true,
147
+ }),
148
+ ),
145
149
  ),
146
150
  );
147
151
 
@@ -156,13 +160,14 @@ function* __highlightCode(el, language, matcher, options = {}) {
156
160
  let step = tags.next();
157
161
 
158
162
  while (true) {
159
- if (step instanceof Promise) {
160
- step = yield wait(step);
163
+ while (step === null || step instanceof Promise) {
164
+ if (step === null) yield continue_(), (step = tags.next());
165
+ if (step instanceof Promise) step = yield wait(step);
161
166
  }
162
167
 
163
168
  if (step.done) break;
164
169
 
165
- let tag = step.value;
170
+ let tag = parseTag(step.value);
166
171
 
167
172
  if (tag.type === OpenNodeTag) {
168
173
  range = range.cloneRange();
@@ -179,7 +184,7 @@ function* __highlightCode(el, language, matcher, options = {}) {
179
184
  open,
180
185
  range,
181
186
  referenceTag,
182
- // langs: stack.value.langs.concat(bindingTag?.value.segments ?? []),
187
+ // langs: stack.value.langs.push(bindingTag?.value ?? []),
183
188
  });
184
189
  }
185
190
 
@@ -511,36 +516,37 @@ export const highlightCode = (el, language, matcher = language.defaultMatcher, o
511
516
  function* __highlightAll(languages, options) {
512
517
  let codeBlocks = document.querySelectorAll('code');
513
518
 
514
- for (let block of codeBlocks) {
515
- let canonicalURL = block.getAttribute('bablr-lang');
516
- let language = languages.get(canonicalURL);
517
- let flags = block.getAttribute('bablr-ref-flags');
519
+ let intersectionChanged = (entries) => {
520
+ for (let entry of entries) {
521
+ let { target } = entry;
518
522
 
519
- let name = block.getAttribute('bablr-prod');
523
+ let canonicalURL = target.getAttribute('bablr-lang');
524
+ let language = languages.get(canonicalURL);
525
+ let flags = target.getAttribute('bablr-ref-flags');
520
526
 
521
- if (!language) continue;
522
- if (!name && !language.defaultMatcher) continue;
527
+ let name = target.getAttribute('bablr-prod');
523
528
 
524
- try {
525
- highlightCode(
526
- block,
527
- language,
528
- name
529
- ? buildEmbeddedMatcher(
530
- buildPropertyMatcher(
531
- buildReferenceMatcher('_', null, flags),
532
- buildBoundNodeMatcher(
533
- [],
534
- buildTreeNodeMatcher(buildTreeNodeMatcherOpen(buildNodeFlags(), null, name)),
535
- ),
536
- ),
537
- )
538
- : language.defaultMatcher,
539
- options,
540
- );
541
- } catch (e) {
542
- console.warn(e);
529
+ if (!language) continue;
530
+ if (!name && !language.defaultMatcher) continue;
531
+
532
+ try {
533
+ highlightCode(
534
+ target,
535
+ language,
536
+ name ? m`<${name}>` : language.defaultMatcher,
537
+
538
+ options,
539
+ );
540
+ } catch (e) {
541
+ console.warn(e);
542
+ }
543
543
  }
544
+ };
545
+
546
+ let io = new IntersectionObserver(intersectionChanged);
547
+
548
+ for (let block of codeBlocks) {
549
+ io.observe(block);
544
550
  }
545
551
  }
546
552
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bedazzlr",
3
- "version": "0.2.9",
3
+ "version": "0.3.1",
4
4
  "description": "Code block syntax highlighting using BABLR",
5
5
  "author": "Conrad Buck<conartist6@gmail.com>",
6
6
  "type": "module",
@@ -15,16 +15,15 @@
15
15
  "test": "mocha test/*.test.js"
16
16
  },
17
17
  "dependencies": {
18
- "@bablr/agast-helpers": "0.10.10",
19
- "@bablr/agast-vm-helpers": "0.10.5",
20
- "@bablr/boot": "0.11.4",
21
- "@bablr/helpers": "0.25.5",
18
+ "@bablr/agast-helpers": "0.11.3",
19
+ "@bablr/agast-vm-helpers": "0.11.3",
20
+ "@bablr/helpers": "0.26.4",
22
21
  "@iter-tools/imm-stack": "1.2.0",
23
- "bablr": "0.11.11",
22
+ "bablr": "0.12.5",
24
23
  "classnames": "2.5.1"
25
24
  },
26
25
  "devDependencies": {
27
- "@bablr/eslint-config-base": "github:bablr-lang/eslint-config-base#c97bfa4b3663f8378e9b3e42bb5a41e685406cf9",
26
+ "@bablr/eslint-config-base": "github:bablr-lang/eslint-config-base#23b94fb3bdfdbc3ac7ed9cb58d7979d8f07dce2b",
28
27
  "@qnighy/dedent": "0.1.1",
29
28
  "enhanced-resolve": "^5.12.0",
30
29
  "eslint": "^8.32.0",