bedazzlr 0.2.6 → 0.2.7
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/lib/index.js +47 -9
- package/package.json +2 -2
package/lib/index.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
/* global document window console */
|
|
1
|
+
/* global document window console requestAnimationFrame */
|
|
2
2
|
|
|
3
3
|
import emptyStack from '@iter-tools/imm-stack';
|
|
4
4
|
import classNames from 'classnames';
|
|
5
|
-
import {
|
|
5
|
+
import { buildModule } from 'bablr/enhanceable';
|
|
6
6
|
|
|
7
7
|
import { CloseNodeTag, LiteralTag, OpenNodeTag, ReferenceTag } from '@bablr/agast-helpers/symbols';
|
|
8
8
|
import { printSelfClosingNodeTag } from '@bablr/agast-helpers/print';
|
|
@@ -16,7 +16,7 @@ import {
|
|
|
16
16
|
buildReferenceMatcher,
|
|
17
17
|
} from '@bablr/helpers/builders';
|
|
18
18
|
import { buildEmbeddedMatcher } from '@bablr/agast-vm-helpers/builders';
|
|
19
|
-
import { hoistTrivia } from '@bablr/agast-helpers/stream';
|
|
19
|
+
import { getStreamIterator, hoistTrivia, StreamIterable, wait } from '@bablr/agast-helpers/stream';
|
|
20
20
|
|
|
21
21
|
let anchorStyle =
|
|
22
22
|
'display: inline-block; position: relative; vertical-align: top; pointer-events: none;';
|
|
@@ -116,10 +116,34 @@ export const removeDocumentListeners = () => {
|
|
|
116
116
|
}
|
|
117
117
|
};
|
|
118
118
|
|
|
119
|
-
|
|
120
|
-
let
|
|
121
|
-
|
|
122
|
-
|
|
119
|
+
function* __chunkify(chunkSize, str) {
|
|
120
|
+
let i = -1;
|
|
121
|
+
for (const chr of str) {
|
|
122
|
+
if (++i === chunkSize) {
|
|
123
|
+
yield wait(new Promise((resolve) => requestAnimationFrame(resolve)));
|
|
124
|
+
i = -1;
|
|
125
|
+
}
|
|
126
|
+
yield chr;
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
const chunkify = (chunkSize, str) => {
|
|
131
|
+
return new StreamIterable(__chunkify(chunkSize, str));
|
|
132
|
+
};
|
|
133
|
+
|
|
134
|
+
let { streamParse } = buildModule();
|
|
135
|
+
|
|
136
|
+
function* __highlightCode(el, language, matcher, options = {}) {
|
|
137
|
+
let { chunkSize, bablr: bablrOptions = {} } = options;
|
|
138
|
+
let input = chunkSize ? chunkify(chunkSize, el.innerText) : el.innerText;
|
|
139
|
+
let tags = getStreamIterator(
|
|
140
|
+
hoistTrivia(
|
|
141
|
+
streamParse(language, matcher, input, null, {
|
|
142
|
+
...bablrOptions,
|
|
143
|
+
holdShiftedNodes: true,
|
|
144
|
+
}),
|
|
145
|
+
),
|
|
146
|
+
);
|
|
123
147
|
|
|
124
148
|
let open;
|
|
125
149
|
let referenceTag = null;
|
|
@@ -131,7 +155,13 @@ export const highlightCode = (el, language, matcher = language.defaultMatcher, o
|
|
|
131
155
|
|
|
132
156
|
let step = tags.next();
|
|
133
157
|
|
|
134
|
-
while (
|
|
158
|
+
while (true) {
|
|
159
|
+
if (step instanceof Promise) {
|
|
160
|
+
step = yield wait(step);
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
if (step.done) break;
|
|
164
|
+
|
|
135
165
|
let tag = step.value;
|
|
136
166
|
|
|
137
167
|
if (tag.type === OpenNodeTag) {
|
|
@@ -472,9 +502,13 @@ export const highlightCode = (el, language, matcher = language.defaultMatcher, o
|
|
|
472
502
|
store.selectionState = 'none';
|
|
473
503
|
}
|
|
474
504
|
});
|
|
505
|
+
}
|
|
506
|
+
|
|
507
|
+
export const highlightCode = (el, language, matcher = language.defaultMatcher, options) => {
|
|
508
|
+
return new StreamIterable(__highlightCode(el, language, matcher, options));
|
|
475
509
|
};
|
|
476
510
|
|
|
477
|
-
|
|
511
|
+
function* __highlightAll(languages, options) {
|
|
478
512
|
let codeBlocks = document.querySelectorAll('code');
|
|
479
513
|
|
|
480
514
|
for (let block of codeBlocks) {
|
|
@@ -508,4 +542,8 @@ export const highlightAll = (languages, options) => {
|
|
|
508
542
|
console.warn(e);
|
|
509
543
|
}
|
|
510
544
|
}
|
|
545
|
+
}
|
|
546
|
+
|
|
547
|
+
export const highlightAll = (languages, options) => {
|
|
548
|
+
return new StreamIterable(__highlightAll(languages, options));
|
|
511
549
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "bedazzlr",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.7",
|
|
4
4
|
"description": "Code block syntax highlighting using BABLR",
|
|
5
5
|
"author": "Conrad Buck<conartist6@gmail.com>",
|
|
6
6
|
"type": "module",
|
|
@@ -20,7 +20,7 @@
|
|
|
20
20
|
"@bablr/boot": "0.11.3",
|
|
21
21
|
"@bablr/helpers": "0.25.2",
|
|
22
22
|
"@iter-tools/imm-stack": "1.2.0",
|
|
23
|
-
"bablr": "0.11.
|
|
23
|
+
"bablr": "0.11.10",
|
|
24
24
|
"classnames": "2.5.1"
|
|
25
25
|
},
|
|
26
26
|
"devDependencies": {
|