@lanfermann/ui 0.0.0-stage → 0.1.0
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 +24 -0
- package/README.md +17 -2
- package/TRADEMARK.md +10 -0
- package/brand/app-icon.svg +1 -0
- package/brand/avatar.svg +1 -0
- package/brand/favicon.svg +1 -0
- package/dist/components/accordion/accordion.d.ts +26 -0
- package/dist/components/accordion/accordion.js +78 -0
- package/dist/components/alert/alert.d.ts +19 -0
- package/dist/components/alert/alert.js +68 -0
- package/dist/components/alert-dialog/alert-dialog.d.ts +20 -0
- package/dist/components/alert-dialog/alert-dialog.js +75 -0
- package/dist/components/anchor-heading/anchor-heading.d.ts +19 -0
- package/dist/components/anchor-heading/anchor-heading.js +39 -0
- package/dist/components/avatar/avatar.d.ts +14 -0
- package/dist/components/avatar/avatar.js +42 -0
- package/dist/components/badge/badge.d.ts +12 -0
- package/dist/components/badge/badge.js +27 -0
- package/dist/components/breadcrumbs/breadcrumbs.d.ts +23 -0
- package/dist/components/breadcrumbs/breadcrumbs.js +63 -0
- package/dist/components/button/button-variants.d.ts +15 -0
- package/dist/components/button/button-variants.js +76 -0
- package/dist/components/button/button.d.ts +25 -0
- package/dist/components/button/button.js +50 -0
- package/dist/components/calendar/calendar-selection.js +216 -0
- package/dist/components/calendar/calendar.d.ts +44 -0
- package/dist/components/calendar/calendar.js +208 -0
- package/dist/components/callout/callout.d.ts +21 -0
- package/dist/components/callout/callout.js +75 -0
- package/dist/components/card/card.d.ts +16 -0
- package/dist/components/card/card.js +53 -0
- package/dist/components/checkbox/checkbox.d.ts +9 -0
- package/dist/components/checkbox/checkbox.js +33 -0
- package/dist/components/checkbox-group/checkbox-group.d.ts +18 -0
- package/dist/components/checkbox-group/checkbox-group.js +33 -0
- package/dist/components/code/code.d.ts +7 -0
- package/dist/components/code/code.js +16 -0
- package/dist/components/code-block/code-block-frame.js +146 -0
- package/dist/components/code-block/code-block-live.d.ts +45 -0
- package/dist/components/code-block/code-block-live.js +51 -0
- package/dist/components/code-block/code-block.d.ts +36 -0
- package/dist/components/code-block/code-block.js +51 -0
- package/dist/components/code-block/code-copy.d.ts +12 -0
- package/dist/components/code-block/code-copy.js +71 -0
- package/dist/components/code-block/code-lines-view.d.ts +16 -0
- package/dist/components/code-block/code-lines-view.js +48 -0
- package/dist/components/code-block/code-lines.js +17 -0
- package/dist/components/code-block/code-scroll.js +29 -0
- package/dist/components/code-block/code-surface.js +13 -0
- package/dist/components/code-block/code-tabs-frame.js +85 -0
- package/dist/components/code-block/code-tabs-store.js +25 -0
- package/dist/components/code-block/code-tabs.d.ts +28 -0
- package/dist/components/code-block/code-tabs.js +29 -0
- package/dist/components/code-block/highlight.d.ts +34 -0
- package/dist/components/code-block/highlight.js +258 -0
- package/dist/components/code-block/lines.d.ts +5 -0
- package/dist/components/code-block/lines.js +40 -0
- package/dist/components/collapsible/collapsible.d.ts +19 -0
- package/dist/components/collapsible/collapsible.js +54 -0
- package/dist/components/combobox/combobox.d.ts +84 -0
- package/dist/components/combobox/combobox.js +169 -0
- package/dist/components/container/container.d.ts +16 -0
- package/dist/components/container/container.js +30 -0
- package/dist/components/copy-pill/copy-pill.d.ts +52 -0
- package/dist/components/copy-pill/copy-pill.js +232 -0
- package/dist/components/cta/cta.d.ts +109 -0
- package/dist/components/cta/cta.js +375 -0
- package/dist/components/date-field/date-field.d.ts +40 -0
- package/dist/components/date-field/date-field.js +99 -0
- package/dist/components/date-range-field/date-range-field.d.ts +41 -0
- package/dist/components/date-range-field/date-range-field.js +155 -0
- package/dist/components/dialog/dialog.d.ts +29 -0
- package/dist/components/dialog/dialog.js +96 -0
- package/dist/components/docs-layout/docs-layout.d.ts +17 -0
- package/dist/components/docs-layout/docs-layout.js +46 -0
- package/dist/components/drawer/drawer.d.ts +39 -0
- package/dist/components/drawer/drawer.js +181 -0
- package/dist/components/field/field.d.ts +18 -0
- package/dist/components/field/field.js +77 -0
- package/dist/components/fieldset/fieldset.d.ts +8 -0
- package/dist/components/fieldset/fieldset.js +25 -0
- package/dist/components/figure/figure.d.ts +14 -0
- package/dist/components/figure/figure.js +35 -0
- package/dist/components/form/form.d.ts +31 -0
- package/dist/components/form/form.js +55 -0
- package/dist/components/ground/ground.d.ts +20 -0
- package/dist/components/ground/ground.js +31 -0
- package/dist/components/heading/heading.d.ts +16 -0
- package/dist/components/heading/heading.js +36 -0
- package/dist/components/icon/icon.d.ts +22 -0
- package/dist/components/icon/icon.js +24 -0
- package/dist/components/input/input.d.ts +19 -0
- package/dist/components/input/input.js +46 -0
- package/dist/components/input-group/input-group.d.ts +34 -0
- package/dist/components/input-group/input-group.js +61 -0
- package/dist/components/kbd/kbd.d.ts +6 -0
- package/dist/components/kbd/kbd.js +15 -0
- package/dist/components/link/link-variants.d.ts +13 -0
- package/dist/components/link/link-variants.js +27 -0
- package/dist/components/link/link.d.ts +9 -0
- package/dist/components/link/link.js +18 -0
- package/dist/components/logo/logo-paths.js +22 -0
- package/dist/components/logo/logomark.d.ts +19 -0
- package/dist/components/logo/logomark.js +49 -0
- package/dist/components/logo/wordmark.d.ts +35 -0
- package/dist/components/logo/wordmark.js +48 -0
- package/dist/components/menu/menu.d.ts +26 -0
- package/dist/components/menu/menu.js +132 -0
- package/dist/components/meter/meter.d.ts +29 -0
- package/dist/components/meter/meter.js +68 -0
- package/dist/components/navigation-menu/navigation-menu-variants.d.ts +12 -0
- package/dist/components/navigation-menu/navigation-menu-variants.js +18 -0
- package/dist/components/navigation-menu/navigation-menu.d.ts +42 -0
- package/dist/components/navigation-menu/navigation-menu.js +128 -0
- package/dist/components/number-field/number-field.d.ts +32 -0
- package/dist/components/number-field/number-field.js +85 -0
- package/dist/components/page-header/page-header.d.ts +27 -0
- package/dist/components/page-header/page-header.js +48 -0
- package/dist/components/pager/pager.d.ts +24 -0
- package/dist/components/pager/pager.js +51 -0
- package/dist/components/parameter-list/parameter-list.d.ts +34 -0
- package/dist/components/parameter-list/parameter-list.js +70 -0
- package/dist/components/popover/popover.d.ts +15 -0
- package/dist/components/popover/popover.js +64 -0
- package/dist/components/progress/progress.d.ts +19 -0
- package/dist/components/progress/progress.js +67 -0
- package/dist/components/prose/prose.d.ts +13 -0
- package/dist/components/prose/prose.js +22 -0
- package/dist/components/quote/quote.d.ts +21 -0
- package/dist/components/quote/quote.js +46 -0
- package/dist/components/radio/radio.d.ts +12 -0
- package/dist/components/radio/radio.js +37 -0
- package/dist/components/reference-entry/reference-entry.d.ts +25 -0
- package/dist/components/reference-entry/reference-entry.js +66 -0
- package/dist/components/section/section.d.ts +13 -0
- package/dist/components/section/section.js +30 -0
- package/dist/components/segmented-control/segmented-control.d.ts +42 -0
- package/dist/components/segmented-control/segmented-control.js +413 -0
- package/dist/components/select/select.d.ts +70 -0
- package/dist/components/select/select.js +189 -0
- package/dist/components/separator/separator.d.ts +12 -0
- package/dist/components/separator/separator.js +29 -0
- package/dist/components/side-nav/side-nav.d.ts +48 -0
- package/dist/components/side-nav/side-nav.js +89 -0
- package/dist/components/site-footer/site-footer-floor.js +48 -0
- package/dist/components/site-footer/site-footer.d.ts +38 -0
- package/dist/components/site-footer/site-footer.js +96 -0
- package/dist/components/site-header/site-header.d.ts +77 -0
- package/dist/components/site-header/site-header.js +214 -0
- package/dist/components/slider/slider.d.ts +42 -0
- package/dist/components/slider/slider.js +279 -0
- package/dist/components/spinner/spinner.d.ts +15 -0
- package/dist/components/spinner/spinner.js +34 -0
- package/dist/components/switch/switch.d.ts +8 -0
- package/dist/components/switch/switch.js +28 -0
- package/dist/components/table/table.d.ts +83 -0
- package/dist/components/table/table.js +123 -0
- package/dist/components/table-of-contents/table-of-contents.d.ts +32 -0
- package/dist/components/table-of-contents/table-of-contents.js +187 -0
- package/dist/components/tabs/tabs.d.ts +28 -0
- package/dist/components/tabs/tabs.js +124 -0
- package/dist/components/text/text.d.ts +16 -0
- package/dist/components/text/text.js +63 -0
- package/dist/components/textarea/textarea.d.ts +19 -0
- package/dist/components/textarea/textarea.js +30 -0
- package/dist/components/time-field/time-field.d.ts +40 -0
- package/dist/components/time-field/time-field.js +123 -0
- package/dist/components/toast/toast.d.ts +55 -0
- package/dist/components/toast/toast.js +129 -0
- package/dist/components/tooltip/tooltip.d.ts +16 -0
- package/dist/components/tooltip/tooltip.js +41 -0
- package/dist/index.d.ts +79 -0
- package/dist/index.js +77 -0
- package/dist/internal/arrow.js +41 -0
- package/dist/internal/disclosure.js +20 -0
- package/dist/internal/field-context.js +21 -0
- package/dist/internal/field.d.ts +23 -0
- package/dist/internal/field.js +49 -0
- package/dist/internal/glyphs.js +84 -0
- package/dist/internal/iso-date.d.ts +2 -0
- package/dist/internal/iso-date.js +268 -0
- package/dist/internal/overlay.js +40 -0
- package/dist/internal/picker-sheet.js +74 -0
- package/dist/internal/popup.js +37 -0
- package/dist/internal/range-shape.js +295 -0
- package/dist/internal/reduced-motion.js +19 -0
- package/dist/internal/scroll-edges.js +45 -0
- package/dist/internal/trough.js +39 -0
- package/dist/internal/typed-input.d.ts +16 -0
- package/dist/internal/typed-input.js +133 -0
- package/dist/lib/cn.d.ts +11 -0
- package/dist/lib/cn.js +98 -0
- package/dist/motion/inertia.js +111 -0
- package/dist/motion/spring-step.js +35 -0
- package/dist/motion/springs.d.ts +78 -0
- package/dist/motion/springs.js +81 -0
- package/package.json +107 -4
- package/src/styles/base.css +93 -0
- package/src/styles/calendar.css +117 -0
- package/src/styles/code.css +252 -0
- package/src/styles/density.css +53 -0
- package/src/styles/depth.css +501 -0
- package/src/styles/grounds.css +191 -0
- package/src/styles/marks.css +50 -0
- package/src/styles/motion.css +55 -0
- package/src/styles/motion.generated.css +101 -0
- package/src/styles/prose.css +252 -0
- package/src/styles/radius.css +20 -0
- package/src/styles/segmented.css +208 -0
- package/src/styles/slider.css +110 -0
- package/src/styles/status.css +108 -0
- package/src/styles/tabs.css +39 -0
- package/src/styles/toast.css +106 -0
- package/src/styles/touch.css +41 -0
- package/src/styles/type.css +54 -0
- package/src/styles.css +38 -0
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { Button } from "../button/button.js";
|
|
3
|
+
import { Icon } from "../icon/icon.js";
|
|
4
|
+
import { Tooltip, TooltipContent, TooltipTrigger } from "../tooltip/tooltip.js";
|
|
5
|
+
import { TickGlyph } from "../../internal/glyphs.js";
|
|
6
|
+
import { cn } from "../../lib/cn.js";
|
|
7
|
+
import { jsx, jsxs } from "react/jsx-runtime";
|
|
8
|
+
import { Copy01Icon } from "@hugeicons/core-free-icons";
|
|
9
|
+
import { useEffect, useRef, useState } from "react";
|
|
10
|
+
// src/components/code-block/code-copy.tsx
|
|
11
|
+
/** How long the tick stands before the copy symbol comes back. */
|
|
12
|
+
const SHOWN_MS = 1600;
|
|
13
|
+
/**
|
|
14
|
+
* The copy button of a code block: the copy symbol shrinks away and a tick is drawn in its
|
|
15
|
+
* place, the tooltip says that it worked, and a screen reader hears it once. After a moment the
|
|
16
|
+
* symbol seats back. If the browser refuses the clipboard, the code is selected instead.
|
|
17
|
+
*/
|
|
18
|
+
function CodeCopy({ text, codeRef, copyLabel = "Code kopieren", copiedLabel = "Kopiert", copiedMessage = "Der Code wurde kopiert.", failedLabel = "Markiert", failedMessage = "Der Code wurde nicht kopiert. Er ist markiert und kann mit ⌘C kopiert werden.", className }) {
|
|
19
|
+
const [state, setState] = useState("idle");
|
|
20
|
+
const timer = useRef(void 0);
|
|
21
|
+
useEffect(() => () => clearTimeout(timer.current), []);
|
|
22
|
+
async function copy() {
|
|
23
|
+
clearTimeout(timer.current);
|
|
24
|
+
try {
|
|
25
|
+
await navigator.clipboard.writeText(text);
|
|
26
|
+
setState("copied");
|
|
27
|
+
} catch {
|
|
28
|
+
const code = codeRef.current;
|
|
29
|
+
if (code) window.getSelection()?.selectAllChildren(code);
|
|
30
|
+
setState("failed");
|
|
31
|
+
}
|
|
32
|
+
timer.current = setTimeout(() => setState("idle"), SHOWN_MS);
|
|
33
|
+
}
|
|
34
|
+
const copied = state === "copied";
|
|
35
|
+
const word = state === "failed" ? failedLabel : copiedLabel;
|
|
36
|
+
return /* @__PURE__ */ jsxs("div", {
|
|
37
|
+
"data-slot": "code-copy",
|
|
38
|
+
className: cn("relative flex items-center", className),
|
|
39
|
+
children: [/* @__PURE__ */ jsxs(Tooltip, { children: [/* @__PURE__ */ jsx(TooltipTrigger, {
|
|
40
|
+
closeOnClick: false,
|
|
41
|
+
render: /* @__PURE__ */ jsx(Button, {
|
|
42
|
+
iconOnly: true,
|
|
43
|
+
size: "sm",
|
|
44
|
+
variant: "quiet",
|
|
45
|
+
"aria-label": copyLabel,
|
|
46
|
+
onClick: () => void copy(),
|
|
47
|
+
className: "text-fg-muted hover:text-fg",
|
|
48
|
+
children: /* @__PURE__ */ jsxs("span", {
|
|
49
|
+
"data-slot": "code-copy-marks",
|
|
50
|
+
"data-copied": copied ? "" : void 0,
|
|
51
|
+
className: "grid place-items-center *:col-start-1 *:row-start-1",
|
|
52
|
+
children: [/* @__PURE__ */ jsx(Icon, {
|
|
53
|
+
"data-slot": "code-copy-symbol",
|
|
54
|
+
icon: Copy01Icon,
|
|
55
|
+
size: 14
|
|
56
|
+
}), /* @__PURE__ */ jsx(TickGlyph, {
|
|
57
|
+
"data-slot": "code-copy-tick",
|
|
58
|
+
width: 14,
|
|
59
|
+
height: 14,
|
|
60
|
+
strokeWidth: 1.5
|
|
61
|
+
})]
|
|
62
|
+
})
|
|
63
|
+
})
|
|
64
|
+
}), /* @__PURE__ */ jsx(TooltipContent, { children: state === "idle" ? copyLabel : word })] }), /* @__PURE__ */ jsx("span", {
|
|
65
|
+
role: "status",
|
|
66
|
+
className: "sr-only",
|
|
67
|
+
children: state === "copied" ? copiedMessage : state === "failed" ? failedMessage : ""
|
|
68
|
+
})]
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
export { CodeCopy };
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { LineSpec } from "./lines.js";
|
|
2
|
+
export type CodeLinesOptions = {
|
|
3
|
+
/** Numbers before each line. For code someone will talk about by line; off for snippets. */
|
|
4
|
+
lineNumbers?: boolean;
|
|
5
|
+
/** Lines to point at: a tint and the accent along the edge. `"3-5"`, `7` or `[2, [4, 6]]`. */
|
|
6
|
+
marked?: LineSpec;
|
|
7
|
+
/** Lines a change added: a green wash and a plus. */
|
|
8
|
+
added?: LineSpec;
|
|
9
|
+
/** Lines a change removed: a red wash and a minus. They are left out when the code is copied. */
|
|
10
|
+
removed?: LineSpec;
|
|
11
|
+
/** Long lines break instead of scrolling sideways. For prose-like code: Markdown, a log. */
|
|
12
|
+
wrap?: boolean;
|
|
13
|
+
/** What the sign of an added and of a removed line is read out as. */
|
|
14
|
+
addedLabel?: string;
|
|
15
|
+
removedLabel?: string;
|
|
16
|
+
};
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { lineKinds } from "./lines.js";
|
|
2
|
+
import { jsx } from "react/jsx-runtime";
|
|
3
|
+
// src/components/code-block/code-lines-view.tsx
|
|
4
|
+
/**
|
|
5
|
+
* The lines of a block once they are coloured: a `<code>` of one span per line, each run in its
|
|
6
|
+
* colour. It only draws. Whoever coloured the lines (the highlighter in `CodeLines`, an app in
|
|
7
|
+
* `CodeBlockLive`) is not its business, and no highlighter may be imported here: this is the
|
|
8
|
+
* half that may travel to the browser.
|
|
9
|
+
*/
|
|
10
|
+
function CodeLinesView({ lines, lineNumbers = false, marked, added, removed, wrap = false, addedLabel = "hinzugefügt", removedLabel = "entfernt" }) {
|
|
11
|
+
const kinds = lineKinds(lines.length, {
|
|
12
|
+
marked,
|
|
13
|
+
added,
|
|
14
|
+
removed
|
|
15
|
+
});
|
|
16
|
+
const changes = kinds.some((kind) => kind === "added" || kind === "removed");
|
|
17
|
+
const digits = lineNumbers ? String(lines.length).length : 0;
|
|
18
|
+
const numberRoom = lineNumbers ? `${digits}ch + 1.25rem` : "0px";
|
|
19
|
+
const signRoom = changes ? "1ch + 0.75rem" : "0px";
|
|
20
|
+
const style = {
|
|
21
|
+
"--code-digits": `${digits}ch`,
|
|
22
|
+
"--code-sign-at": `calc(${numberRoom})`,
|
|
23
|
+
"--code-gutter": `calc(${numberRoom} + ${signRoom})`
|
|
24
|
+
};
|
|
25
|
+
return /* @__PURE__ */ jsx("code", {
|
|
26
|
+
"data-slot": "code-block-code",
|
|
27
|
+
"data-line-numbers": lineNumbers ? "" : void 0,
|
|
28
|
+
"data-wrap": wrap ? "" : void 0,
|
|
29
|
+
style,
|
|
30
|
+
children: lines.map((tokens, index) => {
|
|
31
|
+
const kind = kinds[index];
|
|
32
|
+
return /* @__PURE__ */ jsx("span", {
|
|
33
|
+
"data-slot": "code-block-line",
|
|
34
|
+
"data-kind": kind,
|
|
35
|
+
"data-label": kind === "added" ? addedLabel : kind === "removed" ? removedLabel : void 0,
|
|
36
|
+
children: tokens.map((token, at) => /* @__PURE__ */ jsx("span", {
|
|
37
|
+
style: {
|
|
38
|
+
color: token.color,
|
|
39
|
+
fontStyle: token.italic ? "italic" : void 0,
|
|
40
|
+
fontWeight: token.bold ? 600 : void 0
|
|
41
|
+
},
|
|
42
|
+
children: token.content
|
|
43
|
+
}, at))
|
|
44
|
+
}, index);
|
|
45
|
+
})
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
export { CodeLinesView };
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { CodeLinesView } from "./code-lines-view.js";
|
|
2
|
+
import { highlightLines } from "./highlight.js";
|
|
3
|
+
import { trimCode } from "./lines.js";
|
|
4
|
+
import { jsx } from "react/jsx-runtime";
|
|
5
|
+
// src/components/code-block/code-lines.tsx
|
|
6
|
+
/**
|
|
7
|
+
* The highlighted lines of a block: the code is coloured here, where it renders, and drawn by
|
|
8
|
+
* `CodeLinesView`. Server-safe. In a Server Component the highlighter stays on the server and
|
|
9
|
+
* only the coloured spans travel.
|
|
10
|
+
*/
|
|
11
|
+
function CodeLines({ code, language, ...options }) {
|
|
12
|
+
return /* @__PURE__ */ jsx(CodeLinesView, {
|
|
13
|
+
lines: highlightLines(trimCode(code), language),
|
|
14
|
+
...options
|
|
15
|
+
});
|
|
16
|
+
}
|
|
17
|
+
export { CodeLines };
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { useScrollEdges } from "../../internal/scroll-edges.js";
|
|
3
|
+
import { cn } from "../../lib/cn.js";
|
|
4
|
+
import { jsx } from "react/jsx-runtime";
|
|
5
|
+
import { useRef } from "react";
|
|
6
|
+
// src/components/code-block/code-scroll.tsx
|
|
7
|
+
/**
|
|
8
|
+
* The code's own scrolling region. A long line scrolls inside the block, never the page, and
|
|
9
|
+
* the region takes keyboard focus so it can be scrolled without a pointer. Its edges fade where
|
|
10
|
+
* more code waits (`useScrollEdges`).
|
|
11
|
+
*/
|
|
12
|
+
function CodeScroll({ label, codeRef, className, preClassName, children }) {
|
|
13
|
+
const ref = useRef(null);
|
|
14
|
+
useScrollEdges(ref);
|
|
15
|
+
return /* @__PURE__ */ jsx("div", {
|
|
16
|
+
ref,
|
|
17
|
+
"data-slot": "code-block-scroll",
|
|
18
|
+
role: "group",
|
|
19
|
+
"aria-label": label,
|
|
20
|
+
tabIndex: 0,
|
|
21
|
+
className: cn("min-w-0 overflow-x-auto focus-visible:outline-hidden", className),
|
|
22
|
+
children: /* @__PURE__ */ jsx("pre", {
|
|
23
|
+
ref: codeRef,
|
|
24
|
+
className: cn("m-0 w-full font-mono", preClassName),
|
|
25
|
+
children
|
|
26
|
+
})
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
export { CodeScroll };
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
// src/components/code-block/code-surface.ts
|
|
2
|
+
/** The block around the code: corners of a sheet, a hairline, and the type of a machine's text. */
|
|
3
|
+
const codeRoot = [
|
|
4
|
+
"relative flex min-w-0 flex-col overflow-hidden rounded-xl border border-hairline bg-code-face",
|
|
5
|
+
"font-mono text-[0.8125rem] leading-[1.375rem] text-code-text [--code-pad-x:1rem]",
|
|
6
|
+
"[--code-face:var(--lf-code-face)]",
|
|
7
|
+
"has-[[data-slot=code-block-scroll]:focus-visible]:outline-2",
|
|
8
|
+
"has-[[data-slot=code-block-scroll]:focus-visible]:outline-offset-2",
|
|
9
|
+
"has-[[data-slot=code-block-scroll]:focus-visible]:outline-accent"
|
|
10
|
+
];
|
|
11
|
+
/** The strip at the top: the file's symbol and name, or the tabs, and the copy button. */
|
|
12
|
+
const codeHead = "flex min-h-10 shrink-0 items-center gap-2 border-b border-hairline bg-code-head py-1.5 pr-1.5 pl-(--code-pad-x) font-sans text-fg-muted";
|
|
13
|
+
export { codeHead, codeRoot };
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { cn } from "../../lib/cn.js";
|
|
3
|
+
import { CodeCopy } from "./code-copy.js";
|
|
4
|
+
import { CodeScroll } from "./code-scroll.js";
|
|
5
|
+
import { codeHead, codeRoot } from "./code-surface.js";
|
|
6
|
+
import { readChoice, subscribeChoices, writeChoice } from "./code-tabs-store.js";
|
|
7
|
+
import { jsx, jsxs } from "react/jsx-runtime";
|
|
8
|
+
import { Children, useEffect, useMemo, useRef, useState, useSyncExternalStore } from "react";
|
|
9
|
+
import { Tabs } from "@base-ui/react/tabs";
|
|
10
|
+
// src/components/code-block/code-tabs-frame.tsx
|
|
11
|
+
/**
|
|
12
|
+
* The client half of CodeTabs: a row of tabs in the header, each with its mark, and one soft
|
|
13
|
+
* highlight that glides to the chosen tab (`glide`, placed by Base UI's measure and moved by
|
|
14
|
+
* `translate`). The copy button takes the code that is shown.
|
|
15
|
+
*/
|
|
16
|
+
function CodeTabsFrame({ children, tabs, group, defaultValue, label, className, ...labels }) {
|
|
17
|
+
const names = tabs.map((tab) => tab.label);
|
|
18
|
+
const [own, setOwn] = useState(defaultValue ?? names[0]);
|
|
19
|
+
const shared = useSyncExternalStore(subscribeChoices, () => group ? readChoice(group) : void 0, () => void 0);
|
|
20
|
+
const value = shared && names.includes(shared) ? shared : own;
|
|
21
|
+
const index = Math.max(0, names.indexOf(value ?? ""));
|
|
22
|
+
const [placed, setPlaced] = useState(false);
|
|
23
|
+
useEffect(() => {
|
|
24
|
+
const frame = requestAnimationFrame(() => setPlaced(true));
|
|
25
|
+
return () => cancelAnimationFrame(frame);
|
|
26
|
+
}, []);
|
|
27
|
+
const pres = useRef([]);
|
|
28
|
+
const activeCode = useMemo(() => ({ get current() {
|
|
29
|
+
return pres.current[index] ?? null;
|
|
30
|
+
} }), [index]);
|
|
31
|
+
const panels = Children.toArray(children);
|
|
32
|
+
return /* @__PURE__ */ jsxs(Tabs.Root, {
|
|
33
|
+
"data-slot": "code-tabs",
|
|
34
|
+
value,
|
|
35
|
+
onValueChange: (next) => {
|
|
36
|
+
setOwn(next);
|
|
37
|
+
if (group) writeChoice(group, next);
|
|
38
|
+
},
|
|
39
|
+
className: cn(codeRoot, className),
|
|
40
|
+
children: [/* @__PURE__ */ jsxs("div", {
|
|
41
|
+
"data-slot": "code-tabs-header",
|
|
42
|
+
className: cn(codeHead, "pl-1.5"),
|
|
43
|
+
children: [/* @__PURE__ */ jsxs(Tabs.List, {
|
|
44
|
+
"data-slot": "code-tabs-list",
|
|
45
|
+
"aria-label": label,
|
|
46
|
+
className: "relative isolate flex min-w-0 flex-1 gap-0.5",
|
|
47
|
+
children: [tabs.map((tab) => /* @__PURE__ */ jsxs(Tabs.Tab, {
|
|
48
|
+
value: tab.label,
|
|
49
|
+
"data-slot": "code-tab",
|
|
50
|
+
className: cn("group touch-target relative inline-flex h-(--control-sm) items-center gap-1.5 rounded-sm pr-2.5 pl-2 text-sm font-medium whitespace-nowrap text-fg-muted select-none", "transition-colors duration-150 ease-out hover:text-fg data-active:text-fg", "focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-accent"),
|
|
51
|
+
children: [/* @__PURE__ */ jsx("span", {
|
|
52
|
+
"aria-hidden": true,
|
|
53
|
+
className: "mark group-data-active:mark-on"
|
|
54
|
+
}), tab.label]
|
|
55
|
+
}, tab.label)), /* @__PURE__ */ jsx(Tabs.Indicator, {
|
|
56
|
+
"data-slot": "code-tabs-indicator",
|
|
57
|
+
className: cn("absolute top-(--active-tab-top) left-0 -z-10 h-(--active-tab-height) w-(--active-tab-width) translate-x-(--active-tab-left) rounded-sm bg-hover", "forced-colors:outline forced-colors:-outline-offset-1", placed && "transition-[translate,width] spring-glide")
|
|
58
|
+
})]
|
|
59
|
+
}), /* @__PURE__ */ jsx(CodeCopy, {
|
|
60
|
+
text: tabs[index]?.copyText ?? "",
|
|
61
|
+
codeRef: activeCode,
|
|
62
|
+
...labels
|
|
63
|
+
})]
|
|
64
|
+
}), panels.map((panel, at) => /* @__PURE__ */ jsx(Tabs.Panel, {
|
|
65
|
+
value: names[at],
|
|
66
|
+
"data-slot": "code-tabs-panel",
|
|
67
|
+
tabIndex: -1,
|
|
68
|
+
className: "outline-hidden",
|
|
69
|
+
children: /* @__PURE__ */ jsx(CodeScroll, {
|
|
70
|
+
label: `${label}: ${names[at]}`,
|
|
71
|
+
codeRef: {
|
|
72
|
+
get current() {
|
|
73
|
+
return pres.current[at] ?? null;
|
|
74
|
+
},
|
|
75
|
+
set current(element) {
|
|
76
|
+
pres.current[at] = element;
|
|
77
|
+
}
|
|
78
|
+
},
|
|
79
|
+
preClassName: "py-3",
|
|
80
|
+
children: panel
|
|
81
|
+
})
|
|
82
|
+
}, names[at]))]
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
export { CodeTabsFrame };
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
// src/components/code-block/code-tabs-store.ts
|
|
2
|
+
const listeners = /* @__PURE__ */ new Set();
|
|
3
|
+
const choices = /* @__PURE__ */ new Map();
|
|
4
|
+
const storageKey = (group) => `lanfermann.code-tabs.${group}`;
|
|
5
|
+
function readChoice(group) {
|
|
6
|
+
if (!choices.has(group)) try {
|
|
7
|
+
const stored = window.localStorage.getItem(storageKey(group));
|
|
8
|
+
if (stored) choices.set(group, stored);
|
|
9
|
+
} catch {}
|
|
10
|
+
return choices.get(group);
|
|
11
|
+
}
|
|
12
|
+
function writeChoice(group, value) {
|
|
13
|
+
choices.set(group, value);
|
|
14
|
+
try {
|
|
15
|
+
window.localStorage.setItem(storageKey(group), value);
|
|
16
|
+
} catch {}
|
|
17
|
+
for (const listener of listeners) listener();
|
|
18
|
+
}
|
|
19
|
+
function subscribeChoices(listener) {
|
|
20
|
+
listeners.add(listener);
|
|
21
|
+
return () => {
|
|
22
|
+
listeners.delete(listener);
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
export { readChoice, subscribeChoices, writeChoice };
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { CodeCopyLabels } from "./code-copy.js";
|
|
2
|
+
import { CodeLinesOptions } from "./code-lines-view.js";
|
|
3
|
+
import { CodeLanguage } from "./highlight.js";
|
|
4
|
+
export type CodeTab = CodeLinesOptions & {
|
|
5
|
+
/** The tab: a noun of one or two words, often the tool's own name („bun“, „npm“). */
|
|
6
|
+
label: string;
|
|
7
|
+
code: string;
|
|
8
|
+
language?: CodeLanguage;
|
|
9
|
+
};
|
|
10
|
+
export type CodeTabsProps = CodeCopyLabels & {
|
|
11
|
+
items: CodeTab[];
|
|
12
|
+
/**
|
|
13
|
+
* Blocks with the same group share the reader's choice: picking „bun“ in one shows bun in all
|
|
14
|
+
* of them, and the choice is remembered for the next visit.
|
|
15
|
+
*/
|
|
16
|
+
group?: string;
|
|
17
|
+
/** The tab shown first, until the reader picks. The first tab by default. */
|
|
18
|
+
defaultValue?: string;
|
|
19
|
+
/** The name of the tab row, and of each code region after it. */
|
|
20
|
+
label?: string;
|
|
21
|
+
className?: string;
|
|
22
|
+
};
|
|
23
|
+
/**
|
|
24
|
+
* One block, several versions of the same code: the install command for each package manager,
|
|
25
|
+
* or a component and its stylesheet. The tabs sit in the header with their marks; the copy
|
|
26
|
+
* button copies the version that is shown.
|
|
27
|
+
*/
|
|
28
|
+
export declare function CodeTabs({ items, group, defaultValue, label, className, ...copyLabels }: CodeTabsProps): import("react").JSX.Element;
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { CodeLines } from "./code-lines.js";
|
|
2
|
+
import { CodeTabsFrame } from "./code-tabs-frame.js";
|
|
3
|
+
import { copyText } from "./lines.js";
|
|
4
|
+
import { jsx } from "react/jsx-runtime";
|
|
5
|
+
// src/components/code-block/code-tabs.tsx
|
|
6
|
+
/**
|
|
7
|
+
* One block, several versions of the same code: the install command for each package manager,
|
|
8
|
+
* or a component and its stylesheet. The tabs sit in the header with their marks; the copy
|
|
9
|
+
* button copies the version that is shown.
|
|
10
|
+
*/
|
|
11
|
+
function CodeTabs({ items, group, defaultValue, label = "Code", className, ...copyLabels }) {
|
|
12
|
+
return /* @__PURE__ */ jsx(CodeTabsFrame, {
|
|
13
|
+
tabs: items.map((item) => ({
|
|
14
|
+
label: item.label,
|
|
15
|
+
copyText: copyText(item.code, item.removed)
|
|
16
|
+
})),
|
|
17
|
+
group,
|
|
18
|
+
defaultValue,
|
|
19
|
+
label,
|
|
20
|
+
className,
|
|
21
|
+
...copyLabels,
|
|
22
|
+
children: items.map(({ label: name, code, language = "text", ...lines }) => /* @__PURE__ */ jsx(CodeLines, {
|
|
23
|
+
code,
|
|
24
|
+
language,
|
|
25
|
+
...lines
|
|
26
|
+
}, name))
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
export { CodeTabs };
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/** The languages a block can colour, by the names people write, and how many lines it may hold. */
|
|
2
|
+
declare const languages: {
|
|
3
|
+
readonly tsx: "tsx";
|
|
4
|
+
readonly ts: "typescript";
|
|
5
|
+
readonly typescript: "typescript";
|
|
6
|
+
readonly jsx: "jsx";
|
|
7
|
+
readonly js: "javascript";
|
|
8
|
+
readonly javascript: "javascript";
|
|
9
|
+
readonly json: "json";
|
|
10
|
+
readonly css: "css";
|
|
11
|
+
readonly html: "html";
|
|
12
|
+
readonly sh: "shellscript";
|
|
13
|
+
readonly bash: "shellscript";
|
|
14
|
+
readonly shell: "shellscript";
|
|
15
|
+
readonly zsh: "shellscript";
|
|
16
|
+
readonly ps1: "powershell";
|
|
17
|
+
readonly powershell: "powershell";
|
|
18
|
+
readonly env: "dotenv";
|
|
19
|
+
readonly dotenv: "dotenv";
|
|
20
|
+
readonly toml: "toml";
|
|
21
|
+
readonly ini: "ini";
|
|
22
|
+
readonly swift: "swift";
|
|
23
|
+
readonly py: "python";
|
|
24
|
+
readonly python: "python";
|
|
25
|
+
readonly yaml: "yaml";
|
|
26
|
+
readonly yml: "yaml";
|
|
27
|
+
readonly sql: "sql";
|
|
28
|
+
readonly md: "markdown";
|
|
29
|
+
readonly markdown: "markdown";
|
|
30
|
+
readonly diff: "diff";
|
|
31
|
+
readonly text: "text";
|
|
32
|
+
};
|
|
33
|
+
export type CodeLanguage = keyof typeof languages;
|
|
34
|
+
export {};
|
|
@@ -0,0 +1,258 @@
|
|
|
1
|
+
import { createHighlighterCoreSync } from "shiki/core";
|
|
2
|
+
import { createJavaScriptRegexEngine } from "shiki/engine/javascript";
|
|
3
|
+
import css from "shiki/langs/css.mjs";
|
|
4
|
+
import diff from "shiki/langs/diff.mjs";
|
|
5
|
+
import dotenv from "shiki/langs/dotenv.mjs";
|
|
6
|
+
import html from "shiki/langs/html.mjs";
|
|
7
|
+
import ini from "shiki/langs/ini.mjs";
|
|
8
|
+
import javascript from "shiki/langs/javascript.mjs";
|
|
9
|
+
import json from "shiki/langs/json.mjs";
|
|
10
|
+
import jsx from "shiki/langs/jsx.mjs";
|
|
11
|
+
import markdown from "shiki/langs/markdown.mjs";
|
|
12
|
+
import powershell from "shiki/langs/powershell.mjs";
|
|
13
|
+
import python from "shiki/langs/python.mjs";
|
|
14
|
+
import shellscript from "shiki/langs/shellscript.mjs";
|
|
15
|
+
import sql from "shiki/langs/sql.mjs";
|
|
16
|
+
import swift from "shiki/langs/swift.mjs";
|
|
17
|
+
import toml from "shiki/langs/toml.mjs";
|
|
18
|
+
import tsx from "shiki/langs/tsx.mjs";
|
|
19
|
+
import typescript from "shiki/langs/typescript.mjs";
|
|
20
|
+
import yaml from "shiki/langs/yaml.mjs";
|
|
21
|
+
// src/components/code-block/highlight.ts
|
|
22
|
+
/** The languages a block can colour, by the names people write, and how many lines it may hold. */
|
|
23
|
+
const languages = {
|
|
24
|
+
tsx: "tsx",
|
|
25
|
+
ts: "typescript",
|
|
26
|
+
typescript: "typescript",
|
|
27
|
+
jsx: "jsx",
|
|
28
|
+
js: "javascript",
|
|
29
|
+
javascript: "javascript",
|
|
30
|
+
json: "json",
|
|
31
|
+
css: "css",
|
|
32
|
+
html: "html",
|
|
33
|
+
sh: "shellscript",
|
|
34
|
+
bash: "shellscript",
|
|
35
|
+
shell: "shellscript",
|
|
36
|
+
zsh: "shellscript",
|
|
37
|
+
ps1: "powershell",
|
|
38
|
+
powershell: "powershell",
|
|
39
|
+
env: "dotenv",
|
|
40
|
+
dotenv: "dotenv",
|
|
41
|
+
toml: "toml",
|
|
42
|
+
ini: "ini",
|
|
43
|
+
swift: "swift",
|
|
44
|
+
py: "python",
|
|
45
|
+
python: "python",
|
|
46
|
+
yaml: "yaml",
|
|
47
|
+
yml: "yaml",
|
|
48
|
+
sql: "sql",
|
|
49
|
+
md: "markdown",
|
|
50
|
+
markdown: "markdown",
|
|
51
|
+
diff: "diff",
|
|
52
|
+
text: "text"
|
|
53
|
+
};
|
|
54
|
+
/** Whether a language is a shell: its block gets the terminal symbol, a one-liner gets a prompt. */
|
|
55
|
+
function isShell(language) {
|
|
56
|
+
return languages[language] === "shellscript";
|
|
57
|
+
}
|
|
58
|
+
const v = (role) => `var(--lf-code-${role})`;
|
|
59
|
+
/**
|
|
60
|
+
* Ten roles instead of an editor theme's fifty: code here is read, not edited. The colours are
|
|
61
|
+
* CSS variables (src/styles/code.css), so the same spans read right on every ground.
|
|
62
|
+
*/
|
|
63
|
+
const theme = {
|
|
64
|
+
name: "lanfermann",
|
|
65
|
+
type: "light",
|
|
66
|
+
colors: {
|
|
67
|
+
"editor.foreground": v("text"),
|
|
68
|
+
"editor.background": "transparent"
|
|
69
|
+
},
|
|
70
|
+
tokenColors: [
|
|
71
|
+
{ settings: { foreground: v("text") } },
|
|
72
|
+
{
|
|
73
|
+
scope: [
|
|
74
|
+
"comment",
|
|
75
|
+
"punctuation.definition.comment",
|
|
76
|
+
"string.quoted.docstring"
|
|
77
|
+
],
|
|
78
|
+
settings: {
|
|
79
|
+
foreground: v("comment"),
|
|
80
|
+
fontStyle: "italic"
|
|
81
|
+
}
|
|
82
|
+
},
|
|
83
|
+
{
|
|
84
|
+
scope: [
|
|
85
|
+
"punctuation",
|
|
86
|
+
"meta.brace",
|
|
87
|
+
"keyword.operator",
|
|
88
|
+
"storage.type.function.arrow",
|
|
89
|
+
"meta.tag.sgml.doctype"
|
|
90
|
+
],
|
|
91
|
+
settings: { foreground: v("punct") }
|
|
92
|
+
},
|
|
93
|
+
{
|
|
94
|
+
scope: [
|
|
95
|
+
"keyword",
|
|
96
|
+
"storage",
|
|
97
|
+
"storage.type",
|
|
98
|
+
"storage.modifier",
|
|
99
|
+
"keyword.operator.new",
|
|
100
|
+
"keyword.operator.expression",
|
|
101
|
+
"keyword.operator.word",
|
|
102
|
+
"keyword.operator.logical.python",
|
|
103
|
+
"variable.language",
|
|
104
|
+
"markup.heading",
|
|
105
|
+
"entity.name.section"
|
|
106
|
+
],
|
|
107
|
+
settings: { foreground: v("keyword") }
|
|
108
|
+
},
|
|
109
|
+
{
|
|
110
|
+
scope: [
|
|
111
|
+
"string",
|
|
112
|
+
"punctuation.definition.string",
|
|
113
|
+
"string.regexp",
|
|
114
|
+
"markup.inline.raw",
|
|
115
|
+
"markup.fenced_code",
|
|
116
|
+
"markup.inserted"
|
|
117
|
+
],
|
|
118
|
+
settings: { foreground: v("string") }
|
|
119
|
+
},
|
|
120
|
+
{
|
|
121
|
+
scope: [
|
|
122
|
+
"constant.numeric",
|
|
123
|
+
"constant.language",
|
|
124
|
+
"constant.character",
|
|
125
|
+
"constant.other.color",
|
|
126
|
+
"support.constant",
|
|
127
|
+
"variable.other.enummember",
|
|
128
|
+
"constant.other.option",
|
|
129
|
+
"keyword.other.unit"
|
|
130
|
+
],
|
|
131
|
+
settings: { foreground: v("number") }
|
|
132
|
+
},
|
|
133
|
+
{
|
|
134
|
+
scope: [
|
|
135
|
+
"entity.name.function",
|
|
136
|
+
"support.function",
|
|
137
|
+
"meta.function-call entity.name.function",
|
|
138
|
+
"entity.name.command",
|
|
139
|
+
"markup.deleted"
|
|
140
|
+
],
|
|
141
|
+
settings: { foreground: v("function") }
|
|
142
|
+
},
|
|
143
|
+
{
|
|
144
|
+
scope: [
|
|
145
|
+
"entity.name.type",
|
|
146
|
+
"entity.name.class",
|
|
147
|
+
"entity.other.inherited-class",
|
|
148
|
+
"support.type",
|
|
149
|
+
"support.class",
|
|
150
|
+
"storage.type.primitive",
|
|
151
|
+
"entity.name.namespace"
|
|
152
|
+
],
|
|
153
|
+
settings: { foreground: v("type") }
|
|
154
|
+
},
|
|
155
|
+
{
|
|
156
|
+
scope: [
|
|
157
|
+
"entity.name.tag",
|
|
158
|
+
"support.class.component",
|
|
159
|
+
"punctuation.definition.tag"
|
|
160
|
+
],
|
|
161
|
+
settings: { foreground: v("tag") }
|
|
162
|
+
},
|
|
163
|
+
{
|
|
164
|
+
scope: [
|
|
165
|
+
"entity.other.attribute-name",
|
|
166
|
+
"support.type.property-name",
|
|
167
|
+
"meta.object-literal.key",
|
|
168
|
+
"entity.name.tag.yaml",
|
|
169
|
+
"variable.other.property"
|
|
170
|
+
],
|
|
171
|
+
settings: { foreground: v("attribute") }
|
|
172
|
+
},
|
|
173
|
+
{
|
|
174
|
+
scope: ["markup.bold"],
|
|
175
|
+
settings: { fontStyle: "bold" }
|
|
176
|
+
},
|
|
177
|
+
{
|
|
178
|
+
scope: ["markup.italic"],
|
|
179
|
+
settings: { fontStyle: "italic" }
|
|
180
|
+
}
|
|
181
|
+
]
|
|
182
|
+
};
|
|
183
|
+
let highlighter;
|
|
184
|
+
function getHighlighter() {
|
|
185
|
+
highlighter ??= createHighlighterCoreSync({
|
|
186
|
+
themes: [theme],
|
|
187
|
+
langs: [
|
|
188
|
+
css,
|
|
189
|
+
diff,
|
|
190
|
+
dotenv,
|
|
191
|
+
html,
|
|
192
|
+
ini,
|
|
193
|
+
javascript,
|
|
194
|
+
json,
|
|
195
|
+
jsx,
|
|
196
|
+
markdown,
|
|
197
|
+
powershell,
|
|
198
|
+
python,
|
|
199
|
+
shellscript,
|
|
200
|
+
sql,
|
|
201
|
+
swift,
|
|
202
|
+
toml,
|
|
203
|
+
tsx,
|
|
204
|
+
typescript,
|
|
205
|
+
yaml
|
|
206
|
+
],
|
|
207
|
+
engine: createJavaScriptRegexEngine({ target: "ES2018" })
|
|
208
|
+
});
|
|
209
|
+
return highlighter;
|
|
210
|
+
}
|
|
211
|
+
/**
|
|
212
|
+
* Runs `tokenize` with a clock that stands still. Shiki's tokenizer reads `Date.now()` once per
|
|
213
|
+
* line to enforce a time limit, even when the limit is off. Next.js with `cacheComponents`
|
|
214
|
+
* refuses a page that reads the clock while it is prerendered (the build fails with "Next.js
|
|
215
|
+
* encountered the unstable value `Date.now()`"), and rightly: a limit that trips cuts a slow
|
|
216
|
+
* line short and the page changes from one build to the next. With the limit off
|
|
217
|
+
* (`tokenizeTimeLimit: 0`) the clock decides nothing, so it is answered with a constant for the
|
|
218
|
+
* length of this one synchronous call, and the real `Date.now` is put back before anything else
|
|
219
|
+
* can run.
|
|
220
|
+
*/
|
|
221
|
+
function withoutClock(tokenize) {
|
|
222
|
+
const now = Date.now;
|
|
223
|
+
Date.now = () => 0;
|
|
224
|
+
try {
|
|
225
|
+
return tokenize();
|
|
226
|
+
} finally {
|
|
227
|
+
Date.now = now;
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
/**
|
|
231
|
+
* Splits code into lines of coloured runs. Neighbouring runs of the same colour are merged, and
|
|
232
|
+
* so are spaces into the run before them, so a line is a handful of spans, not one per token.
|
|
233
|
+
*/
|
|
234
|
+
function highlightLines(code, language = "text") {
|
|
235
|
+
const lang = languages[language];
|
|
236
|
+
if (lang === "text") return code.split("\n").map((line) => line ? [{ content: line }] : []);
|
|
237
|
+
return withoutClock(() => getHighlighter().codeToTokensBase(code, {
|
|
238
|
+
lang,
|
|
239
|
+
theme: "lanfermann",
|
|
240
|
+
tokenizeTimeLimit: 0
|
|
241
|
+
})).map((line) => {
|
|
242
|
+
const merged = [];
|
|
243
|
+
for (const token of line) {
|
|
244
|
+
const next = {
|
|
245
|
+
content: token.content,
|
|
246
|
+
color: token.color,
|
|
247
|
+
italic: Boolean((token.fontStyle ?? 0) & 1),
|
|
248
|
+
bold: Boolean((token.fontStyle ?? 0) & 2)
|
|
249
|
+
};
|
|
250
|
+
const last = merged.at(-1);
|
|
251
|
+
const blank = next.content.trim() === "";
|
|
252
|
+
if (last && (blank || last.color === next.color && last.italic === next.italic && last.bold === next.bold)) last.content += next.content;
|
|
253
|
+
else merged.push(next);
|
|
254
|
+
}
|
|
255
|
+
return merged;
|
|
256
|
+
});
|
|
257
|
+
}
|
|
258
|
+
export { highlightLines, isShell };
|