@brillout/docpress 0.16.35 → 0.16.37
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/code-blocks/components/ChoiceGroup.css +2 -1
- package/code-blocks/components/ChoiceGroup.tsx +1 -1
- package/code-blocks/remarkChoiceGroup.ts +9 -6
- package/code-blocks/remarkDetype.ts +5 -12
- package/code-blocks/remarkPkgManager.ts +0 -9
- package/code-blocks/utils/generateChoiceGroupCode.ts +6 -1
- package/dist/code-blocks/remarkChoiceGroup.d.ts +1 -0
- package/dist/code-blocks/remarkChoiceGroup.js +8 -7
- package/dist/code-blocks/remarkDetype.js +5 -11
- package/dist/code-blocks/remarkPkgManager.js +0 -8
- package/dist/code-blocks/utils/generateChoiceGroupCode.js +3 -1
- package/package.json +1 -1
|
@@ -10,7 +10,6 @@
|
|
|
10
10
|
display: flex;
|
|
11
11
|
flex-direction: row-reverse;
|
|
12
12
|
gap: 2px;
|
|
13
|
-
z-index: 3;
|
|
14
13
|
top: var(--top-position);
|
|
15
14
|
right: 42px;
|
|
16
15
|
}
|
|
@@ -18,6 +17,7 @@
|
|
|
18
17
|
.choice-select {
|
|
19
18
|
background: #eee;
|
|
20
19
|
font-size: 13.3333px;
|
|
20
|
+
z-index: 3;
|
|
21
21
|
|
|
22
22
|
-webkit-user-select: none; /* Safari */
|
|
23
23
|
-moz-user-select: none; /* Firefox */
|
|
@@ -58,6 +58,7 @@
|
|
|
58
58
|
.choice-select[aria-expanded='true'] {
|
|
59
59
|
overflow: visible;
|
|
60
60
|
border-width: 0;
|
|
61
|
+
z-index: 5;
|
|
61
62
|
|
|
62
63
|
.choice-select__list {
|
|
63
64
|
border-style: solid;
|
|
@@ -109,7 +109,7 @@ function ChoiceGroup({ children, choiceGroup, parentChoiceGroup }: ChoiceGroupPr
|
|
|
109
109
|
))}
|
|
110
110
|
</select>
|
|
111
111
|
{children}
|
|
112
|
-
{lvl === 0 && (
|
|
112
|
+
{lvl === 0 && !choiceGroup.hidden && (
|
|
113
113
|
<div className={`choice-group__selects`}>
|
|
114
114
|
{choiceGroupAll
|
|
115
115
|
.slice()
|
|
@@ -16,7 +16,10 @@ function remarkChoiceGroup() {
|
|
|
16
16
|
const { choice } = meta.props
|
|
17
17
|
node.meta = meta.rest
|
|
18
18
|
|
|
19
|
-
if (choice)
|
|
19
|
+
if (choice) {
|
|
20
|
+
const filter = ['jsx', 'tsx', 'vue'].includes(node.lang ?? '') ? 'code-component' : `code-${node.lang}`
|
|
21
|
+
node.data ??= { customDataChoice: choice, customDataFilter: filter }
|
|
22
|
+
}
|
|
20
23
|
}
|
|
21
24
|
if (node.type === 'containerDirective' && node.name === 'Choice') {
|
|
22
25
|
if (!node.attributes) return
|
|
@@ -28,9 +31,8 @@ function remarkChoiceGroup() {
|
|
|
28
31
|
}
|
|
29
32
|
})
|
|
30
33
|
|
|
31
|
-
const replaced = new WeakSet()
|
|
32
34
|
visit(tree, (node) => {
|
|
33
|
-
if (!('children' in node) ||
|
|
35
|
+
if (!('children' in node) || node.data?.customDataIsVisited) return 'skip'
|
|
34
36
|
|
|
35
37
|
let start = -1
|
|
36
38
|
let end = 0
|
|
@@ -43,9 +45,10 @@ function remarkChoiceGroup() {
|
|
|
43
45
|
|
|
44
46
|
for (const choiceNodes of choiceNodesFiltered) {
|
|
45
47
|
const replacement = generateChoiceGroupCode(choiceNodes, node)
|
|
48
|
+
replacement.data ??= {}
|
|
49
|
+
replacement.data.customDataIsVisited = true
|
|
46
50
|
replacements.push(replacement)
|
|
47
51
|
}
|
|
48
|
-
replaced.add(replacements)
|
|
49
52
|
|
|
50
53
|
node.children.splice(start, end - start, ...replacements)
|
|
51
54
|
|
|
@@ -56,7 +59,7 @@ function remarkChoiceGroup() {
|
|
|
56
59
|
for (; end < node.children.length; end++) {
|
|
57
60
|
const child = node.children[end]!
|
|
58
61
|
|
|
59
|
-
if (!['code', '
|
|
62
|
+
if (!['code', 'containerDirective'].includes(child.type)) {
|
|
60
63
|
process()
|
|
61
64
|
continue
|
|
62
65
|
}
|
|
@@ -85,7 +88,6 @@ function filterChoices(nodes: ChoiceNode['children']) {
|
|
|
85
88
|
.map((node) => {
|
|
86
89
|
const choice = node.data!.customDataChoice!
|
|
87
90
|
const nodes = nodesByChoice.get(choice) ?? []
|
|
88
|
-
node.data!.customDataChoice = undefined
|
|
89
91
|
nodes.push(node)
|
|
90
92
|
nodesByChoice.set(choice, nodes)
|
|
91
93
|
})
|
|
@@ -99,6 +101,7 @@ function filterChoices(nodes: ChoiceNode['children']) {
|
|
|
99
101
|
|
|
100
102
|
declare module 'mdast' {
|
|
101
103
|
export interface Data {
|
|
104
|
+
customDataIsVisited?: boolean
|
|
102
105
|
customDataChoice?: string
|
|
103
106
|
customDataFilter?: string
|
|
104
107
|
customDataParentChoiceGroup?: {
|
|
@@ -33,6 +33,9 @@ function remarkDetype() {
|
|
|
33
33
|
// Skip if 'ts-only' meta is present
|
|
34
34
|
if (node.meta?.includes('ts-only')) return
|
|
35
35
|
|
|
36
|
+
// Skip if 'customDataChoice' is 'TypeScript'
|
|
37
|
+
if (node.data?.customDataChoice === 'TypeScript') return
|
|
38
|
+
|
|
36
39
|
// Collect this node for post-processing
|
|
37
40
|
code_nodes.push({ codeBlock: node, index, parent })
|
|
38
41
|
})
|
|
@@ -54,9 +57,6 @@ function transformYaml(node: CodeNode) {
|
|
|
54
57
|
// Skip wrapping if the YAML code block hasn't changed
|
|
55
58
|
if (codeBlockContentJs === codeBlock.value) return
|
|
56
59
|
|
|
57
|
-
const meta = parseMetaString(codeBlock.meta, ['choice'])
|
|
58
|
-
const { choice } = meta.props
|
|
59
|
-
codeBlock.meta = meta.rest
|
|
60
60
|
const { position, ...rest } = codeBlock
|
|
61
61
|
|
|
62
62
|
// Create a new code node for the JS version based on the modified YAML
|
|
@@ -71,21 +71,16 @@ function transformYaml(node: CodeNode) {
|
|
|
71
71
|
{ choiceValue: 'TypeScript', children: [codeBlock] },
|
|
72
72
|
]
|
|
73
73
|
const replacement = generateChoiceGroupCode(choiceNodes, parent, true)
|
|
74
|
-
replacement.data ??= { customDataChoice: choice, customDataFilter: 'codeLang' }
|
|
75
74
|
|
|
76
75
|
parent.children.splice(index, 1, replacement)
|
|
77
76
|
}
|
|
78
77
|
|
|
79
78
|
async function transformTsToJs(node: CodeNode, file: VFile) {
|
|
80
79
|
const { codeBlock, index, parent } = node
|
|
81
|
-
const meta = parseMetaString(codeBlock.meta, ['max-width'
|
|
80
|
+
const meta = parseMetaString(codeBlock.meta, ['max-width'])
|
|
82
81
|
const maxWidth = Number(meta.props['max-width'])
|
|
83
|
-
const { choice } = meta.props
|
|
84
82
|
codeBlock.meta = meta.rest
|
|
85
83
|
|
|
86
|
-
codeBlock.data ??= { customDataChoice: choice, customDataFilter: 'codeLang' }
|
|
87
|
-
if (choice === 'TypeScript') return
|
|
88
|
-
|
|
89
84
|
let codeBlockReplacedJs = replaceFileNameSuffixes(codeBlock.value)
|
|
90
85
|
let codeBlockContentJs = ''
|
|
91
86
|
|
|
@@ -126,7 +121,7 @@ async function transformTsToJs(node: CodeNode, file: VFile) {
|
|
|
126
121
|
// No wrapping needed if JS and TS code are still identical
|
|
127
122
|
if (codeBlockContentJs === codeBlock.value) return
|
|
128
123
|
|
|
129
|
-
const { position, lang,
|
|
124
|
+
const { position, lang, ...rest } = codeBlock
|
|
130
125
|
|
|
131
126
|
const tsCode: Code = { ...rest, lang }
|
|
132
127
|
const jsCode: Code = {
|
|
@@ -145,8 +140,6 @@ async function transformTsToJs(node: CodeNode, file: VFile) {
|
|
|
145
140
|
const hide = codeBlockReplacedJs === codeBlockContentJs
|
|
146
141
|
const replacement = generateChoiceGroupCode(choiceNodes, parent, hide)
|
|
147
142
|
|
|
148
|
-
replacement.data ??= { ...data }
|
|
149
|
-
|
|
150
143
|
parent.children.splice(index, 1, replacement)
|
|
151
144
|
}
|
|
152
145
|
|
|
@@ -4,7 +4,6 @@ import type { Code, Root } from 'mdast'
|
|
|
4
4
|
import type { VFile } from '@mdx-js/mdx/internal-create-format-aware-processors'
|
|
5
5
|
import { visit } from 'unist-util-visit'
|
|
6
6
|
import convert_ from 'npm-to-yarn'
|
|
7
|
-
import { parseMetaString } from './rehypeMetaToProps.js'
|
|
8
7
|
import { generateChoiceGroupCode } from './utils/generateChoiceGroupCode.js'
|
|
9
8
|
import { assertUsage } from '../utils/assert.js'
|
|
10
9
|
import pc from '@brillout/picocolors'
|
|
@@ -25,15 +24,8 @@ function remarkPkgManager() {
|
|
|
25
24
|
}. Replace it with the equivalent 'npm' command for the package manager toggle to work.`,
|
|
26
25
|
)
|
|
27
26
|
if (!node.value.includes('npm ') && !node.value.includes('npx ')) return
|
|
28
|
-
let choice: string | undefined = undefined
|
|
29
27
|
const nodes = new Map<string, Code>()
|
|
30
28
|
|
|
31
|
-
if (node.meta) {
|
|
32
|
-
const meta = parseMetaString(node.meta, ['choice'])
|
|
33
|
-
choice = meta.props['choice']
|
|
34
|
-
node.meta = meta.rest
|
|
35
|
-
}
|
|
36
|
-
|
|
37
29
|
node.value = node.value.replaceAll('npm i ', 'npm install ')
|
|
38
30
|
nodes.set('npm', node)
|
|
39
31
|
|
|
@@ -49,7 +41,6 @@ function remarkPkgManager() {
|
|
|
49
41
|
const choiceNodes = [...nodes].map(([name, node]) => ({ choiceValue: name, children: [node] }))
|
|
50
42
|
const replacement = generateChoiceGroupCode(choiceNodes, parent)
|
|
51
43
|
|
|
52
|
-
replacement.data ??= { customDataChoice: choice, customDataFilter: replacement.type }
|
|
53
44
|
parent.children.splice(index, 1, replacement)
|
|
54
45
|
})
|
|
55
46
|
}
|
|
@@ -25,6 +25,11 @@ const CHOICES_BUILT_IN: Record<string, { choices: string[]; default: string }> =
|
|
|
25
25
|
|
|
26
26
|
function generateChoiceGroupCode(choiceNodes: ChoiceNode[], parent: Parent, hide: boolean = false): MdxJsxFlowElement {
|
|
27
27
|
let lvl: number = 0
|
|
28
|
+
const customHidden = choiceNodes.some((node) =>
|
|
29
|
+
node.children.some((node) => node.type === 'containerDirective' && node.children[0]!.type !== 'code'),
|
|
30
|
+
)
|
|
31
|
+
const hidden = hide || customHidden
|
|
32
|
+
|
|
28
33
|
const { choiceGroup, mergedChoiceNodes } = resolveChoiceGroupNodes(choiceNodes)
|
|
29
34
|
const attributes: MdxJsxAttribute[] = []
|
|
30
35
|
const children: MdxJsxFlowElement[] = []
|
|
@@ -66,7 +71,7 @@ function generateChoiceGroupCode(choiceNodes: ChoiceNode[], parent: Parent, hide
|
|
|
66
71
|
}
|
|
67
72
|
|
|
68
73
|
attributes.push(
|
|
69
|
-
expressionToAttribute('choiceGroup', { ...choiceGroup, hidden: choiceNodes.length === 1 ||
|
|
74
|
+
expressionToAttribute('choiceGroup', { ...choiceGroup, hidden: choiceNodes.length === 1 || hidden, lvl }),
|
|
70
75
|
)
|
|
71
76
|
|
|
72
77
|
const choiceGroupNode: MdxJsxFlowElement = {
|
|
@@ -11,8 +11,10 @@ function remarkChoiceGroup() {
|
|
|
11
11
|
const meta = parseMetaString(node.meta, ['choice']);
|
|
12
12
|
const { choice } = meta.props;
|
|
13
13
|
node.meta = meta.rest;
|
|
14
|
-
if (choice)
|
|
15
|
-
|
|
14
|
+
if (choice) {
|
|
15
|
+
const filter = ['jsx', 'tsx', 'vue'].includes(node.lang ?? '') ? 'code-component' : `code-${node.lang}`;
|
|
16
|
+
node.data ?? (node.data = { customDataChoice: choice, customDataFilter: filter });
|
|
17
|
+
}
|
|
16
18
|
}
|
|
17
19
|
if (node.type === 'containerDirective' && node.name === 'Choice') {
|
|
18
20
|
if (!node.attributes)
|
|
@@ -24,9 +26,8 @@ function remarkChoiceGroup() {
|
|
|
24
26
|
}
|
|
25
27
|
}
|
|
26
28
|
});
|
|
27
|
-
const replaced = new WeakSet();
|
|
28
29
|
visit(tree, (node) => {
|
|
29
|
-
if (!('children' in node) ||
|
|
30
|
+
if (!('children' in node) || node.data?.customDataIsVisited)
|
|
30
31
|
return 'skip';
|
|
31
32
|
let start = -1;
|
|
32
33
|
let end = 0;
|
|
@@ -38,16 +39,17 @@ function remarkChoiceGroup() {
|
|
|
38
39
|
const replacements = [];
|
|
39
40
|
for (const choiceNodes of choiceNodesFiltered) {
|
|
40
41
|
const replacement = generateChoiceGroupCode(choiceNodes, node);
|
|
42
|
+
replacement.data ?? (replacement.data = {});
|
|
43
|
+
replacement.data.customDataIsVisited = true;
|
|
41
44
|
replacements.push(replacement);
|
|
42
45
|
}
|
|
43
|
-
replaced.add(replacements);
|
|
44
46
|
node.children.splice(start, end - start, ...replacements);
|
|
45
47
|
end = start;
|
|
46
48
|
start = -1;
|
|
47
49
|
};
|
|
48
50
|
for (; end < node.children.length; end++) {
|
|
49
51
|
const child = node.children[end];
|
|
50
|
-
if (!['code', '
|
|
52
|
+
if (!['code', 'containerDirective'].includes(child.type)) {
|
|
51
53
|
process();
|
|
52
54
|
continue;
|
|
53
55
|
}
|
|
@@ -72,7 +74,6 @@ function filterChoices(nodes) {
|
|
|
72
74
|
.map((node) => {
|
|
73
75
|
const choice = node.data.customDataChoice;
|
|
74
76
|
const nodes = nodesByChoice.get(choice) ?? [];
|
|
75
|
-
node.data.customDataChoice = undefined;
|
|
76
77
|
nodes.push(node);
|
|
77
78
|
nodesByChoice.set(choice, nodes);
|
|
78
79
|
});
|
|
@@ -22,6 +22,9 @@ function remarkDetype() {
|
|
|
22
22
|
// Skip if 'ts-only' meta is present
|
|
23
23
|
if (node.meta?.includes('ts-only'))
|
|
24
24
|
return;
|
|
25
|
+
// Skip if 'customDataChoice' is 'TypeScript'
|
|
26
|
+
if (node.data?.customDataChoice === 'TypeScript')
|
|
27
|
+
return;
|
|
25
28
|
// Collect this node for post-processing
|
|
26
29
|
code_nodes.push({ codeBlock: node, index, parent });
|
|
27
30
|
});
|
|
@@ -41,9 +44,6 @@ function transformYaml(node) {
|
|
|
41
44
|
// Skip wrapping if the YAML code block hasn't changed
|
|
42
45
|
if (codeBlockContentJs === codeBlock.value)
|
|
43
46
|
return;
|
|
44
|
-
const meta = parseMetaString(codeBlock.meta, ['choice']);
|
|
45
|
-
const { choice } = meta.props;
|
|
46
|
-
codeBlock.meta = meta.rest;
|
|
47
47
|
const { position, ...rest } = codeBlock;
|
|
48
48
|
// Create a new code node for the JS version based on the modified YAML
|
|
49
49
|
const yamlJsCode = {
|
|
@@ -56,18 +56,13 @@ function transformYaml(node) {
|
|
|
56
56
|
{ choiceValue: 'TypeScript', children: [codeBlock] },
|
|
57
57
|
];
|
|
58
58
|
const replacement = generateChoiceGroupCode(choiceNodes, parent, true);
|
|
59
|
-
replacement.data ?? (replacement.data = { customDataChoice: choice, customDataFilter: 'codeLang' });
|
|
60
59
|
parent.children.splice(index, 1, replacement);
|
|
61
60
|
}
|
|
62
61
|
async function transformTsToJs(node, file) {
|
|
63
62
|
const { codeBlock, index, parent } = node;
|
|
64
|
-
const meta = parseMetaString(codeBlock.meta, ['max-width'
|
|
63
|
+
const meta = parseMetaString(codeBlock.meta, ['max-width']);
|
|
65
64
|
const maxWidth = Number(meta.props['max-width']);
|
|
66
|
-
const { choice } = meta.props;
|
|
67
65
|
codeBlock.meta = meta.rest;
|
|
68
|
-
codeBlock.data ?? (codeBlock.data = { customDataChoice: choice, customDataFilter: 'codeLang' });
|
|
69
|
-
if (choice === 'TypeScript')
|
|
70
|
-
return;
|
|
71
66
|
let codeBlockReplacedJs = replaceFileNameSuffixes(codeBlock.value);
|
|
72
67
|
let codeBlockContentJs = '';
|
|
73
68
|
// Remove TypeScript from the TS/TSX/Vue code node
|
|
@@ -104,7 +99,7 @@ async function transformTsToJs(node, file) {
|
|
|
104
99
|
// No wrapping needed if JS and TS code are still identical
|
|
105
100
|
if (codeBlockContentJs === codeBlock.value)
|
|
106
101
|
return;
|
|
107
|
-
const { position, lang,
|
|
102
|
+
const { position, lang, ...rest } = codeBlock;
|
|
108
103
|
const tsCode = { ...rest, lang };
|
|
109
104
|
const jsCode = {
|
|
110
105
|
...rest,
|
|
@@ -120,7 +115,6 @@ async function transformTsToJs(node, file) {
|
|
|
120
115
|
// Add `hide` prop to `<ChoiceGroup>` if the only change was replacing `.ts` with `.js`
|
|
121
116
|
const hide = codeBlockReplacedJs === codeBlockContentJs;
|
|
122
117
|
const replacement = generateChoiceGroupCode(choiceNodes, parent, hide);
|
|
123
|
-
replacement.data ?? (replacement.data = { ...data });
|
|
124
118
|
parent.children.splice(index, 1, replacement);
|
|
125
119
|
}
|
|
126
120
|
// Replace all '.ts' extensions with '.js'
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
export { remarkPkgManager };
|
|
2
2
|
import { visit } from 'unist-util-visit';
|
|
3
3
|
import convert_ from 'npm-to-yarn';
|
|
4
|
-
import { parseMetaString } from './rehypeMetaToProps.js';
|
|
5
4
|
import { generateChoiceGroupCode } from './utils/generateChoiceGroupCode.js';
|
|
6
5
|
import { assertUsage } from '../utils/assert.js';
|
|
7
6
|
import pc from '@brillout/picocolors';
|
|
@@ -18,13 +17,7 @@ function remarkPkgManager() {
|
|
|
18
17
|
assertUsage(!node.value.includes('pnpm'), `Found a 'pnpm' command in the code block at: ${pc.bold(pc.blue(file.path))}, line ${node.position?.start.line}. Replace it with the equivalent 'npm' command for the package manager toggle to work.`);
|
|
19
18
|
if (!node.value.includes('npm ') && !node.value.includes('npx '))
|
|
20
19
|
return;
|
|
21
|
-
let choice = undefined;
|
|
22
20
|
const nodes = new Map();
|
|
23
|
-
if (node.meta) {
|
|
24
|
-
const meta = parseMetaString(node.meta, ['choice']);
|
|
25
|
-
choice = meta.props['choice'];
|
|
26
|
-
node.meta = meta.rest;
|
|
27
|
-
}
|
|
28
21
|
node.value = node.value.replaceAll('npm i ', 'npm install ');
|
|
29
22
|
nodes.set('npm', node);
|
|
30
23
|
for (const pm of PKG_MANAGERS) {
|
|
@@ -37,7 +30,6 @@ function remarkPkgManager() {
|
|
|
37
30
|
}
|
|
38
31
|
const choiceNodes = [...nodes].map(([name, node]) => ({ choiceValue: name, children: [node] }));
|
|
39
32
|
const replacement = generateChoiceGroupCode(choiceNodes, parent);
|
|
40
|
-
replacement.data ?? (replacement.data = { customDataChoice: choice, customDataFilter: replacement.type });
|
|
41
33
|
parent.children.splice(index, 1, replacement);
|
|
42
34
|
});
|
|
43
35
|
};
|
|
@@ -14,6 +14,8 @@ const CHOICES_BUILT_IN = {
|
|
|
14
14
|
};
|
|
15
15
|
function generateChoiceGroupCode(choiceNodes, parent, hide = false) {
|
|
16
16
|
let lvl = 0;
|
|
17
|
+
const customHidden = choiceNodes.some((node) => node.children.some((node) => node.type === 'containerDirective' && node.children[0].type !== 'code'));
|
|
18
|
+
const hidden = hide || customHidden;
|
|
17
19
|
const { choiceGroup, mergedChoiceNodes } = resolveChoiceGroupNodes(choiceNodes);
|
|
18
20
|
const attributes = [];
|
|
19
21
|
const children = [];
|
|
@@ -49,7 +51,7 @@ function generateChoiceGroupCode(choiceNodes, parent, hide = false) {
|
|
|
49
51
|
lvl = parentLvl + 1;
|
|
50
52
|
parent.data.customDataParentChoiceGroup = undefined;
|
|
51
53
|
}
|
|
52
|
-
attributes.push(expressionToAttribute('choiceGroup', { ...choiceGroup, hidden: choiceNodes.length === 1 ||
|
|
54
|
+
attributes.push(expressionToAttribute('choiceGroup', { ...choiceGroup, hidden: choiceNodes.length === 1 || hidden, lvl }));
|
|
53
55
|
const choiceGroupNode = {
|
|
54
56
|
type: 'mdxJsxFlowElement',
|
|
55
57
|
name: 'ChoiceGroup',
|