@allons-y/webc-code-sample 0.0.0-stage → 0.1.0-beta.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.
- package/LICENSE +385 -0
- package/README.md +126 -2
- package/components/norn-code-block.webc +306 -0
- package/index.js +100 -0
- package/markdown.js +25 -0
- package/package.json +56 -5
- package/tokenize.js +231 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,385 @@
|
|
|
1
|
+
Mozilla Public License Version 2.0
|
|
2
|
+
==================================
|
|
3
|
+
|
|
4
|
+
1. Definitions
|
|
5
|
+
|
|
6
|
+
--------------
|
|
7
|
+
|
|
8
|
+
1.1. "Contributor"
|
|
9
|
+
means each individual or legal entity that creates, contributes to
|
|
10
|
+
the creation of, or owns Covered Software.
|
|
11
|
+
|
|
12
|
+
1.2. "Contributor Version"
|
|
13
|
+
means the combination of the Contributions of others (if any) used
|
|
14
|
+
by a Contributor and that particular Contributor's Contribution.
|
|
15
|
+
|
|
16
|
+
1.3. "Contribution"
|
|
17
|
+
means Covered Software of a particular Contributor.
|
|
18
|
+
|
|
19
|
+
1.4. "Covered Software"
|
|
20
|
+
means Source Code Form to which the initial Contributor has attached
|
|
21
|
+
the notice in Exhibit A, the Executable Form of such Source Code
|
|
22
|
+
Form, and Modifications of such Source Code Form, in each case
|
|
23
|
+
including portions thereof.
|
|
24
|
+
|
|
25
|
+
1.5. "Incompatible With Secondary Licenses"
|
|
26
|
+
means
|
|
27
|
+
|
|
28
|
+
(a) that the initial Contributor has attached the notice described
|
|
29
|
+
in Exhibit B to the Covered Software; or
|
|
30
|
+
|
|
31
|
+
(b) that the Covered Software was made available under the terms of
|
|
32
|
+
version 1.1 or earlier of the License, but not also under the
|
|
33
|
+
terms of a Secondary License.
|
|
34
|
+
|
|
35
|
+
1.6. "Executable Form"
|
|
36
|
+
means any form of the work other than Source Code Form.
|
|
37
|
+
|
|
38
|
+
1.7. "Larger Work"
|
|
39
|
+
means a work that combines Covered Software with other material, in
|
|
40
|
+
a separate file or files, that is not Covered Software.
|
|
41
|
+
|
|
42
|
+
1.8. "License"
|
|
43
|
+
means this document.
|
|
44
|
+
|
|
45
|
+
1.9. "Licensable"
|
|
46
|
+
means having the right to grant, to the maximum extent possible,
|
|
47
|
+
whether at the time of the initial grant or subsequently, any and
|
|
48
|
+
all of the rights conveyed by this License.
|
|
49
|
+
|
|
50
|
+
1.10. "Modifications"
|
|
51
|
+
means any of the following:
|
|
52
|
+
|
|
53
|
+
(a) any file in Source Code Form that results from an addition to,
|
|
54
|
+
deletion from, or modification of the contents of Covered
|
|
55
|
+
Software; or
|
|
56
|
+
|
|
57
|
+
(b) any new file in Source Code Form that contains any Covered
|
|
58
|
+
Software.
|
|
59
|
+
|
|
60
|
+
1.11. "Patent Claims" of a Contributor
|
|
61
|
+
means any patent claim(s), including without limitation, method,
|
|
62
|
+
process, and apparatus claims, in any patent Licensable by such
|
|
63
|
+
Contributor that would be infringed, but for the grant of the
|
|
64
|
+
License, by the making, using, selling, offering for sale, having
|
|
65
|
+
made, import, or transfer of either its Contributions or its
|
|
66
|
+
Contributor Version.
|
|
67
|
+
|
|
68
|
+
1.12. "Secondary License"
|
|
69
|
+
means either the GNU General Public License, Version 2.0, the GNU
|
|
70
|
+
Lesser General Public License, Version 2.1, the GNU Affero General
|
|
71
|
+
Public License, Version 3.0, or any later versions of those
|
|
72
|
+
licenses.
|
|
73
|
+
|
|
74
|
+
1.13. "Source Code Form"
|
|
75
|
+
means the form of the work preferred for making modifications.
|
|
76
|
+
|
|
77
|
+
1.14. "You" (or "Your")
|
|
78
|
+
means an individual or a legal entity exercising rights under this
|
|
79
|
+
License. For legal entities, "You" includes any entity that
|
|
80
|
+
controls, is controlled by, or is under common control with You. For
|
|
81
|
+
purposes of this definition, "control" means (a) the power, direct
|
|
82
|
+
or indirect, to cause the direction or management of such entity,
|
|
83
|
+
whether by contract or otherwise, or (b) ownership of more than
|
|
84
|
+
fifty percent (50%) of the outstanding shares or beneficial
|
|
85
|
+
ownership of such entity.
|
|
86
|
+
|
|
87
|
+
1. License Grants and Conditions
|
|
88
|
+
|
|
89
|
+
--------------------------------
|
|
90
|
+
|
|
91
|
+
2.1. Grants
|
|
92
|
+
|
|
93
|
+
Each Contributor hereby grants You a world-wide, royalty-free,
|
|
94
|
+
non-exclusive license:
|
|
95
|
+
|
|
96
|
+
(a) under intellectual property rights (other than patent or trademark)
|
|
97
|
+
Licensable by such Contributor to use, reproduce, make available,
|
|
98
|
+
modify, display, perform, distribute, and otherwise exploit its
|
|
99
|
+
Contributions, either on an unmodified basis, with Modifications, or
|
|
100
|
+
as part of a Larger Work; and
|
|
101
|
+
|
|
102
|
+
(b) under Patent Claims of such Contributor to make, use, sell, offer
|
|
103
|
+
for sale, have made, import, and otherwise transfer either its
|
|
104
|
+
Contributions or its Contributor Version.
|
|
105
|
+
|
|
106
|
+
2.2. Effective Date
|
|
107
|
+
|
|
108
|
+
The licenses granted in Section 2.1 with respect to any Contribution
|
|
109
|
+
become effective for each Contribution on the date the Contributor first
|
|
110
|
+
distributes such Contribution.
|
|
111
|
+
|
|
112
|
+
2.3. Limitations on Grant Scope
|
|
113
|
+
|
|
114
|
+
The licenses granted in this Section 2 are the only rights granted under
|
|
115
|
+
this License. No additional rights or licenses will be implied from the
|
|
116
|
+
distribution or licensing of Covered Software under this License.
|
|
117
|
+
Notwithstanding Section 2.1(b) above, no patent license is granted by a
|
|
118
|
+
Contributor:
|
|
119
|
+
|
|
120
|
+
(a) for any code that a Contributor has removed from Covered Software;
|
|
121
|
+
or
|
|
122
|
+
|
|
123
|
+
(b) for infringements caused by: (i) Your and any other third party's
|
|
124
|
+
modifications of Covered Software, or (ii) the combination of its
|
|
125
|
+
Contributions with other software (except as part of its Contributor
|
|
126
|
+
Version); or
|
|
127
|
+
|
|
128
|
+
(c) under Patent Claims infringed by Covered Software in the absence of
|
|
129
|
+
its Contributions.
|
|
130
|
+
|
|
131
|
+
This License does not grant any rights in the trademarks, service marks,
|
|
132
|
+
or logos of any Contributor (except as may be necessary to comply with
|
|
133
|
+
the notice requirements in Section 3.4).
|
|
134
|
+
|
|
135
|
+
2.4. Subsequent Licenses
|
|
136
|
+
|
|
137
|
+
No Contributor makes additional grants as a result of Your choice to
|
|
138
|
+
distribute the Covered Software under a subsequent version of this
|
|
139
|
+
License (see Section 10.2) or under the terms of a Secondary License (if
|
|
140
|
+
permitted under the terms of Section 3.3).
|
|
141
|
+
|
|
142
|
+
2.5. Representation
|
|
143
|
+
|
|
144
|
+
Each Contributor represents that the Contributor believes its
|
|
145
|
+
Contributions are its original creation(s) or it has sufficient rights
|
|
146
|
+
to grant the rights to its Contributions conveyed by this License.
|
|
147
|
+
|
|
148
|
+
2.6. Fair Use
|
|
149
|
+
|
|
150
|
+
This License is not intended to limit any rights You have under
|
|
151
|
+
applicable copyright doctrines of fair use, fair dealing, or other
|
|
152
|
+
equivalents.
|
|
153
|
+
|
|
154
|
+
2.7. Conditions
|
|
155
|
+
|
|
156
|
+
Sections 3.1, 3.2, 3.3, and 3.4 are conditions of the licenses granted
|
|
157
|
+
in Section 2.1.
|
|
158
|
+
|
|
159
|
+
1. Responsibilities
|
|
160
|
+
|
|
161
|
+
-------------------
|
|
162
|
+
|
|
163
|
+
3.1. Distribution of Source Form
|
|
164
|
+
|
|
165
|
+
All distribution of Covered Software in Source Code Form, including any
|
|
166
|
+
Modifications that You create or to which You contribute, must be under
|
|
167
|
+
the terms of this License. You must inform recipients that the Source
|
|
168
|
+
Code Form of the Covered Software is governed by the terms of this
|
|
169
|
+
License, and how they can obtain a copy of this License. You may not
|
|
170
|
+
attempt to alter or restrict the recipients' rights in the Source Code
|
|
171
|
+
Form.
|
|
172
|
+
|
|
173
|
+
3.2. Distribution of Executable Form
|
|
174
|
+
|
|
175
|
+
If You distribute Covered Software in Executable Form then:
|
|
176
|
+
|
|
177
|
+
(a) such Covered Software must also be made available in Source Code
|
|
178
|
+
Form, as described in Section 3.1, and You must inform recipients of
|
|
179
|
+
the Executable Form how they can obtain a copy of such Source Code
|
|
180
|
+
Form by reasonable means in a timely manner, at a charge no more
|
|
181
|
+
than the cost of distribution to the recipient; and
|
|
182
|
+
|
|
183
|
+
(b) You may distribute such Executable Form under the terms of this
|
|
184
|
+
License, or sublicense it under different terms, provided that the
|
|
185
|
+
license for the Executable Form does not attempt to limit or alter
|
|
186
|
+
the recipients' rights in the Source Code Form under this License.
|
|
187
|
+
|
|
188
|
+
3.3. Distribution of a Larger Work
|
|
189
|
+
|
|
190
|
+
You may create and distribute a Larger Work under terms of Your choice,
|
|
191
|
+
provided that You also comply with the requirements of this License for
|
|
192
|
+
the Covered Software. If the Larger Work is a combination of Covered
|
|
193
|
+
Software with a work governed by one or more Secondary Licenses, and the
|
|
194
|
+
Covered Software is not Incompatible With Secondary Licenses, this
|
|
195
|
+
License permits You to additionally distribute such Covered Software
|
|
196
|
+
under the terms of such Secondary License(s), so that the recipient of
|
|
197
|
+
the Larger Work may, at their option, further distribute the Covered
|
|
198
|
+
Software under the terms of either this License or such Secondary
|
|
199
|
+
License(s).
|
|
200
|
+
|
|
201
|
+
3.4. Notices
|
|
202
|
+
|
|
203
|
+
You may not remove or alter the substance of any license notices
|
|
204
|
+
(including copyright notices, patent notices, disclaimers of warranty,
|
|
205
|
+
or limitations of liability) contained within the Source Code Form of
|
|
206
|
+
the Covered Software, except that You may alter any license notices to
|
|
207
|
+
the extent required to remedy known factual inaccuracies.
|
|
208
|
+
|
|
209
|
+
3.5. Application of Additional Terms
|
|
210
|
+
|
|
211
|
+
You may choose to offer, and to charge a fee for, warranty, support,
|
|
212
|
+
indemnity or liability obligations to one or more recipients of Covered
|
|
213
|
+
Software. However, You may do so only on Your own behalf, and not on
|
|
214
|
+
behalf of any Contributor. You must make it absolutely clear that any
|
|
215
|
+
such warranty, support, indemnity, or liability obligation is offered by
|
|
216
|
+
You alone, and You hereby agree to indemnify every Contributor for any
|
|
217
|
+
liability incurred by such Contributor as a result of warranty, support,
|
|
218
|
+
indemnity or liability terms You offer. You may include additional
|
|
219
|
+
disclaimers of warranty and limitations of liability specific to any
|
|
220
|
+
jurisdiction.
|
|
221
|
+
|
|
222
|
+
1. Inability to Comply Due to Statute or Regulation
|
|
223
|
+
|
|
224
|
+
---------------------------------------------------
|
|
225
|
+
|
|
226
|
+
If it is impossible for You to comply with any of the terms of this
|
|
227
|
+
License with respect to some or all of the Covered Software due to
|
|
228
|
+
statute, judicial order, or regulation then You must: (a) comply with
|
|
229
|
+
the terms of this License to the maximum extent possible; and (b)
|
|
230
|
+
describe the limitations and the code they affect. Such description must
|
|
231
|
+
be placed in a text file included with all distributions of the Covered
|
|
232
|
+
Software under this License. Except to the extent prohibited by statute
|
|
233
|
+
or regulation, such description must be sufficiently detailed for a
|
|
234
|
+
recipient of ordinary skill to be able to understand it.
|
|
235
|
+
|
|
236
|
+
1. Termination
|
|
237
|
+
|
|
238
|
+
--------------
|
|
239
|
+
|
|
240
|
+
5.1. The rights granted under this License will terminate automatically
|
|
241
|
+
if You fail to comply with any of its terms. However, if You become
|
|
242
|
+
compliant, then the rights granted under this License from a particular
|
|
243
|
+
Contributor are reinstated (a) provisionally, unless and until such
|
|
244
|
+
Contributor explicitly and finally terminates Your grants, and (b) on an
|
|
245
|
+
ongoing basis, if such Contributor fails to notify You of the
|
|
246
|
+
non-compliance by some reasonable means prior to 60 days after You have
|
|
247
|
+
come back into compliance. Moreover, Your grants from a particular
|
|
248
|
+
Contributor are reinstated on an ongoing basis if such Contributor
|
|
249
|
+
notifies You of the non-compliance by some reasonable means, this is the
|
|
250
|
+
first time You have received notice of non-compliance with this License
|
|
251
|
+
from such Contributor, and You become compliant prior to 30 days after
|
|
252
|
+
Your receipt of the notice.
|
|
253
|
+
|
|
254
|
+
5.2. If You initiate litigation against any entity by asserting a patent
|
|
255
|
+
infringement claim (excluding declaratory judgment actions,
|
|
256
|
+
counter-claims, and cross-claims) alleging that a Contributor Version
|
|
257
|
+
directly or indirectly infringes any patent, then the rights granted to
|
|
258
|
+
You by any and all Contributors for the Covered Software under Section
|
|
259
|
+
2.1 of this License shall terminate.
|
|
260
|
+
|
|
261
|
+
5.3. In the event of termination under Sections 5.1 or 5.2 above, all
|
|
262
|
+
end user license agreements (excluding distributors and resellers) which
|
|
263
|
+
have been validly granted by You or Your distributors under this License
|
|
264
|
+
prior to termination shall survive termination.
|
|
265
|
+
|
|
266
|
+
************************************************************************
|
|
267
|
+
|
|
268
|
+
* *
|
|
269
|
+
* 1. Disclaimer of Warranty *
|
|
270
|
+
* ------------------------- *
|
|
271
|
+
* *
|
|
272
|
+
* Covered Software is provided under this License on an "as is" *
|
|
273
|
+
* basis, without warranty of any kind, either expressed, implied, or *
|
|
274
|
+
* statutory, including, without limitation, warranties that the *
|
|
275
|
+
* Covered Software is free of defects, merchantable, fit for a *
|
|
276
|
+
* particular purpose or non-infringing. The entire risk as to the *
|
|
277
|
+
* quality and performance of the Covered Software is with You. *
|
|
278
|
+
* Should any Covered Software prove defective in any respect, You *
|
|
279
|
+
* (not any Contributor) assume the cost of any necessary servicing, *
|
|
280
|
+
* repair, or correction. This disclaimer of warranty constitutes an *
|
|
281
|
+
* essential part of this License. No use of any Covered Software is *
|
|
282
|
+
* authorized under this License except under this disclaimer. *
|
|
283
|
+
* *
|
|
284
|
+
|
|
285
|
+
************************************************************************
|
|
286
|
+
|
|
287
|
+
************************************************************************
|
|
288
|
+
|
|
289
|
+
* *
|
|
290
|
+
* 1. Limitation of Liability *
|
|
291
|
+
* -------------------------- *
|
|
292
|
+
* *
|
|
293
|
+
* Under no circumstances and under no legal theory, whether tort *
|
|
294
|
+
* (including negligence), contract, or otherwise, shall any *
|
|
295
|
+
* Contributor, or anyone who distributes Covered Software as *
|
|
296
|
+
* permitted above, be liable to You for any direct, indirect, *
|
|
297
|
+
* special, incidental, or consequential damages of any character *
|
|
298
|
+
* including, without limitation, damages for lost profits, loss of *
|
|
299
|
+
* goodwill, work stoppage, computer failure or malfunction, or any *
|
|
300
|
+
* and all other commercial damages or losses, even if such party *
|
|
301
|
+
* shall have been informed of the possibility of such damages. This *
|
|
302
|
+
* limitation of liability shall not apply to liability for death or *
|
|
303
|
+
* personal injury resulting from such party's negligence to the *
|
|
304
|
+
* extent applicable law prohibits such limitation. Some *
|
|
305
|
+
* jurisdictions do not allow the exclusion or limitation of *
|
|
306
|
+
* incidental or consequential damages, so this exclusion and *
|
|
307
|
+
* limitation may not apply to You. *
|
|
308
|
+
* *
|
|
309
|
+
|
|
310
|
+
************************************************************************
|
|
311
|
+
|
|
312
|
+
1. Litigation
|
|
313
|
+
|
|
314
|
+
-------------
|
|
315
|
+
|
|
316
|
+
Any litigation relating to this License may be brought only in the
|
|
317
|
+
courts of a jurisdiction where the defendant maintains its principal
|
|
318
|
+
place of business and such litigation shall be governed by laws of that
|
|
319
|
+
jurisdiction, without reference to its conflict-of-law provisions.
|
|
320
|
+
Nothing in this Section shall prevent a party's ability to bring
|
|
321
|
+
cross-claims or counter-claims.
|
|
322
|
+
|
|
323
|
+
1. Miscellaneous
|
|
324
|
+
|
|
325
|
+
----------------
|
|
326
|
+
|
|
327
|
+
This License represents the complete agreement concerning the subject
|
|
328
|
+
matter hereof. If any provision of this License is held to be
|
|
329
|
+
unenforceable, such provision shall be reformed only to the extent
|
|
330
|
+
necessary to make it enforceable. Any law or regulation which provides
|
|
331
|
+
that the language of a contract shall be construed against the drafter
|
|
332
|
+
shall not be used to construe this License against a Contributor.
|
|
333
|
+
|
|
334
|
+
1. Versions of the License
|
|
335
|
+
|
|
336
|
+
---------------------------
|
|
337
|
+
|
|
338
|
+
10.1. New Versions
|
|
339
|
+
|
|
340
|
+
Mozilla Foundation is the license steward. Except as provided in Section
|
|
341
|
+
10.3, no one other than the license steward has the right to modify or
|
|
342
|
+
publish new versions of this License. Each version will be given a
|
|
343
|
+
distinguishing version number.
|
|
344
|
+
|
|
345
|
+
10.2. Effect of New Versions
|
|
346
|
+
|
|
347
|
+
You may distribute the Covered Software under the terms of the version
|
|
348
|
+
of the License under which You originally received the Covered Software,
|
|
349
|
+
or under the terms of any subsequent version published by the license
|
|
350
|
+
steward.
|
|
351
|
+
|
|
352
|
+
10.3. Modified Versions
|
|
353
|
+
|
|
354
|
+
If you create software not governed by this License, and you want to
|
|
355
|
+
create a new license for such software, you may create and use a
|
|
356
|
+
modified version of this License if you rename the license and remove
|
|
357
|
+
any references to the name of the license steward (except to note that
|
|
358
|
+
such modified license differs from this License).
|
|
359
|
+
|
|
360
|
+
10.4. Distributing Source Code Form that is Incompatible With Secondary
|
|
361
|
+
Licenses
|
|
362
|
+
|
|
363
|
+
If You choose to distribute Source Code Form that is Incompatible With
|
|
364
|
+
Secondary Licenses under the terms of this version of the License, the
|
|
365
|
+
notice described in Exhibit B of this License must be attached.
|
|
366
|
+
|
|
367
|
+
Exhibit A - Source Code Form License Notice
|
|
368
|
+
-------------------------------------------
|
|
369
|
+
|
|
370
|
+
This Source Code Form is subject to the terms of the Mozilla Public
|
|
371
|
+
License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
372
|
+
file, You can obtain one at <http://mozilla.org/MPL/2.0/>.
|
|
373
|
+
|
|
374
|
+
If it is not possible or desirable to put the notice in a particular
|
|
375
|
+
file, then You may include the notice in a location (such as a LICENSE
|
|
376
|
+
file in a relevant directory) where a recipient would be likely to look
|
|
377
|
+
for such a notice.
|
|
378
|
+
|
|
379
|
+
You may add additional accurate notices of copyright ownership.
|
|
380
|
+
|
|
381
|
+
Exhibit B - "Incompatible With Secondary Licenses" Notice
|
|
382
|
+
---------------------------------------------------------
|
|
383
|
+
|
|
384
|
+
This Source Code Form is "Incompatible With Secondary Licenses", as
|
|
385
|
+
defined by the Mozilla Public License, v. 2.0.
|
package/README.md
CHANGED
|
@@ -1,3 +1,127 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @allons-y/webc-code-sample
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Part of [Norn](https://github.com/allonsy-studio/webc-resources#readme). `norn-code-block` WebC component with build-time syntax highlighting. Shiki runs at build; the browser receives token ranges for the CSS Custom Highlight API and a copy button, with no highlighter shipped.
|
|
4
|
+
|
|
5
|
+
See it in the [component preview](https://allonsy-studio.github.io/webc-resources/).
|
|
6
|
+
|
|
7
|
+
## Status
|
|
8
|
+
|
|
9
|
+
Beta. Versions are `0.1.0-beta.N` until 1.0.
|
|
10
|
+
|
|
11
|
+
## Install
|
|
12
|
+
|
|
13
|
+
```sh
|
|
14
|
+
npm install --save-dev @11ty/eleventy @11ty/eleventy-plugin-webc @allons-y/webc-patterns @allons-y/webc-code-sample
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
Node 22 or later. Evergreen browsers from about 2024. Features newer than that, such as the CSS Custom Highlight API, degrade gracefully: the code stays readable plain text.
|
|
18
|
+
|
|
19
|
+
The package registers nothing itself. It is an optional peer dependency of `@allons-y/webc-patterns`: with it installed, the Norn plugin registers `norn-code-block`, the `nornTokenRanges` helper, and the Markdown add-on, and the Norn layouts show their samples with it. The copy button is the patterns package's `norn-copy-button`.
|
|
20
|
+
|
|
21
|
+
## Usage
|
|
22
|
+
|
|
23
|
+
Install it beside the patterns package and add the Norn plugin (or a theme that adds it, such as `@allons-y/webc-oss-site`). There is nothing else to register:
|
|
24
|
+
|
|
25
|
+
```js
|
|
26
|
+
import norn from "@allons-y/webc-patterns/plugin";
|
|
27
|
+
|
|
28
|
+
export default function (config) {
|
|
29
|
+
config.addPlugin(norn, { props: (data) => ({ title: data.title, name: "My project" }) });
|
|
30
|
+
|
|
31
|
+
return { markdownTemplateEngine: false };
|
|
32
|
+
}
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
Without the Norn plugin, add `codeBlock` to the project's own WebC `components` list beside `patterns()` from `@allons-y/webc-patterns`, and register `tokenRanges` as `nornTokenRanges`. Without `patterns()` in the list, `norn-copy-button` is an unknown tag and the code blocks render with no copy button and no error.
|
|
36
|
+
|
|
37
|
+
Call the component from a WebC page. Props take the `@` prefix:
|
|
38
|
+
|
|
39
|
+
```html
|
|
40
|
+
<norn-code-block @language="js" @caption="example.js" @code="const answer = 42;"></norn-code-block>
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
### Markdown
|
|
44
|
+
|
|
45
|
+
The Norn plugin adds the Markdown add-on for fenced code. Without the plugin, amend the library yourself:
|
|
46
|
+
|
|
47
|
+
```js
|
|
48
|
+
import { highlightFences } from "@allons-y/webc-code-sample/markdown";
|
|
49
|
+
|
|
50
|
+
config.amendLibrary("md", highlightFences);
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
Each fence renders as the `<norn-code-block>` an author would write, with the fence's first word as `@language`. The add-on fills no slots, so the labels and button texts come from the component's fallbacks, as in Translating below. The fence's tag is compiled by WebC, so the page's layout renders the Markdown content with `@html="content"`. Every Norn layout does, and `norn-base` does for a Markdown page that uses it directly.
|
|
54
|
+
|
|
55
|
+
### Translating
|
|
56
|
+
|
|
57
|
+
Every string a visitor sees or hears is content in the page, in a named slot, hidden until the script uses it: slot `label` names the code when there is no caption, `copy-label` names the copy button (`{name}` stands in for the code's name, filled in by the script), and `copy`, `copied`, and `failed` are the button's texts:
|
|
58
|
+
|
|
59
|
+
```html
|
|
60
|
+
<norn-code-block @language="sh" @code="npm install">
|
|
61
|
+
<span slot="label">Commande d'installation</span>
|
|
62
|
+
<span slot="copy-label">Copier la {name}</span>
|
|
63
|
+
<span slot="copy">Copier</span>
|
|
64
|
+
</norn-code-block>
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
An empty slot falls back to the site's strings from the patterns package's `componentStrings`, under `norn-code-block` (`label`, with `{language}` for the language, and `copy-label`) and `norn-copy-button` (`copy`, `copied`, `failed`), then to English. That covers Markdown fences too: `highlightFences` writes no slots of its own.
|
|
68
|
+
|
|
69
|
+
### Layout requirements
|
|
70
|
+
|
|
71
|
+
The Norn layouts meet these. A layout of the project's own:
|
|
72
|
+
|
|
73
|
+
- Links the page's bundles from the layout head, with `webc:keep` on both tags. WebC removes `<link>` and `<script src>` without it. `norn-head` does this.
|
|
74
|
+
|
|
75
|
+
```html
|
|
76
|
+
<link rel="stylesheet" :href="getBundleFileUrl('css')" webc:keep />
|
|
77
|
+
<script :src="getBundleFileUrl('js')" defer webc:keep></script>
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
- Sets `markdownTemplateEngine: false`, or the default Liquid pass rewrites `{{ }}` inside fences.
|
|
81
|
+
- Drops the `href=""` and `src=""` tags a page with no bundles is left with: `webc:if` can't guard them, because the URL resolves after render. The Norn plugin registers `dropEmptyBundleTags` from the patterns package as a transform for this.
|
|
82
|
+
|
|
83
|
+
### Without JavaScript
|
|
84
|
+
|
|
85
|
+
The code stays readable plain text, and is not a tab stop: the script makes it a named, focusable region only while it scrolls sideways. The highlight colors need the CSS Custom Highlight API and a script. The copy button is an icon over the top end corner of the code, which names itself when the pointer or focus is on it, and needs a script and the Clipboard API, which browsers expose only on secure origins (HTTPS or localhost): elsewhere it stays hidden. The code always reads left to right, in a right-to-left page too. The script registers `norn-code-block` as a custom element, so a sample added to the page later is enhanced too.
|
|
86
|
+
|
|
87
|
+
### Customizing
|
|
88
|
+
|
|
89
|
+
`@chrome="terminal"` frames the code with a header bar for the caption:
|
|
90
|
+
|
|
91
|
+
```html
|
|
92
|
+
<norn-code-block @chrome="terminal" @language="sh" @caption="Terminal" @code="npm install"></norn-code-block>
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
The sample reads shared tokens only, each falling back to its default compiled into the component, so it keeps its look with no stylesheet beside it. Set them in a project `tokens.css`:
|
|
96
|
+
|
|
97
|
+
| Token | Controls |
|
|
98
|
+
| --- | --- |
|
|
99
|
+
| `--norn-surface-code`, `--norn-text-base` | Surface and text colors |
|
|
100
|
+
| `--norn-ui-border-radius`, `--norn-space-m` | Corner radius and inner spacing |
|
|
101
|
+
| `--norn-font-mono` | Font stack |
|
|
102
|
+
| `--norn-code-comment`, `--norn-code-string`, `--norn-code-keyword`, `--norn-code-function`, `--norn-code-number`, `--norn-code-flag` | Syntax colors |
|
|
103
|
+
|
|
104
|
+
## Exports
|
|
105
|
+
|
|
106
|
+
| Export | Purpose |
|
|
107
|
+
| --- | --- |
|
|
108
|
+
| `codeBlock` | Absolute path to `components/norn-code-block.webc` |
|
|
109
|
+
| `tokenRanges(code, language)` | Shiki tokens as the compact JSON that `data-tokens` carries, or undefined with no tokens: `[groups, numbers]`, the group names once each, then three whole numbers per range, the group's index in `groups`, the gap from the end of the range before (from 0 for the first), and the length: `[["keyword","string"],[0,0,5,1,3,4]]` is `keyword` from 0 to 5 and `string` from 8 to 12. The ranges are character offsets in order, and none overlaps the next. The groups are `comment`, `string`, `keyword`, `function`, `number`, `flag`, and `punctuation`. For `sh`, `bash`, `shell`, and `shell-session`, the command is a `function`, its subcommand a `keyword`, a flag a `flag`, and an argument a `string`. The plugin registers it as `nornTokenRanges` |
|
|
110
|
+
| `decodeRanges(value)` | Reads a `tokenRanges` string back into `[group, start, end][]` character offsets. Undefined reads as `[]`; any other value that is not the compact form throws a `TypeError` that says what is wrong |
|
|
111
|
+
| `loadLanguages(...languages)` | Adds Shiki language registrations beyond the defaults (`bash`, `css`, `diff`, `html`, `javascript`, `json`, `markdown`, `typescript`, `yaml`), such as `import rust from "@shikijs/langs/rust"`. Call it once, before the build highlights code |
|
|
112
|
+
|
|
113
|
+
`@allons-y/webc-code-sample/markdown` exports `highlightFences(md)`, a markdown-it amend that renders fenced code as `<norn-code-block>`.
|
|
114
|
+
|
|
115
|
+
## License
|
|
116
|
+
|
|
117
|
+
[MPL-2.0](LICENSE): use freely, modify as needed; changes to MPL-licensed files should be shared back under the same license.
|
|
118
|
+
|
|
119
|
+
<p align="center">
|
|
120
|
+
<a href="https://allons-y.studio">
|
|
121
|
+
<picture>
|
|
122
|
+
<source media="(prefers-color-scheme: dark)" srcset="https://raw.githubusercontent.com/allonsy-studio/.github/main/assets/png/oss-footer-dark.png">
|
|
123
|
+
<source media="(prefers-color-scheme: light)" srcset="https://raw.githubusercontent.com/allonsy-studio/.github/main/assets/png/oss-footer-light.png">
|
|
124
|
+
<img id="user-content-logo" src="https://raw.githubusercontent.com/allonsy-studio/.github/main/assets/png/oss-footer-light.png" alt="Brought to you by Allons-y Studio" width="400">
|
|
125
|
+
</picture>
|
|
126
|
+
</a>
|
|
127
|
+
</p>
|
|
@@ -0,0 +1,306 @@
|
|
|
1
|
+
<!---
|
|
2
|
+
/*
|
|
3
|
+
* SPDX-License-Identifier: MPL-2.0
|
|
4
|
+
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
5
|
+
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
6
|
+
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
7
|
+
*/
|
|
8
|
+
--->
|
|
9
|
+
<script webc:setup>
|
|
10
|
+
/** The code as it should paste: a leading `$ ` prompt comes off each line, and `$HOME` is untouched. */
|
|
11
|
+
const pasteable = (code) =>
|
|
12
|
+
String(code)
|
|
13
|
+
.split("\n")
|
|
14
|
+
.map((line) => line.replace(/^\$\s+/, ""))
|
|
15
|
+
.join("\n");
|
|
16
|
+
|
|
17
|
+
/** The code's default name: the label template with the language in it. */
|
|
18
|
+
const nameOf = (language, label) =>
|
|
19
|
+
(typeof label === "string" && label ? label : "{language} code")
|
|
20
|
+
.replaceAll("{language}", typeof language === "string" ? language : "")
|
|
21
|
+
.replace(/\s+/g, " ")
|
|
22
|
+
.trim();
|
|
23
|
+
</script>
|
|
24
|
+
<figure :data-chrome="chrome === 'terminal' ? 'terminal' : undefined">
|
|
25
|
+
<figcaption webc:if="caption" @text="caption"></figcaption>
|
|
26
|
+
<norn-copy-button :@value="pasteable(code)" :@strings="'norn-code-block-' + uid" :@compact="true" @surface="code"></norn-copy-button>
|
|
27
|
+
<pre dir="ltr" :data-language="typeof language === 'string' && language ? language : undefined"><code :data-tokens="nornTokenRanges(code, language)" @text="code"></code></pre>
|
|
28
|
+
<span hidden :id="'norn-code-block-' + uid">
|
|
29
|
+
<span data-string="label" :id="'norn-code-block-' + uid + '-copy'"><slot name="copy-label"><span webc:nokeep @text="nornComponentStrings?.('norn-code-block', 'copy-label', $data.page) || 'Copy {name} to clipboard'"></span></slot></span>
|
|
30
|
+
<span data-string="copy"><slot name="copy"><span webc:nokeep @text="nornComponentStrings?.('norn-copy-button', 'copy', $data.page) || 'Copy'"></span></slot></span>
|
|
31
|
+
<span data-string="copied"><slot name="copied"><span webc:nokeep @text="nornComponentStrings?.('norn-copy-button', 'copied', $data.page) || 'Copied'"></span></slot></span>
|
|
32
|
+
<span data-string="failed"><slot name="failed"><span webc:nokeep @text="nornComponentStrings?.('norn-copy-button', 'failed', $data.page) || 'Copy failed'"></span></slot></span>
|
|
33
|
+
</span>
|
|
34
|
+
<span data-name hidden><slot name="label"><span data-default @text="nameOf(language, nornComponentStrings?.('norn-code-block', 'label', $data.page))"></span></slot></span>
|
|
35
|
+
</figure>
|
|
36
|
+
<style>
|
|
37
|
+
@layer norn.components {
|
|
38
|
+
norn-code-block {
|
|
39
|
+
display: block;
|
|
40
|
+
|
|
41
|
+
/* The caption sits above the code. The copy button shares the code's cell, over its top end corner. */
|
|
42
|
+
& figure {
|
|
43
|
+
display: grid;
|
|
44
|
+
grid-template-columns: minmax(0, 1fr);
|
|
45
|
+
margin: 0;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
& figure:has(> figcaption) {
|
|
49
|
+
row-gap: var(--norn-space-xs, 0.5rem);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
& figcaption {
|
|
53
|
+
grid-area: 1 / 1;
|
|
54
|
+
color: var(--norn-text-muted, light-dark(#4b5160, #a4abba));
|
|
55
|
+
font-family: var(--norn-font-mono, ui-monospace, SFMono-Regular, Menlo, Consolas, monospace);
|
|
56
|
+
font-size: var(--norn-font-size-s, 0.875rem);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
& norn-copy-button {
|
|
60
|
+
grid-area: 2 / 1;
|
|
61
|
+
z-index: 1;
|
|
62
|
+
justify-self: end;
|
|
63
|
+
align-self: start;
|
|
64
|
+
margin: var(--norn-space-2xs, 0.25rem);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
& pre {
|
|
68
|
+
box-sizing: border-box;
|
|
69
|
+
grid-area: 2 / 1;
|
|
70
|
+
margin: 0;
|
|
71
|
+
overflow-x: auto;
|
|
72
|
+
padding: var(--norn-space-m, 1.25rem);
|
|
73
|
+
border-radius: var(--norn-ui-border-radius, 6px);
|
|
74
|
+
background: var(--norn-surface-code, light-dark(hsl(40, 14%, 94%), #2c313b));
|
|
75
|
+
color: var(--norn-text-base, light-dark(#16181d, #eceef3));
|
|
76
|
+
font-family: var(--norn-font-mono, ui-monospace, SFMono-Regular, Menlo, Consolas, monospace);
|
|
77
|
+
font-size: var(--norn-font-size-s, 0.875rem);
|
|
78
|
+
line-height: 1.5;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/* With the copy button showing, the code's scrolling area stops short of it. A transparent border is outside the scrolling area and under the background, so no code runs beneath the button. The button sits at the figure's end, and the code is always left to right, so a right-to-left page takes the border on the other side. */
|
|
82
|
+
& figure:has(> norn-copy-button > button:not([hidden])) {
|
|
83
|
+
--_chip: calc(2 * var(--norn-space-xs, 0.5rem) + var(--norn-ui-icon-s, 1em) + var(--norn-space-2xs, 0.25rem) + var(--norn-space-xs, 0.5rem));
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
& figure:has(> norn-copy-button > button:not([hidden])) > pre {
|
|
87
|
+
border-inline-end: var(--_chip) solid transparent;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
& figure:dir(rtl):has(> norn-copy-button > button:not([hidden])) > pre {
|
|
91
|
+
border-inline-end: 0;
|
|
92
|
+
border-inline-start: var(--_chip) solid transparent;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
& code {
|
|
96
|
+
padding: 0;
|
|
97
|
+
background: none;
|
|
98
|
+
font: inherit;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/* A frame with a header bar for the caption, for `@chrome="terminal"`. `:where()` keeps each rule as specific as the plain one it follows, so a container's rules still win. */
|
|
102
|
+
& figure:where([data-chrome="terminal"]),
|
|
103
|
+
& figure:where([data-chrome="terminal"]):has(> figcaption) {
|
|
104
|
+
overflow: hidden;
|
|
105
|
+
row-gap: 0;
|
|
106
|
+
border-radius: var(--norn-ui-border-radius, 6px);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
& figure:where([data-chrome="terminal"]) > figcaption {
|
|
110
|
+
box-sizing: border-box;
|
|
111
|
+
display: flex;
|
|
112
|
+
align-items: center;
|
|
113
|
+
block-size: 2rem;
|
|
114
|
+
padding-inline: var(--norn-space-s, 0.75rem);
|
|
115
|
+
background: var(--norn-surface-raised, light-dark(hsl(40, 14%, 96%), #242932));
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
& figure:where([data-chrome="terminal"]) > pre {
|
|
119
|
+
border-radius: 0;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/* Colors for the ranges the script registers, set on `code` as Firefox ignores them on an ancestor. Plain text without Custom Highlight API support. */
|
|
123
|
+
& code::highlight(tok-comment) {
|
|
124
|
+
color: var(--norn-code-comment, light-dark(#566070, #9aa3b5));
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
& code::highlight(tok-string) {
|
|
128
|
+
color: var(--norn-code-string, light-dark(#0b6b3a, #7ed6a1));
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
& code::highlight(tok-keyword) {
|
|
132
|
+
color: var(--norn-code-keyword, light-dark(#6b2fc2, #c4a3ff));
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
& code::highlight(tok-function) {
|
|
136
|
+
color: var(--norn-code-function, light-dark(#1b4fc4, #8fb0ff));
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
& code::highlight(tok-number) {
|
|
140
|
+
color: var(--norn-code-number, light-dark(#9a4a00, #ffb36b));
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
& code::highlight(tok-flag) {
|
|
144
|
+
color: var(--norn-code-flag, light-dark(#00687a, #5ccfe0));
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
& code::highlight(tok-punctuation) {
|
|
148
|
+
color: var(--norn-text-muted, light-dark(#4b5160, #a4abba));
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
@media print {
|
|
152
|
+
break-inside: avoid;
|
|
153
|
+
|
|
154
|
+
& pre {
|
|
155
|
+
white-space: pre-wrap;
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
</style>
|
|
161
|
+
<script>
|
|
162
|
+
{
|
|
163
|
+
/** The ranges of every connected sample, by sample. The Custom Highlight API registry is shared by the page. */
|
|
164
|
+
const rangesBySample = new Map();
|
|
165
|
+
const supported = typeof CSS !== "undefined" && "highlights" in CSS;
|
|
166
|
+
|
|
167
|
+
/**
|
|
168
|
+
* Read `data-tokens`: `[groups, numbers]`, the group names, then a group index, the gap from the previous range's end, and a length per range.
|
|
169
|
+
*
|
|
170
|
+
* @param {string} tokens
|
|
171
|
+
* @returns {[string, number, number][]}
|
|
172
|
+
*/
|
|
173
|
+
const decode = (tokens) => {
|
|
174
|
+
const [groups, numbers] = JSON.parse(tokens);
|
|
175
|
+
const ranges = [];
|
|
176
|
+
let end = 0;
|
|
177
|
+
for (let at = 0; at < numbers.length; at += 3) {
|
|
178
|
+
const group = groups[numbers[at]];
|
|
179
|
+
const start = end + numbers[at + 1];
|
|
180
|
+
end = start + numbers[at + 2];
|
|
181
|
+
if (typeof group !== "string" || !Number.isInteger(start) || !(end > start)) throw new TypeError("Unusable data-tokens");
|
|
182
|
+
ranges.push([group, start, end]);
|
|
183
|
+
}
|
|
184
|
+
return ranges;
|
|
185
|
+
};
|
|
186
|
+
|
|
187
|
+
/** Add a sample's ranges to the page's highlights, one group at a time, so each connect costs only its own ranges. */
|
|
188
|
+
const paint = (sample, found) => {
|
|
189
|
+
rangesBySample.set(sample, found);
|
|
190
|
+
if (!supported) return;
|
|
191
|
+
for (const [group, range] of found) {
|
|
192
|
+
const name = `tok-${group}`;
|
|
193
|
+
if (!CSS.highlights.has(name)) CSS.highlights.set(name, new Highlight());
|
|
194
|
+
CSS.highlights.get(name).add(range);
|
|
195
|
+
}
|
|
196
|
+
};
|
|
197
|
+
|
|
198
|
+
/** Take a sample's ranges back out, and drop a group left empty. */
|
|
199
|
+
const unpaint = (sample) => {
|
|
200
|
+
const found = rangesBySample.get(sample);
|
|
201
|
+
rangesBySample.delete(sample);
|
|
202
|
+
if (!found || !supported) return;
|
|
203
|
+
for (const [group, range] of found) {
|
|
204
|
+
const name = `tok-${group}`;
|
|
205
|
+
const highlight = CSS.highlights.get(name);
|
|
206
|
+
highlight?.delete(range);
|
|
207
|
+
if (highlight?.size === 0) CSS.highlights.delete(name);
|
|
208
|
+
}
|
|
209
|
+
};
|
|
210
|
+
|
|
211
|
+
/**
|
|
212
|
+
* Code sample enhancements. The server-rendered figure, caption, and code are the whole component without a script:
|
|
213
|
+
* this adds the highlight colors, the copy button's name, and a tab stop only while the code scrolls.
|
|
214
|
+
*/
|
|
215
|
+
class CodeBlock extends HTMLElement {
|
|
216
|
+
#observer;
|
|
217
|
+
|
|
218
|
+
connectedCallback() {
|
|
219
|
+
if (document.readyState === "loading") {
|
|
220
|
+
document.addEventListener("DOMContentLoaded", () => this.isConnected && this.#enhance(), { once: true });
|
|
221
|
+
return;
|
|
222
|
+
}
|
|
223
|
+
this.#enhance();
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
disconnectedCallback() {
|
|
227
|
+
unpaint(this);
|
|
228
|
+
this.#observer?.disconnect();
|
|
229
|
+
this.#observer = undefined;
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
/** Safe to run again: a move reconnects the element. */
|
|
233
|
+
#enhance() {
|
|
234
|
+
const pre = this.querySelector("pre");
|
|
235
|
+
if (!pre) return;
|
|
236
|
+
const name = this.#name();
|
|
237
|
+
this.#nameCopyButton(name);
|
|
238
|
+
this.#highlight(pre.querySelector("code[data-tokens]"));
|
|
239
|
+
this.#watchOverflow(pre, name);
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
/** What names the code: its caption, else the `label` text. Either gets an id from the texts' holder, for `aria-labelledby`. */
|
|
243
|
+
#name() {
|
|
244
|
+
const holder = this.querySelector(":scope > figure > span[hidden][id]");
|
|
245
|
+
const named = this.querySelector(":scope > figure > figcaption") ?? this.querySelector(":scope > figure > [data-name]");
|
|
246
|
+
if (named && holder) named.id ||= `${holder.id}-name`;
|
|
247
|
+
return named;
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
/** Put the code's name in the copy button's, where it has `{name}`. Once filled, it has none left. */
|
|
251
|
+
#nameCopyButton(name) {
|
|
252
|
+
const label = this.querySelector(':scope > figure > [hidden] > [data-string="label"]');
|
|
253
|
+
if (!label || !name) return;
|
|
254
|
+
const walker = document.createTreeWalker(label, NodeFilter.SHOW_TEXT);
|
|
255
|
+
const words = name.textContent.trim();
|
|
256
|
+
while (walker.nextNode()) walker.currentNode.data = walker.currentNode.data.replaceAll("{name}", words);
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
/** A sample whose token data is unusable stays plain. */
|
|
260
|
+
#highlight(code) {
|
|
261
|
+
unpaint(this);
|
|
262
|
+
try {
|
|
263
|
+
const text = code?.firstChild;
|
|
264
|
+
if (text) {
|
|
265
|
+
paint(
|
|
266
|
+
this,
|
|
267
|
+
decode(code.dataset.tokens).map(([group, start, end]) => {
|
|
268
|
+
const range = new Range();
|
|
269
|
+
range.setStart(text, start);
|
|
270
|
+
range.setEnd(text, end);
|
|
271
|
+
return [group, range];
|
|
272
|
+
}),
|
|
273
|
+
);
|
|
274
|
+
}
|
|
275
|
+
} catch {
|
|
276
|
+
unpaint(this);
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
|
|
281
|
+
/** Keep the code a named, focusable region only while it scrolls: a tab stop is for something to scroll. */
|
|
282
|
+
#watchOverflow(pre, name) {
|
|
283
|
+
const fit = () => {
|
|
284
|
+
if (pre.scrollWidth > pre.clientWidth) {
|
|
285
|
+
pre.setAttribute("tabindex", "0");
|
|
286
|
+
if (name?.id) {
|
|
287
|
+
pre.setAttribute("role", "region");
|
|
288
|
+
pre.setAttribute("aria-labelledby", name.id);
|
|
289
|
+
}
|
|
290
|
+
} else {
|
|
291
|
+
pre.removeAttribute("tabindex");
|
|
292
|
+
pre.removeAttribute("role");
|
|
293
|
+
pre.removeAttribute("aria-labelledby");
|
|
294
|
+
}
|
|
295
|
+
};
|
|
296
|
+
document.fonts?.ready.then(fit);
|
|
297
|
+
this.#observer?.disconnect();
|
|
298
|
+
if (typeof ResizeObserver === "undefined") return;
|
|
299
|
+
this.#observer = new ResizeObserver(fit);
|
|
300
|
+
this.#observer.observe(pre);
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
if (!customElements.get("norn-code-block")) customElements.define("norn-code-block", CodeBlock);
|
|
305
|
+
}
|
|
306
|
+
</script>
|
package/index.js
ADDED
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* SPDX-License-Identifier: MPL-2.0
|
|
3
|
+
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
4
|
+
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
5
|
+
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import path from "node:path";
|
|
9
|
+
|
|
10
|
+
import { addLanguages, tokenize } from "./tokenize.js";
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Ranges by language and code, for the life of the process. The code is the key, so a watch-mode rebuild with changed code tokenizes again.
|
|
14
|
+
* Holds at most `CACHE_LIMIT` samples and drops the oldest first.
|
|
15
|
+
*/
|
|
16
|
+
const cache = new Map();
|
|
17
|
+
const CACHE_LIMIT = 1000;
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Add languages beyond the defaults.
|
|
21
|
+
*
|
|
22
|
+
* @param {...import("shiki/core").LanguageRegistration[]} languages - Shiki language registrations, such as the default export of `@shikijs/langs/rust`
|
|
23
|
+
*/
|
|
24
|
+
export function loadLanguages(...languages) {
|
|
25
|
+
addLanguages(languages.flat());
|
|
26
|
+
cache.clear();
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** Absolute glob-safe path to `components/norn-code-block.webc`, for the project's WebC `components` list. */
|
|
30
|
+
export const codeBlock = path.join(import.meta.dirname, "components", "norn-code-block.webc").split(path.sep).join("/");
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Tokenize code with Shiki into highlight ranges, in the compact form `data-tokens` carries (see `encodeRanges`).
|
|
34
|
+
*
|
|
35
|
+
* @param {string} code
|
|
36
|
+
* @param {string} [language]
|
|
37
|
+
* @returns {string | undefined} JSON `[groups, numbers]`; undefined when the language isn't loaded or nothing matched
|
|
38
|
+
*/
|
|
39
|
+
export function tokenRanges(code, language = "") {
|
|
40
|
+
const key = `${language}\0${code}`;
|
|
41
|
+
if (cache.has(key)) return cache.get(key);
|
|
42
|
+
const found = tokenize(code, language);
|
|
43
|
+
const ranges = found && encodeRanges(found);
|
|
44
|
+
if (cache.size >= CACHE_LIMIT) cache.delete(cache.keys().next().value);
|
|
45
|
+
cache.set(key, ranges);
|
|
46
|
+
return ranges;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Write ranges in the compact `data-tokens` form: `[groups, numbers]`, the group names once each, in order of first use,
|
|
51
|
+
* then three numbers per range: the group's index in `groups`, the gap from the end of the range before (or from 0), and the length.
|
|
52
|
+
*
|
|
53
|
+
* @param {[string, number, number][]} ranges - `[group, start, end]`, ordered and apart
|
|
54
|
+
* @returns {string}
|
|
55
|
+
*/
|
|
56
|
+
function encodeRanges(ranges) {
|
|
57
|
+
const groups = [];
|
|
58
|
+
const numbers = [];
|
|
59
|
+
let previousEnd = 0;
|
|
60
|
+
for (const [group, start, end] of ranges) {
|
|
61
|
+
if (!groups.includes(group)) groups.push(group);
|
|
62
|
+
numbers.push(groups.indexOf(group), start - previousEnd, end - start);
|
|
63
|
+
previousEnd = end;
|
|
64
|
+
}
|
|
65
|
+
return JSON.stringify([groups, numbers]);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Read a `tokenRanges` string, the `data-tokens` value, back into `[group, start, end][]` character offsets.
|
|
70
|
+
*
|
|
71
|
+
* @param {string | undefined} value - The compact form; undefined, as `tokenRanges` returns for no ranges, reads as none
|
|
72
|
+
* @returns {[string, number, number][]}
|
|
73
|
+
* @throws {TypeError} When the value is not the compact form, naming what is wrong
|
|
74
|
+
*/
|
|
75
|
+
export function decodeRanges(value) {
|
|
76
|
+
if (value === undefined) return [];
|
|
77
|
+
if (typeof value !== "string") throw new TypeError(`decodeRanges takes a string, not ${typeof value}.`);
|
|
78
|
+
let parsed;
|
|
79
|
+
try {
|
|
80
|
+
parsed = JSON.parse(value);
|
|
81
|
+
} catch {
|
|
82
|
+
throw new TypeError("decodeRanges: the value is not JSON.");
|
|
83
|
+
}
|
|
84
|
+
const [groups, numbers] = Array.isArray(parsed) ? parsed : [];
|
|
85
|
+
if (parsed?.length !== 2 || !Array.isArray(groups) || !Array.isArray(numbers)) throw new TypeError("decodeRanges: the value is not a group table and a list of numbers.");
|
|
86
|
+
if (!groups.every((group) => typeof group === "string")) throw new TypeError("decodeRanges: the group table holds group names only.");
|
|
87
|
+
if (numbers.length % 3) throw new TypeError("decodeRanges: the list of numbers is not a multiple of three long.");
|
|
88
|
+
const ranges = [];
|
|
89
|
+
let end = 0;
|
|
90
|
+
for (let at = 0; at < numbers.length; at += 3) {
|
|
91
|
+
const [index, gap, length] = numbers.slice(at, at + 3);
|
|
92
|
+
if (!Number.isInteger(index) || !Object.hasOwn(groups, index)) throw new TypeError(`decodeRanges: range ${at / 3} names group ${index}, which the table lacks.`);
|
|
93
|
+
if (!Number.isInteger(gap) || gap < 0) throw new TypeError(`decodeRanges: range ${at / 3} has gap ${gap}, not a whole number of 0 or more.`);
|
|
94
|
+
if (!Number.isInteger(length) || length < 1) throw new TypeError(`decodeRanges: range ${at / 3} has length ${length}, not a whole number of 1 or more.`);
|
|
95
|
+
const start = end + gap;
|
|
96
|
+
end = start + length;
|
|
97
|
+
ranges.push([groups[index], start, end]);
|
|
98
|
+
}
|
|
99
|
+
return ranges;
|
|
100
|
+
}
|
package/markdown.js
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* SPDX-License-Identifier: MPL-2.0
|
|
3
|
+
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
4
|
+
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
5
|
+
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
/*
|
|
9
|
+
* The Markdown add-on. It writes the `<norn-code-block>` tag an author would write by hand
|
|
10
|
+
* and no text of its own: the component's slot fallbacks and `componentStrings` supply every string.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* markdown-it amend: render fenced code as `<norn-code-block>`, with the fence's first word as its `language`.
|
|
15
|
+
* It fills no slots: the component names the code ("js code") and its copy button, and takes the button texts from the site's `componentStrings`, then English.
|
|
16
|
+
*
|
|
17
|
+
* @param {import("markdown-it")} md
|
|
18
|
+
*/
|
|
19
|
+
export function highlightFences(md) {
|
|
20
|
+
md.renderer.rules.fence = (tokens, idx) => {
|
|
21
|
+
const { content, info } = tokens[idx];
|
|
22
|
+
const language = info.trim().split(/\s+/)[0];
|
|
23
|
+
return `<norn-code-block @code="${md.utils.escapeHtml(content)}" @language="${md.utils.escapeHtml(language)}"></norn-code-block>\n`;
|
|
24
|
+
};
|
|
25
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,57 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
2
|
+
"name": "@allons-y/webc-code-sample",
|
|
3
|
+
"version": "0.1.0-beta.1",
|
|
4
|
+
"description": "WebC code sample component with build-time syntax highlighting and no highlighter shipped to the browser.",
|
|
5
|
+
"license": "MPL-2.0",
|
|
6
|
+
"author": "Cassondra Roberts <castastrophe@users.noreply.github.com> (https://allons-y.studio)",
|
|
7
|
+
"homepage": "https://github.com/allonsy-studio/webc-resources/tree/main/packages/webc-code-sample#readme",
|
|
8
|
+
"repository": {
|
|
9
|
+
"type": "git",
|
|
10
|
+
"url": "git+https://github.com/allonsy-studio/webc-resources.git",
|
|
11
|
+
"directory": "packages/webc-code-sample"
|
|
12
|
+
},
|
|
13
|
+
"bugs": {
|
|
14
|
+
"url": "https://github.com/allonsy-studio/webc-resources/issues"
|
|
15
|
+
},
|
|
16
|
+
"publishConfig": {
|
|
17
|
+
"access": "public",
|
|
18
|
+
"registry": "https://registry.npmjs.org/"
|
|
19
|
+
},
|
|
20
|
+
"type": "module",
|
|
21
|
+
"exports": {
|
|
22
|
+
".": "./index.js",
|
|
23
|
+
"./markdown": "./markdown.js",
|
|
24
|
+
"./package.json": "./package.json"
|
|
25
|
+
},
|
|
26
|
+
"files": [
|
|
27
|
+
"components/",
|
|
28
|
+
"index.js",
|
|
29
|
+
"markdown.js",
|
|
30
|
+
"tokenize.js",
|
|
31
|
+
"LICENSE",
|
|
32
|
+
"README.md"
|
|
33
|
+
],
|
|
34
|
+
"scripts": {
|
|
35
|
+
"prepack": "node ../../scripts/build-components.js webc-code-sample"
|
|
36
|
+
},
|
|
37
|
+
"devDependencies": {
|
|
38
|
+
"markdown-it": "^15.0.2"
|
|
39
|
+
},
|
|
40
|
+
"peerDependencies": {
|
|
41
|
+
"@11ty/eleventy": "^3.0.0",
|
|
42
|
+
"@11ty/eleventy-plugin-webc": "^0.11.0",
|
|
43
|
+
"@allons-y/webc-patterns": "^0.1.0-beta.0"
|
|
44
|
+
},
|
|
45
|
+
"keywords": [
|
|
46
|
+
"webc",
|
|
47
|
+
"eleventy",
|
|
48
|
+
"11ty"
|
|
49
|
+
],
|
|
50
|
+
"engines": {
|
|
51
|
+
"node": ">=22"
|
|
52
|
+
},
|
|
53
|
+
"dependencies": {
|
|
54
|
+
"@shikijs/langs": "^4.5.0",
|
|
55
|
+
"shiki": "^4.5.0"
|
|
56
|
+
}
|
|
57
|
+
}
|
package/tokenize.js
ADDED
|
@@ -0,0 +1,231 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* SPDX-License-Identifier: MPL-2.0
|
|
3
|
+
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
4
|
+
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
5
|
+
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
/** Shiki tokenizing behind `tokenRanges`. Internal: the package exports map leaves it out. */
|
|
9
|
+
|
|
10
|
+
import bash from "@shikijs/langs/shellscript";
|
|
11
|
+
import css from "@shikijs/langs/css";
|
|
12
|
+
import diff from "@shikijs/langs/diff";
|
|
13
|
+
import html from "@shikijs/langs/html";
|
|
14
|
+
import javascript from "@shikijs/langs/javascript";
|
|
15
|
+
import json from "@shikijs/langs/json";
|
|
16
|
+
import markdown from "@shikijs/langs/markdown";
|
|
17
|
+
import typescript from "@shikijs/langs/typescript";
|
|
18
|
+
import yaml from "@shikijs/langs/yaml";
|
|
19
|
+
import { createHighlighterCoreSync } from "shiki/core";
|
|
20
|
+
import { createJavaScriptRegexEngine } from "shiki/engine/javascript";
|
|
21
|
+
|
|
22
|
+
/** Shiki needs a theme to tokenize. The colors come from the page's CSS, so this one has none. */
|
|
23
|
+
const THEME = { name: "norn", type: "light", fg: "#000", bg: "#fff", settings: [] };
|
|
24
|
+
|
|
25
|
+
const highlighter = createHighlighterCoreSync({
|
|
26
|
+
themes: [THEME],
|
|
27
|
+
langs: [bash, css, diff, html, javascript, json, markdown, typescript, yaml],
|
|
28
|
+
engine: createJavaScriptRegexEngine(),
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Add Shiki languages to the highlighter.
|
|
33
|
+
*
|
|
34
|
+
* @param {import("shiki/core").LanguageRegistration[]} languages
|
|
35
|
+
*/
|
|
36
|
+
export function addLanguages(languages) {
|
|
37
|
+
highlighter.loadLanguageSync(languages);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** Highlight group by TextMate scope prefix, most specific first. The deepest scope of a token that matches a prefix decides. */
|
|
41
|
+
const SCOPES = [
|
|
42
|
+
["comment", "comment"],
|
|
43
|
+
["punctuation.definition.comment", "comment"],
|
|
44
|
+
["punctuation.definition.string", "string"],
|
|
45
|
+
["string.unquoted", undefined],
|
|
46
|
+
["string", "string"],
|
|
47
|
+
["markup.inserted", "string"],
|
|
48
|
+
["constant.character.escape.line-continuation", "punctuation"],
|
|
49
|
+
["constant.numeric", "number"],
|
|
50
|
+
["constant.language", "number"],
|
|
51
|
+
["keyword.operator", "punctuation"],
|
|
52
|
+
["keyword", "keyword"],
|
|
53
|
+
["storage", "keyword"],
|
|
54
|
+
["entity.name.tag", "keyword"],
|
|
55
|
+
["markup.heading", "keyword"],
|
|
56
|
+
["entity.name.function", "function"],
|
|
57
|
+
["entity.name.command", "function"],
|
|
58
|
+
["entity.name.type", "function"],
|
|
59
|
+
["entity.name.class", "function"],
|
|
60
|
+
["entity.other.attribute-name", "function"],
|
|
61
|
+
["entity.other.inherited-class", "function"],
|
|
62
|
+
["support.function", "function"],
|
|
63
|
+
["support.class", "function"],
|
|
64
|
+
["support.type.property-name", "function"],
|
|
65
|
+
["meta.object-literal.key", "function"],
|
|
66
|
+
["punctuation", "punctuation"],
|
|
67
|
+
];
|
|
68
|
+
|
|
69
|
+
/** The highlight group of a stack of scopes, from the outermost to the deepest. A prefix with no group, such as an unquoted word, stops the search: it stays plain. */
|
|
70
|
+
const groupOf = (scopes) => {
|
|
71
|
+
for (const { scopeName } of scopes.toReversed()) {
|
|
72
|
+
const match = SCOPES.find(([prefix]) => scopeName === prefix || scopeName.startsWith(`${prefix}.`));
|
|
73
|
+
if (match) return match[1];
|
|
74
|
+
}
|
|
75
|
+
return undefined;
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
/** Languages that read as shell commands, and the ones whose lines are commands only after a `$ ` prompt. */
|
|
79
|
+
const SHELL = new Set(["sh", "bash", "shell"]);
|
|
80
|
+
const SESSION = new Set(["shell-session", "sh-session", "shellsession"]);
|
|
81
|
+
|
|
82
|
+
/** Words that start a command list again: after one, the next word is a command. */
|
|
83
|
+
const CONTROL = new Set(["if", "then", "else", "elif", "do", "while", "until", "!", "time"]);
|
|
84
|
+
|
|
85
|
+
/** Words made only of operator characters: a pipe or a list separator starts a command, a redirect does not. */
|
|
86
|
+
const OPERATOR = /^[<>|&;()0-9]*[<>|&;()]+$/;
|
|
87
|
+
const ASSIGNMENT = /^[A-Za-z_]\w*=/;
|
|
88
|
+
const SEPARATOR = /^(\|\|?|&&|&|;|\(|\|&)$/;
|
|
89
|
+
|
|
90
|
+
/** Whether a line ends with a lone backslash, so its command goes on in the next line. */
|
|
91
|
+
const continues = (line) => line.trimEnd().split(/\s+/).at(-1) === "\\";
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Tokenize code with Shiki into highlight ranges, ordered and apart: each range starts at or after the end of the one before.
|
|
96
|
+
*
|
|
97
|
+
* @param {string} code
|
|
98
|
+
* @param {string} [language]
|
|
99
|
+
* @returns {[string, number, number][] | undefined} `[group, start, end]` character offsets; undefined when the language isn't loaded or nothing matched
|
|
100
|
+
*/
|
|
101
|
+
export function tokenize(code, language = "") {
|
|
102
|
+
const session = SESSION.has(language);
|
|
103
|
+
const lang = session ? "bash" : language;
|
|
104
|
+
if (!lang || !highlighter.getLoadedLanguages().includes(lang)) return undefined;
|
|
105
|
+
let ranges = [];
|
|
106
|
+
// No time limit: Shiki tokenizes each line twice, and a limit that stops one pass early, as on a busy machine, breaks the explanation.
|
|
107
|
+
const { tokens } = highlighter.codeToTokens(code, { lang, theme: THEME.name, includeExplanation: "scopeName", tokenizeTimeLimit: 0 });
|
|
108
|
+
for (const lineTokens of tokens) {
|
|
109
|
+
for (const token of lineTokens) {
|
|
110
|
+
let at = token.offset;
|
|
111
|
+
for (const { content, scopes } of token.explanation ?? []) {
|
|
112
|
+
const group = groupOf(scopes);
|
|
113
|
+
const end = at + content.length;
|
|
114
|
+
if (group && content.trim()) {
|
|
115
|
+
const previous = ranges.at(-1);
|
|
116
|
+
if (previous?.[0] === group && previous[2] === at) previous[2] = end;
|
|
117
|
+
else ranges.push([group, at, end]);
|
|
118
|
+
}
|
|
119
|
+
at = end;
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
if (SHELL.has(language) || session) ranges = shellWords(code, ranges, session);
|
|
124
|
+
return ranges.length ? ranges : undefined;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* Give the words of each shell line their roles over Shiki's ranges: the command is a `function`, the first plain word after it a `keyword` (the subcommand), a flag a `flag`, and any other word a `string`.
|
|
129
|
+
* Comments, quoted strings, and variables keep what Shiki gave them. A `$ ` prompt stays punctuation.
|
|
130
|
+
*
|
|
131
|
+
* @param {string} code
|
|
132
|
+
* @param {[string, number, number][]} ranges - Shiki's ranges, in order
|
|
133
|
+
* @param {boolean} session - Only lines that start with a `$ ` prompt are commands
|
|
134
|
+
* @returns {[string, number, number][]}
|
|
135
|
+
*/
|
|
136
|
+
function shellWords(code, ranges, session) {
|
|
137
|
+
const roles = [];
|
|
138
|
+
if (session) {
|
|
139
|
+
// Output lines are plain: keep Shiki's ranges only on the lines of a command, which start with a prompt and go on while a line ends with a backslash.
|
|
140
|
+
let at = 0;
|
|
141
|
+
let running = false;
|
|
142
|
+
const kept = new Set();
|
|
143
|
+
for (const line of code.split("\n")) {
|
|
144
|
+
const command = running || /^\s*\$\s/.test(line);
|
|
145
|
+
if (command) kept.add(at);
|
|
146
|
+
running = command && continues(line);
|
|
147
|
+
at += line.length + 1;
|
|
148
|
+
}
|
|
149
|
+
const lineStart = (offset) => code.lastIndexOf("\n", offset - 1) + 1;
|
|
150
|
+
ranges = ranges.filter(([, start]) => kept.has(lineStart(start)));
|
|
151
|
+
}
|
|
152
|
+
const within = (at, groups) => ranges.some(([group, start, end]) => groups.includes(group) && at >= start && at < end);
|
|
153
|
+
let command = true;
|
|
154
|
+
let sub = true;
|
|
155
|
+
let dashes = false;
|
|
156
|
+
let continued = false;
|
|
157
|
+
let offset = 0;
|
|
158
|
+
for (const line of code.split("\n")) {
|
|
159
|
+
const words = [...line.matchAll(/\S+/g)];
|
|
160
|
+
const prompted = /^\s*\$\s/.test(line);
|
|
161
|
+
const continuing = continued;
|
|
162
|
+
if (!continuing) {
|
|
163
|
+
command = !session || prompted;
|
|
164
|
+
sub = false;
|
|
165
|
+
dashes = false;
|
|
166
|
+
}
|
|
167
|
+
continued = (!session || prompted || continuing) && continues(line);
|
|
168
|
+
for (const [index, match] of words.entries()) {
|
|
169
|
+
const [word] = match;
|
|
170
|
+
const start = offset + match.index;
|
|
171
|
+
const end = start + word.length;
|
|
172
|
+
if (within(start, ["comment", "string"])) {
|
|
173
|
+
if (within(start, ["comment"])) break;
|
|
174
|
+
continue;
|
|
175
|
+
}
|
|
176
|
+
if (index === 0 && prompted && word === "$") {
|
|
177
|
+
roles.push(["punctuation", start, end]);
|
|
178
|
+
continue;
|
|
179
|
+
}
|
|
180
|
+
if (!command && session && !prompted && !continuing) continue;
|
|
181
|
+
if (SEPARATOR.test(word)) {
|
|
182
|
+
command = true;
|
|
183
|
+
continue;
|
|
184
|
+
}
|
|
185
|
+
if (OPERATOR.test(word) || word === "\\") continue;
|
|
186
|
+
if (command && ASSIGNMENT.test(word)) {
|
|
187
|
+
const equals = word.indexOf("=");
|
|
188
|
+
const value = word.slice(equals + 1);
|
|
189
|
+
roles.push(["punctuation", start + equals, start + equals + 1]);
|
|
190
|
+
if (value && !/^["']/.test(value)) roles.push([/^\d+$/.test(value) ? "number" : "string", start + equals + 1, end]);
|
|
191
|
+
continue;
|
|
192
|
+
}
|
|
193
|
+
if (word.startsWith("$")) {
|
|
194
|
+
if (!command) roles.push(["string", start, end]);
|
|
195
|
+
continue;
|
|
196
|
+
}
|
|
197
|
+
if (command) {
|
|
198
|
+
if (CONTROL.has(word)) {
|
|
199
|
+
roles.push(["keyword", start, end]);
|
|
200
|
+
} else {
|
|
201
|
+
roles.push(["function", start, end]);
|
|
202
|
+
command = false;
|
|
203
|
+
sub = false;
|
|
204
|
+
dashes = false;
|
|
205
|
+
}
|
|
206
|
+
} else if (word === "--") {
|
|
207
|
+
dashes = true;
|
|
208
|
+
roles.push(["punctuation", start, end]);
|
|
209
|
+
} else if (word.startsWith("-") && word.length > 1) {
|
|
210
|
+
const equals = word.indexOf("=");
|
|
211
|
+
const stop = equals === -1 ? end : start + equals;
|
|
212
|
+
roles.push(["flag", start, stop]);
|
|
213
|
+
if (equals !== -1) roles.push(["punctuation", stop, stop + 1]);
|
|
214
|
+
if (equals !== -1 && equals + 1 < word.length) roles.push(["string", start + equals + 1, end]);
|
|
215
|
+
} else if (!sub && !dashes) {
|
|
216
|
+
roles.push(["keyword", start, end]);
|
|
217
|
+
sub = true;
|
|
218
|
+
} else {
|
|
219
|
+
roles.push(["string", start, end]);
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
offset += line.length + 1;
|
|
223
|
+
}
|
|
224
|
+
// The roles replace whatever Shiki gave the same characters.
|
|
225
|
+
const cut = ranges.flatMap(([group, start, end]) => {
|
|
226
|
+
let pieces = [[start, end]];
|
|
227
|
+
for (const [, from, to] of roles) pieces = pieces.flatMap(([a, b]) => (to <= a || from >= b ? [[a, b]] : [[a, Math.min(b, from)], [Math.max(a, to), b]].filter(([x, y]) => y > x)));
|
|
228
|
+
return pieces.map(([a, b]) => [group, a, b]);
|
|
229
|
+
});
|
|
230
|
+
return [...cut, ...roles].sort((a, b) => a[1] - b[1]);
|
|
231
|
+
}
|