@multiplatform.one/markdown 11.12.2 → 11.13.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/dist/cjs/MdxCodeBlock/index.cjs +20 -98
- package/dist/cjs/MdxCodeBlock/index.native.cjs +23 -104
- package/dist/cjs/MdxCodeBlock/index.native.js +23 -104
- package/dist/cjs/MdxCodeBlock/index.native.js.map +1 -1
- package/dist/cjs/components/CodeBlock.cjs +4 -25
- package/dist/cjs/components/CodeBlock.native.cjs +4 -26
- package/dist/cjs/components/CodeBlock.native.js +4 -26
- package/dist/cjs/components/CodeBlock.native.js.map +1 -1
- package/dist/esm/MdxCodeBlock/index.mjs +23 -101
- package/dist/esm/MdxCodeBlock/index.mjs.map +1 -1
- package/dist/esm/MdxCodeBlock/index.native.js +26 -107
- package/dist/esm/MdxCodeBlock/index.native.js.map +1 -1
- package/dist/esm/components/CodeBlock.mjs +5 -26
- package/dist/esm/components/CodeBlock.mjs.map +1 -1
- package/dist/esm/components/CodeBlock.native.js +5 -27
- package/dist/esm/components/CodeBlock.native.js.map +1 -1
- package/dist/jsx/MdxCodeBlock/index.mjs +23 -101
- package/dist/jsx/MdxCodeBlock/index.mjs.map +1 -1
- package/dist/jsx/MdxCodeBlock/index.native.cjs +23 -104
- package/dist/jsx/MdxCodeBlock/index.native.js +23 -104
- package/dist/jsx/MdxCodeBlock/index.native.js.map +1 -1
- package/dist/jsx/components/CodeBlock.mjs +5 -26
- package/dist/jsx/components/CodeBlock.mjs.map +1 -1
- package/dist/jsx/components/CodeBlock.native.cjs +4 -26
- package/dist/jsx/components/CodeBlock.native.js +4 -26
- package/dist/jsx/components/CodeBlock.native.js.map +1 -1
- package/package.json +10 -10
- package/src/MdxCodeBlock/index.tsx +33 -127
- package/src/MdxCodeBlock/mdxCodeBlock.spec.tsx +50 -49
- package/src/components/CodeBlock.tsx +12 -46
- package/src/stories/Code.stories.tsx +2 -1
- package/types/MdxCodeBlock/index.d.ts +6 -0
- package/types/MdxCodeBlock/index.d.ts.map +1 -1
- package/types/components/CodeBlock.d.ts +4 -6
- package/types/components/CodeBlock.d.ts.map +1 -1
|
@@ -25,37 +25,15 @@ __export(CodeBlock_exports, { CodeBlock: () => CodeBlock });
|
|
|
25
25
|
module.exports = __toCommonJS(CodeBlock_exports);
|
|
26
26
|
var import_jsx_runtime = require("react/jsx-runtime");
|
|
27
27
|
var import_components = require("@multiplatform.one/components");
|
|
28
|
-
var import_theme = require("@multiplatform.one/theme");
|
|
29
28
|
var import_tamagui = require("tamagui");
|
|
30
|
-
var import_colorRamps = require("../shared/colorRamps.native.js");
|
|
31
29
|
function CodeBlock(param) {
|
|
32
30
|
var { children, lang, theme: themeProp = "auto" } = param;
|
|
33
|
-
var _knobProps_borderRadiusNested;
|
|
34
|
-
var { knobProps } = (0, import_theme.useResolvedKnobs)();
|
|
35
|
-
var tokens = (0, import_components.useShikiTokens)(children, lang || "text", themeProp === "auto" ? void 0 : themeProp);
|
|
36
31
|
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_tamagui.View, {
|
|
37
|
-
...(_knobProps_borderRadiusNested = knobProps.borderRadiusNested) !== null && _knobProps_borderRadiusNested !== void 0 ? _knobProps_borderRadiusNested : { borderRadius: "4" },
|
|
38
|
-
backgroundColor: import_colorRamps.markdownCodeColors.background.base,
|
|
39
|
-
borderWidth: knobProps.borderRadius.borderWidth,
|
|
40
|
-
borderColor: (0, import_theme.edgeColorAt)(import_colorRamps.markdownCodeColors.border.base, knobProps.borderWidthStop),
|
|
41
|
-
...knobProps.panelPadding,
|
|
42
32
|
marginVertical: "2",
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_components.Code, {
|
|
48
|
-
backgroundColor: "transparent",
|
|
49
|
-
borderWidth: 0,
|
|
50
|
-
padding: 0,
|
|
51
|
-
color: import_colorRamps.markdownCodeColors.text.base,
|
|
52
|
-
fontSize: knobProps.sizeToken,
|
|
53
|
-
children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_components.HighlightedCode, {
|
|
54
|
-
tokens,
|
|
55
|
-
fontSize: knobProps.sizeToken,
|
|
56
|
-
children
|
|
57
|
-
})
|
|
58
|
-
})
|
|
33
|
+
children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_components.CodeBlock, {
|
|
34
|
+
language: lang || "text",
|
|
35
|
+
theme: themeProp === "auto" ? void 0 : themeProp,
|
|
36
|
+
children
|
|
59
37
|
})
|
|
60
38
|
});
|
|
61
39
|
}
|
|
@@ -25,37 +25,15 @@ __export(CodeBlock_exports, { CodeBlock: () => CodeBlock });
|
|
|
25
25
|
module.exports = __toCommonJS(CodeBlock_exports);
|
|
26
26
|
var import_jsx_runtime = require("react/jsx-runtime");
|
|
27
27
|
var import_components = require("@multiplatform.one/components");
|
|
28
|
-
var import_theme = require("@multiplatform.one/theme");
|
|
29
28
|
var import_tamagui = require("tamagui");
|
|
30
|
-
var import_colorRamps = require("../shared/colorRamps.native.js");
|
|
31
29
|
function CodeBlock(param) {
|
|
32
30
|
var { children, lang, theme: themeProp = "auto" } = param;
|
|
33
|
-
var _knobProps_borderRadiusNested;
|
|
34
|
-
var { knobProps } = (0, import_theme.useResolvedKnobs)();
|
|
35
|
-
var tokens = (0, import_components.useShikiTokens)(children, lang || "text", themeProp === "auto" ? void 0 : themeProp);
|
|
36
31
|
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_tamagui.View, {
|
|
37
|
-
...(_knobProps_borderRadiusNested = knobProps.borderRadiusNested) !== null && _knobProps_borderRadiusNested !== void 0 ? _knobProps_borderRadiusNested : { borderRadius: "4" },
|
|
38
|
-
backgroundColor: import_colorRamps.markdownCodeColors.background.base,
|
|
39
|
-
borderWidth: knobProps.borderRadius.borderWidth,
|
|
40
|
-
borderColor: (0, import_theme.edgeColorAt)(import_colorRamps.markdownCodeColors.border.base, knobProps.borderWidthStop),
|
|
41
|
-
...knobProps.panelPadding,
|
|
42
32
|
marginVertical: "2",
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_components.Code, {
|
|
48
|
-
backgroundColor: "transparent",
|
|
49
|
-
borderWidth: 0,
|
|
50
|
-
padding: 0,
|
|
51
|
-
color: import_colorRamps.markdownCodeColors.text.base,
|
|
52
|
-
fontSize: knobProps.sizeToken,
|
|
53
|
-
children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_components.HighlightedCode, {
|
|
54
|
-
tokens,
|
|
55
|
-
fontSize: knobProps.sizeToken,
|
|
56
|
-
children
|
|
57
|
-
})
|
|
58
|
-
})
|
|
33
|
+
children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_components.CodeBlock, {
|
|
34
|
+
language: lang || "text",
|
|
35
|
+
theme: themeProp === "auto" ? void 0 : themeProp,
|
|
36
|
+
children
|
|
59
37
|
})
|
|
60
38
|
});
|
|
61
39
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"CodeBlock.native.js","names":[],"sources":["jsx/components/CodeBlock.native.js"],"sourcesContent":["\"use strict\";\nvar __defProp = Object.defineProperty;\nvar __getOwnPropDesc = Object.getOwnPropertyDescriptor;\nvar __getOwnPropNames = Object.getOwnPropertyNames;\nvar __hasOwnProp = Object.prototype.hasOwnProperty;\nvar __export = (target, all) => {\n for (var name in all)\n __defProp(target, name, { get: all[name], enumerable: true });\n};\nvar __copyProps = (to, from, except, desc) => {\n if (from && typeof from === \"object\" || typeof from === \"function\") {\n for (let key of __getOwnPropNames(from))\n if (!__hasOwnProp.call(to, key) && key !== except)\n __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });\n }\n return to;\n};\nvar __toCommonJS = (mod) => __copyProps(__defProp({}, \"__esModule\", { value: true }), mod);\nvar CodeBlock_exports = {};\n__export(CodeBlock_exports, {\n CodeBlock: () => CodeBlock\n});\nmodule.exports = __toCommonJS(CodeBlock_exports);\nvar import_jsx_runtime = require(\"react/jsx-runtime\");\nvar import_components = require(\"@multiplatform.one/components\");\nvar
|
|
1
|
+
{"version":3,"file":"CodeBlock.native.js","names":[],"sources":["jsx/components/CodeBlock.native.js"],"sourcesContent":["\"use strict\";\nvar __defProp = Object.defineProperty;\nvar __getOwnPropDesc = Object.getOwnPropertyDescriptor;\nvar __getOwnPropNames = Object.getOwnPropertyNames;\nvar __hasOwnProp = Object.prototype.hasOwnProperty;\nvar __export = (target, all) => {\n for (var name in all)\n __defProp(target, name, { get: all[name], enumerable: true });\n};\nvar __copyProps = (to, from, except, desc) => {\n if (from && typeof from === \"object\" || typeof from === \"function\") {\n for (let key of __getOwnPropNames(from))\n if (!__hasOwnProp.call(to, key) && key !== except)\n __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });\n }\n return to;\n};\nvar __toCommonJS = (mod) => __copyProps(__defProp({}, \"__esModule\", { value: true }), mod);\nvar CodeBlock_exports = {};\n__export(CodeBlock_exports, {\n CodeBlock: () => CodeBlock\n});\nmodule.exports = __toCommonJS(CodeBlock_exports);\nvar import_jsx_runtime = require(\"react/jsx-runtime\");\nvar import_components = require(\"@multiplatform.one/components\");\nvar import_tamagui = require(\"tamagui\");\nfunction CodeBlock(param) {\n var { children, lang, theme: themeProp = \"auto\" } = param;\n return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_tamagui.View, {\n marginVertical: \"2\",\n children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_components.CodeBlock, {\n language: lang || \"text\",\n theme: themeProp === \"auto\" ? void 0 : themeProp,\n children\n })\n });\n}\n//# sourceMappingURL=CodeBlock.js.map\n"],"mappings":";;;AACA,IAAI,YAAY,OAAO;AACvB,IAAI,mBAAmB,OAAO;AAC9B,IAAI,oBAAoB,OAAO;AAC/B,IAAI,eAAe,OAAO,UAAU;AACpC,IAAI,YAAY,QAAQ,QAAQ;CAC9B,KAAK,IAAI,QAAQ,KACf,UAAU,QAAQ,MAAM;EAAE,KAAK,IAAI;EAAO,YAAY;CAAK,CAAC;AAChE;AACA,IAAI,eAAe,IAAI,MAAM,QAAQ,SAAS;CAC5C,IAAI,QAAQ,OAAO,SAAS,YAAY,OAAO,SAAS,YAAY;EAClE,KAAK,IAAI,OAAO,kBAAkB,IAAI,GACpC,IAAI,CAAC,aAAa,KAAK,IAAI,GAAG,KAAK,QAAQ,QACzC,UAAU,IAAI,KAAK;GAAE,WAAW,KAAK;GAAM,YAAY,EAAE,OAAO,iBAAiB,MAAM,GAAG,MAAM,KAAK;EAAW,CAAC;CACvH;CACA,OAAO;AACT;AACA,IAAI,gBAAgB,QAAQ,YAAY,UAAU,CAAC,GAAG,cAAc,EAAE,OAAO,KAAK,CAAC,GAAG,GAAG;AACzF,IAAI,oBAAoB,CAAC;AACzB,SAAS,mBAAmB,EAC1B,iBAAiB,UACnB,CAAC;AACD,OAAO,UAAU,aAAa,iBAAiB;AAC/C,IAAI,qBAAqB,QAAQ,mBAAmB;AACpD,IAAI,oBAAoB,QAAQ,+BAA+B;AAC/D,IAAI,iBAAiB,QAAQ,SAAS;AACtC,SAAS,UAAU,OAAO;CACxB,IAAI,EAAE,UAAU,MAAM,OAAO,YAAY,WAAW;CACpD,OAAuB,iBAAC,GAAG,mBAAmB,IAAG,CAAE,eAAe,MAAM;EACtE,gBAAgB;EAChB,UAA0B,iBAAC,GAAG,mBAAmB,IAAG,CAAE,kBAAkB,WAAW;GACjF,UAAU,QAAQ;GAClB,OAAO,cAAc,SAAS,KAAK,IAAI;GACvC;EACF,CAAC;CACH,CAAC;AACH"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@multiplatform.one/markdown",
|
|
3
|
-
"version": "11.
|
|
3
|
+
"version": "11.13.1",
|
|
4
4
|
"description": "Cross-platform markdown renderer with Shiki syntax highlighting for React and React Native",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"cross-platform",
|
|
@@ -68,11 +68,11 @@
|
|
|
68
68
|
"dependencies": {
|
|
69
69
|
"@mdx-js/mdx": "^3.1.1",
|
|
70
70
|
"@mdx-js/react": "^3.1.1",
|
|
71
|
-
"@multiplatform.one/components": "^11.
|
|
72
|
-
"@multiplatform.one/platform": "^11.
|
|
73
|
-
"@multiplatform.one/router": "^11.
|
|
74
|
-
"@multiplatform.one/table-primitives": "^11.
|
|
75
|
-
"@multiplatform.one/theme": "^11.
|
|
71
|
+
"@multiplatform.one/components": "^11.13.1",
|
|
72
|
+
"@multiplatform.one/platform": "^11.13.1",
|
|
73
|
+
"@multiplatform.one/router": "^11.13.1",
|
|
74
|
+
"@multiplatform.one/table-primitives": "^11.13.1",
|
|
75
|
+
"@multiplatform.one/theme": "^11.13.1",
|
|
76
76
|
"@tamagui/linear-gradient": "3.2.3",
|
|
77
77
|
"@tamagui/react-native-svg": "3.2.3",
|
|
78
78
|
"@tamagui/web": "3.2.3",
|
|
@@ -90,9 +90,9 @@
|
|
|
90
90
|
"unist-util-visit": "^5.1.0"
|
|
91
91
|
},
|
|
92
92
|
"devDependencies": {
|
|
93
|
-
"@multiplatform.one/config": "11.
|
|
94
|
-
"@multiplatform.one/storybook": "11.
|
|
95
|
-
"@multiplatform.one/test-utils": "11.
|
|
93
|
+
"@multiplatform.one/config": "11.13.1",
|
|
94
|
+
"@multiplatform.one/storybook": "11.13.1",
|
|
95
|
+
"@multiplatform.one/test-utils": "11.13.1",
|
|
96
96
|
"@tamagui/build": "3.2.3",
|
|
97
97
|
"@testing-library/jest-dom": "^7.0.1",
|
|
98
98
|
"@testing-library/react": "^16.3.3",
|
|
@@ -112,6 +112,6 @@
|
|
|
112
112
|
"scripts": {
|
|
113
113
|
"build": "rm -rf dist types 2>/dev/null && tamagui-build --ts-project ./tsconfig.build.json --exclude '\\.spec\\.|\\.test\\.|\\.stories\\.'",
|
|
114
114
|
"test": "vitest run --coverage --coverage.provider=v8 --coverage.reporter=text --coverage.reporter=lcov --coverage.reporter=html",
|
|
115
|
-
"typecheck": "tsgo --noEmit"
|
|
115
|
+
"typecheck": "tsgo --noEmit -p tsconfig.typecheck.json"
|
|
116
116
|
}
|
|
117
117
|
}
|
|
@@ -1,24 +1,8 @@
|
|
|
1
|
-
import {
|
|
2
|
-
Button,
|
|
3
|
-
Code,
|
|
4
|
-
ErrorBoundary,
|
|
5
|
-
HighlightedCode,
|
|
6
|
-
Pre,
|
|
7
|
-
useClipboard,
|
|
8
|
-
useShikiTokens,
|
|
9
|
-
ScrollView,
|
|
10
|
-
} from "@multiplatform.one/components";
|
|
11
|
-
import { useResolvedKnobs } from "@multiplatform.one/theme";
|
|
12
|
-
import { LinearGradient } from "@tamagui/linear-gradient";
|
|
1
|
+
import { Button, CodeBlock } from "@multiplatform.one/components";
|
|
13
2
|
import rangeParser from "parse-numeric-range";
|
|
14
3
|
import type { ReactNode } from "react";
|
|
15
4
|
import { useContext, useMemo, useState } from "react";
|
|
16
|
-
import {
|
|
17
|
-
import { CheckCircleIcon, ClipboardIcon } from "../icons";
|
|
18
|
-
import { useCutoffGradientColors } from "../shared/cutoffGradient";
|
|
19
|
-
import { tokenPx } from "../shared/tokenPx";
|
|
20
|
-
|
|
21
|
-
const zIndex = { localTop: 100 };
|
|
5
|
+
import { XStack, YStack } from "tamagui";
|
|
22
6
|
import type { MDXCodeBlockContextValue } from "./mdxCodeBlockContext";
|
|
23
7
|
import { MDXCodeBlockContext } from "./mdxCodeBlockContext";
|
|
24
8
|
|
|
@@ -49,6 +33,12 @@ function extractLanguage(className?: string): string {
|
|
|
49
33
|
return match?.[1] || "text";
|
|
50
34
|
}
|
|
51
35
|
|
|
36
|
+
/**
|
|
37
|
+
* A fenced block in MDX. It renders the components `CodeBlock` (LC-323, LC-16):
|
|
38
|
+
* the code surface, the copy action, line numbers past ten lines and the
|
|
39
|
+
* Show more clamp are the block's own. What is left here is the MDX contract
|
|
40
|
+
* (the `language-*` class, the `line` range) and the collapse toggle.
|
|
41
|
+
*/
|
|
52
42
|
export function MDXCodeBlock(props: MDXCodeBlockProps) {
|
|
53
43
|
const {
|
|
54
44
|
children,
|
|
@@ -57,131 +47,47 @@ export function MDXCodeBlock(props: MDXCodeBlockProps) {
|
|
|
57
47
|
id,
|
|
58
48
|
isCollapsible,
|
|
59
49
|
isHighlightingLines,
|
|
60
|
-
showLineNumbers
|
|
50
|
+
showLineNumbers,
|
|
61
51
|
size,
|
|
62
52
|
line,
|
|
63
|
-
...codeProps
|
|
64
53
|
} = { ...useContext(MDXCodeBlockContext), ...props } as MDXCodeBlockProps & {
|
|
65
54
|
line?: string;
|
|
66
55
|
};
|
|
67
56
|
|
|
68
|
-
const { knobProps } = useResolvedKnobs();
|
|
69
|
-
const gradientColors = useCutoffGradientColors();
|
|
70
|
-
const cutoffMaxHeight = tokenPx(knobProps.sizeToken, 24) * 16;
|
|
71
|
-
const gradientHeight = tokenPx(knobProps.sizeToken, 24) * 8;
|
|
72
|
-
const iconPx = tokenPx(knobProps.label.fontSize as string, 14, "fontSize");
|
|
73
57
|
const codeText = useMemo(() => extractCodeText(children).replace(/\n$/, ""), [children]);
|
|
74
58
|
const language = useMemo(() => extractLanguage(className), [className]);
|
|
75
|
-
const tokens = useShikiTokens(codeText, language);
|
|
76
|
-
|
|
77
|
-
const lines = tokens?.length ?? codeText.split("\n").length;
|
|
78
|
-
const showLineNumbers = propShowLineNumbers ?? lines > 10;
|
|
79
59
|
const [isCollapsed, setIsCollapsed] = useState(isCollapsible);
|
|
80
|
-
const isLong = lines > 22;
|
|
81
|
-
const [isCutoff, setIsCutoff] = useState(isLong && !isCollapsible);
|
|
82
|
-
const { hasCopied, onCopy } = useClipboard(codeText);
|
|
83
|
-
|
|
84
60
|
const highlightLines = useMemo(() => {
|
|
85
61
|
if (!isHighlightingLines || !line) return [];
|
|
86
62
|
return rangeParser(line);
|
|
87
63
|
}, [isHighlightingLines, line]);
|
|
64
|
+
const shown = !isCollapsed || !isCollapsible;
|
|
88
65
|
|
|
89
66
|
return (
|
|
90
|
-
<YStack
|
|
91
|
-
|
|
92
|
-
<
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
67
|
+
<YStack marginBottom="4" gap="2" id={id} data-mdx-code-block="">
|
|
68
|
+
{isCollapsible ? (
|
|
69
|
+
<XStack>
|
|
70
|
+
<Button
|
|
71
|
+
aria-label="Show or hide code"
|
|
72
|
+
aria-expanded={shown}
|
|
73
|
+
size="2"
|
|
74
|
+
onPress={() => setIsCollapsed((x) => !x)}
|
|
75
|
+
>
|
|
76
|
+
{shown ? "Hide code" : "Show code"}
|
|
77
|
+
</Button>
|
|
78
|
+
</XStack>
|
|
79
|
+
) : null}
|
|
80
|
+
{shown ? (
|
|
81
|
+
<CodeBlock
|
|
82
|
+
language={language}
|
|
83
|
+
copyable={copyable}
|
|
84
|
+
showLineNumbers={showLineNumbers}
|
|
85
|
+
highlightLines={highlightLines}
|
|
86
|
+
size={size as string | undefined}
|
|
99
87
|
>
|
|
100
|
-
{
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
position="absolute"
|
|
104
|
-
bottom={0}
|
|
105
|
-
left={0}
|
|
106
|
-
right={0}
|
|
107
|
-
height={gradientHeight}
|
|
108
|
-
colors={gradientColors}
|
|
109
|
-
pointerEvents="none"
|
|
110
|
-
zIndex={zIndex.localTop}
|
|
111
|
-
/>
|
|
112
|
-
<XStack
|
|
113
|
-
position="absolute"
|
|
114
|
-
bottom="4"
|
|
115
|
-
left={0}
|
|
116
|
-
right={0}
|
|
117
|
-
justifyContent="center"
|
|
118
|
-
zIndex={zIndex.localTop + 1}
|
|
119
|
-
>
|
|
120
|
-
<Button onPress={() => setIsCutoff(false)}>Show more</Button>
|
|
121
|
-
</XStack>
|
|
122
|
-
</>
|
|
123
|
-
)}
|
|
124
|
-
{(!isCollapsed || !isCollapsible) && (
|
|
125
|
-
<Pre
|
|
126
|
-
padding={0}
|
|
127
|
-
marginBottom={0}
|
|
128
|
-
id={id}
|
|
129
|
-
data-line-numbers={showLineNumbers ? "true" : "false"}
|
|
130
|
-
>
|
|
131
|
-
<ScrollView
|
|
132
|
-
style={{ width: "100%" }}
|
|
133
|
-
contentContainerStyle={{ minWidth: "100%" }}
|
|
134
|
-
horizontal
|
|
135
|
-
showsHorizontalScrollIndicator={false}
|
|
136
|
-
>
|
|
137
|
-
<Code
|
|
138
|
-
backgroundColor="transparent"
|
|
139
|
-
flex={1}
|
|
140
|
-
borderWidth={0}
|
|
141
|
-
// SP-PAD + T-VALUE via sizeToken (spec MdxCodeBlock; LC-18):
|
|
142
|
-
// inset is the panelPadding role, content type rides the size
|
|
143
|
-
// token — components-CodeBlock parity. Explicit props eject.
|
|
144
|
-
{...knobProps.panelPadding}
|
|
145
|
-
size={(size ?? knobProps.sizeToken) as any}
|
|
146
|
-
{...codeProps}
|
|
147
|
-
>
|
|
148
|
-
<HighlightedCode
|
|
149
|
-
tokens={tokens}
|
|
150
|
-
highlightLines={highlightLines}
|
|
151
|
-
showLineNumbers={showLineNumbers}
|
|
152
|
-
>
|
|
153
|
-
{codeText}
|
|
154
|
-
</HighlightedCode>
|
|
155
|
-
</Code>
|
|
156
|
-
</ScrollView>
|
|
157
|
-
</Pre>
|
|
158
|
-
)}
|
|
159
|
-
<XStack position="absolute" top="3" insetInlineEnd="3" {...knobProps.gap}>
|
|
160
|
-
{(!isCollapsed || !isCollapsible) && (copyable ?? true) && (
|
|
161
|
-
<TooltipSimple label={hasCopied ? "Copied" : "Copy to clipboard"}>
|
|
162
|
-
<Button
|
|
163
|
-
aria-label="Copy code to clipboard"
|
|
164
|
-
size="2"
|
|
165
|
-
display="max-xs:none"
|
|
166
|
-
icon={
|
|
167
|
-
hasCopied ? <CheckCircleIcon size={iconPx} /> : <ClipboardIcon size={iconPx} />
|
|
168
|
-
}
|
|
169
|
-
onPress={onCopy}
|
|
170
|
-
/>
|
|
171
|
-
</TooltipSimple>
|
|
172
|
-
)}
|
|
173
|
-
{isCollapsible && (
|
|
174
|
-
<Button
|
|
175
|
-
aria-label="Show or hide code"
|
|
176
|
-
size="2"
|
|
177
|
-
onPress={() => setIsCollapsed((x) => !x)}
|
|
178
|
-
>
|
|
179
|
-
{isCollapsed ? "Show code" : "Hide code"}
|
|
180
|
-
</Button>
|
|
181
|
-
)}
|
|
182
|
-
</XStack>
|
|
183
|
-
</YStack>
|
|
184
|
-
</ErrorBoundary>
|
|
88
|
+
{codeText}
|
|
89
|
+
</CodeBlock>
|
|
90
|
+
) : null}
|
|
185
91
|
</YStack>
|
|
186
92
|
);
|
|
187
93
|
}
|
|
@@ -1,66 +1,67 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* LC-323: an MDX fence renders the components CodeBlock and keeps only its
|
|
3
|
+
* collapse toggle. The copy action, the line numbers and the surface are the
|
|
4
|
+
* block's, so the spec reads them where CodeBlock draws them.
|
|
5
|
+
*/
|
|
1
6
|
import { renderWithProviders } from "@multiplatform.one/test-utils";
|
|
7
|
+
import { cleanup, fireEvent } from "@testing-library/react";
|
|
2
8
|
import React from "react";
|
|
3
|
-
import { describe, expect, it } from "vitest";
|
|
9
|
+
import { afterEach, describe, expect, it } from "vitest";
|
|
4
10
|
import { MDXCodeBlock } from "./index";
|
|
5
11
|
|
|
6
|
-
|
|
7
|
-
it("should render code content", () => {
|
|
8
|
-
const codeContent = <span className="token keyword">const</span>;
|
|
9
|
-
|
|
10
|
-
const { container } = renderWithProviders(<MDXCodeBlock>{codeContent}</MDXCodeBlock>);
|
|
12
|
+
afterEach(cleanup);
|
|
11
13
|
|
|
14
|
+
describe("MDXCodeBlock", () => {
|
|
15
|
+
it("renders the code on the components CodeBlock's code surface", () => {
|
|
16
|
+
const { container } = renderWithProviders(
|
|
17
|
+
<MDXCodeBlock className="language-ts">
|
|
18
|
+
<span className="token keyword">const</span>
|
|
19
|
+
</MDXCodeBlock>,
|
|
20
|
+
);
|
|
12
21
|
expect(container.textContent).toContain("const");
|
|
22
|
+
const surface = container.querySelector("[data-mono-surface]");
|
|
23
|
+
expect(surface?.getAttribute("data-mono-surface")).toBe("code");
|
|
24
|
+
expect(surface?.getAttribute("data-mono-form")).toBe("card");
|
|
25
|
+
expect(container.querySelector("[data-mono-scroller]")).not.toBeNull();
|
|
13
26
|
});
|
|
14
27
|
|
|
15
|
-
it("
|
|
16
|
-
const
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
28
|
+
it("collapses behind Show code and opens on Hide code", () => {
|
|
29
|
+
const { getByText, queryByText, container } = renderWithProviders(
|
|
30
|
+
<MDXCodeBlock isCollapsible>
|
|
31
|
+
<span>Test code</span>
|
|
32
|
+
</MDXCodeBlock>,
|
|
20
33
|
);
|
|
21
|
-
|
|
22
|
-
const toggleButton = getByText("Show code");
|
|
23
|
-
expect(toggleButton).toBeDefined();
|
|
34
|
+
const toggle = getByText("Show code");
|
|
24
35
|
expect(queryByText("Test code")).toBeNull();
|
|
36
|
+
expect(container.querySelector("[data-mono-surface]")).toBeNull();
|
|
37
|
+
fireEvent.click(toggle);
|
|
38
|
+
expect(container.textContent).toContain("Test code");
|
|
39
|
+
expect(getByText("Hide code")).toBeDefined();
|
|
25
40
|
});
|
|
26
41
|
|
|
27
|
-
it("
|
|
28
|
-
const
|
|
29
|
-
|
|
30
|
-
const
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
expect(pre?.getAttribute("data-line-numbers")).toBe("
|
|
42
|
+
it("numbers a block past ten lines and not a short one", () => {
|
|
43
|
+
const lines = (count: number) =>
|
|
44
|
+
Array.from({ length: count }, (_, i) => <span key={i}>{`Line ${i + 1}\n`}</span>);
|
|
45
|
+
const long = renderWithProviders(<MDXCodeBlock>{lines(15)}</MDXCodeBlock>);
|
|
46
|
+
expect(long.container.querySelector("pre")?.getAttribute("data-line-numbers")).toBe("true");
|
|
47
|
+
cleanup();
|
|
48
|
+
const short = renderWithProviders(<MDXCodeBlock>{lines(5)}</MDXCodeBlock>);
|
|
49
|
+
expect(short.container.querySelector("pre")?.getAttribute("data-line-numbers")).toBe("false");
|
|
35
50
|
});
|
|
36
51
|
|
|
37
|
-
it("
|
|
38
|
-
const
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
const pre = container.querySelector("pre");
|
|
43
|
-
expect(pre).toBeDefined();
|
|
44
|
-
expect(pre?.getAttribute("data-line-numbers")).toBe("false");
|
|
45
|
-
});
|
|
46
|
-
|
|
47
|
-
it("should handle copy functionality when not disabled", () => {
|
|
48
|
-
const codeContent = <span>Copy this code</span>;
|
|
49
|
-
|
|
50
|
-
const { container } = renderWithProviders(<MDXCodeBlock>{codeContent}</MDXCodeBlock>);
|
|
51
|
-
|
|
52
|
-
const copyButton = container.querySelector('[aria-label="Copy code to clipboard"]');
|
|
53
|
-
expect(copyButton).toBeDefined();
|
|
54
|
-
});
|
|
55
|
-
|
|
56
|
-
it("should not show copy button when copyable is false", () => {
|
|
57
|
-
const codeContent = <span>Cannot copy this</span>;
|
|
58
|
-
|
|
59
|
-
const { container } = renderWithProviders(
|
|
60
|
-
<MDXCodeBlock copyable={false}>{codeContent}</MDXCodeBlock>,
|
|
52
|
+
it("carries the CodeBlock copy action unless copyable is false", () => {
|
|
53
|
+
const copyable = renderWithProviders(
|
|
54
|
+
<MDXCodeBlock>
|
|
55
|
+
<span>Copy this code</span>
|
|
56
|
+
</MDXCodeBlock>,
|
|
61
57
|
);
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
58
|
+
expect(copyable.container.querySelector('[aria-label="Copy to clipboard"]')).not.toBeNull();
|
|
59
|
+
cleanup();
|
|
60
|
+
const plain = renderWithProviders(
|
|
61
|
+
<MDXCodeBlock copyable={false}>
|
|
62
|
+
<span>Cannot copy this</span>
|
|
63
|
+
</MDXCodeBlock>,
|
|
64
|
+
);
|
|
65
|
+
expect(plain.container.querySelector('[aria-label="Copy to clipboard"]')).toBeNull();
|
|
65
66
|
});
|
|
66
67
|
});
|
|
@@ -1,18 +1,14 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Plain-markdown code block.
|
|
3
3
|
*
|
|
4
|
-
* LC-16 wrapper:
|
|
5
|
-
* `@multiplatform.one/components`
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
* the spacing roles — SP-PAD, spec CodeBlock frame) and the shared markdown
|
|
9
|
-
* code color ramp.
|
|
4
|
+
* LC-16 wrapper: a fence in prose is source code, so it renders the canonical
|
|
5
|
+
* `@multiplatform.one/components` `CodeBlock` (LC-323), the one surface code
|
|
6
|
+
* is drawn on. This file only maps the fence's language and theme onto it and
|
|
7
|
+
* keeps the block's margin in the prose flow.
|
|
10
8
|
*/
|
|
11
9
|
|
|
12
|
-
import {
|
|
13
|
-
import { edgeColorAt, useResolvedKnobs } from "@multiplatform.one/theme";
|
|
10
|
+
import { CodeBlock as HouseCodeBlock } from "@multiplatform.one/components";
|
|
14
11
|
import { View } from "tamagui";
|
|
15
|
-
import { markdownCodeColors } from "../shared/colorRamps";
|
|
16
12
|
import type { CodeBlockProps } from "../types";
|
|
17
13
|
|
|
18
14
|
interface CodeBlockInternalProps extends CodeBlockProps {
|
|
@@ -20,44 +16,14 @@ interface CodeBlockInternalProps extends CodeBlockProps {
|
|
|
20
16
|
}
|
|
21
17
|
|
|
22
18
|
export function CodeBlock({ children, lang, theme: themeProp = "auto" }: CodeBlockInternalProps) {
|
|
23
|
-
const { knobProps } = useResolvedKnobs();
|
|
24
|
-
// "auto" defers to useShikiTokens' default, the house syntax theme, whose
|
|
25
|
-
// token inks follow the scheme (SB-M-1216, MPO-474).
|
|
26
|
-
const tokens = useShikiTokens(
|
|
27
|
-
children,
|
|
28
|
-
lang || "text",
|
|
29
|
-
themeProp === "auto" ? undefined : themeProp,
|
|
30
|
-
);
|
|
31
|
-
|
|
32
19
|
return (
|
|
33
|
-
<View
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
borderColor={edgeColorAt(markdownCodeColors.border.base, knobProps.borderWidthStop)}
|
|
41
|
-
// SP-PAD (spec CodeBlock frame; LC-18 / UX-P07): the block inset rides
|
|
42
|
-
// the complete panelPadding role fragment —
|
|
43
|
-
// components-CodeBlock parity.
|
|
44
|
-
{...knobProps.panelPadding}
|
|
45
|
-
marginVertical="2"
|
|
46
|
-
overflow="hidden"
|
|
47
|
-
>
|
|
48
|
-
<ScrollView horizontal showsHorizontalScrollIndicator={false}>
|
|
49
|
-
<Code
|
|
50
|
-
backgroundColor="transparent"
|
|
51
|
-
borderWidth={0}
|
|
52
|
-
padding={0}
|
|
53
|
-
color={markdownCodeColors.text.base}
|
|
54
|
-
fontSize={knobProps.sizeToken}
|
|
55
|
-
>
|
|
56
|
-
<HighlightedCode tokens={tokens} fontSize={knobProps.sizeToken}>
|
|
57
|
-
{children}
|
|
58
|
-
</HighlightedCode>
|
|
59
|
-
</Code>
|
|
60
|
-
</ScrollView>
|
|
20
|
+
<View marginVertical="2">
|
|
21
|
+
<HouseCodeBlock
|
|
22
|
+
language={lang || "text"}
|
|
23
|
+
theme={themeProp === "auto" ? undefined : themeProp}
|
|
24
|
+
>
|
|
25
|
+
{children}
|
|
26
|
+
</HouseCodeBlock>
|
|
61
27
|
</View>
|
|
62
28
|
);
|
|
63
29
|
}
|
|
@@ -9,7 +9,8 @@ const meta: Meta = {
|
|
|
9
9
|
status: { type: "beta" },
|
|
10
10
|
docs: {
|
|
11
11
|
description: {
|
|
12
|
-
component:
|
|
12
|
+
component:
|
|
13
|
+
"Markdown CodeBlock and MDXCodeBlock, both rendering the components CodeBlock on LC-323's code surface.",
|
|
13
14
|
},
|
|
14
15
|
},
|
|
15
16
|
},
|
|
@@ -3,5 +3,11 @@ export interface MDXCodeBlockProps extends MDXCodeBlockContextValue {
|
|
|
3
3
|
className?: string;
|
|
4
4
|
id?: string;
|
|
5
5
|
}
|
|
6
|
+
/**
|
|
7
|
+
* A fenced block in MDX. It renders the components `CodeBlock` (LC-323, LC-16):
|
|
8
|
+
* the code surface, the copy action, line numbers past ten lines and the
|
|
9
|
+
* Show more clamp are the block's own. What is left here is the MDX contract
|
|
10
|
+
* (the `language-*` class, the `line` range) and the collapse toggle.
|
|
11
|
+
*/
|
|
6
12
|
export declare function MDXCodeBlock(props: MDXCodeBlockProps): import("react").JSX.Element;
|
|
7
13
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/MdxCodeBlock/index.tsx"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/MdxCodeBlock/index.tsx"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAAE,wBAAwB,EAAE,MAAM,uBAAuB,CAAC;AAGtE,MAAM,WAAW,iBAAkB,SAAQ,wBAAwB;IACjE,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,EAAE,CAAC,EAAE,MAAM,CAAC;CACb;AAwBD;;;;;GAKG;AACH,wBAAgB,YAAY,CAAC,KAAK,EAAE,iBAAiB,+BAmDpD"}
|
|
@@ -1,12 +1,10 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Plain-markdown code block.
|
|
3
3
|
*
|
|
4
|
-
* LC-16 wrapper:
|
|
5
|
-
* `@multiplatform.one/components`
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
* the spacing roles — SP-PAD, spec CodeBlock frame) and the shared markdown
|
|
9
|
-
* code color ramp.
|
|
4
|
+
* LC-16 wrapper: a fence in prose is source code, so it renders the canonical
|
|
5
|
+
* `@multiplatform.one/components` `CodeBlock` (LC-323), the one surface code
|
|
6
|
+
* is drawn on. This file only maps the fence's language and theme onto it and
|
|
7
|
+
* keeps the block's margin in the prose flow.
|
|
10
8
|
*/
|
|
11
9
|
import type { CodeBlockProps } from "../types";
|
|
12
10
|
interface CodeBlockInternalProps extends CodeBlockProps {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"CodeBlock.d.ts","sourceRoot":"","sources":["../../src/components/CodeBlock.tsx"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"CodeBlock.d.ts","sourceRoot":"","sources":["../../src/components/CodeBlock.tsx"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAIH,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,UAAU,CAAC;AAE/C,UAAU,sBAAuB,SAAQ,cAAc;IACrD,KAAK,CAAC,EAAE,MAAM,GAAG,MAAM,CAAC;CACzB;AAED,wBAAgB,SAAS,CAAC,EAAE,QAAQ,EAAE,IAAI,EAAE,KAAK,EAAE,SAAkB,EAAE,EAAE,sBAAsB,+BAW9F"}
|