@brillout/docpress 0.16.15 → 0.16.16
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/+config.ts +7 -1
- package/code-blocks/components/ChoiceGroup.tsx +10 -55
- package/code-blocks/remarkDetype.ts +1 -4
- package/code-blocks/remarkPkgManager.ts +12 -3
- package/code-blocks/utils/generateChoiceGroupCode.ts +60 -13
- package/dist/+config.d.ts +11 -1
- package/dist/+config.js +4 -0
- package/dist/code-blocks/remarkDetype.js +1 -4
- package/dist/code-blocks/remarkPkgManager.d.ts +2 -1
- package/dist/code-blocks/remarkPkgManager.js +6 -3
- package/dist/code-blocks/utils/generateChoiceGroupCode.js +50 -13
- package/dist/types/Config.d.ts +2 -2
- package/docsearch/DocSearchInstall.tsx +3 -3
- package/package.json +3 -2
- package/types/Config.ts +3 -2
package/+config.ts
CHANGED
|
@@ -2,7 +2,7 @@ export { config as default }
|
|
|
2
2
|
|
|
3
3
|
import type { Config } from 'vike/types'
|
|
4
4
|
import { viteConfig } from './vite.config.js'
|
|
5
|
-
import type { Config as DocpressConfig } from './types/Config.js'
|
|
5
|
+
import type { Config as DocpressConfig, ChoicesConfig } from './types/Config.js'
|
|
6
6
|
import type { PageSection } from './parsePageSections.js'
|
|
7
7
|
import type { Resolved } from './resolvePageContext.js'
|
|
8
8
|
|
|
@@ -21,6 +21,10 @@ const config = {
|
|
|
21
21
|
env: { server: true, client: true },
|
|
22
22
|
global: true,
|
|
23
23
|
},
|
|
24
|
+
choices: {
|
|
25
|
+
env: { server: true, client: true, config: true },
|
|
26
|
+
global: true,
|
|
27
|
+
},
|
|
24
28
|
},
|
|
25
29
|
prefetch: {
|
|
26
30
|
staticAssets: 'hover',
|
|
@@ -37,9 +41,11 @@ declare global {
|
|
|
37
41
|
}
|
|
38
42
|
interface Config {
|
|
39
43
|
docpress?: DocpressConfig
|
|
44
|
+
choices?: ChoicesConfig
|
|
40
45
|
}
|
|
41
46
|
interface ConfigResolved {
|
|
42
47
|
docpress: DocpressConfig
|
|
48
|
+
choices: ChoicesConfig
|
|
43
49
|
pageSectionsExport: PageSection[] | undefined
|
|
44
50
|
}
|
|
45
51
|
}
|
|
@@ -1,36 +1,26 @@
|
|
|
1
1
|
export { ChoiceGroup }
|
|
2
2
|
|
|
3
3
|
import React from 'react'
|
|
4
|
-
import { usePageContext } from '../../renderer/usePageContext.js'
|
|
5
4
|
import { useSelectedChoice } from '../hooks/useSelectedChoice.js'
|
|
6
5
|
import { useRestoreScroll } from '../hooks/useRestoreScroll.js'
|
|
7
|
-
import { assertUsage } from '../../utils/assert.js'
|
|
8
6
|
import { cls } from '../../utils/cls.js'
|
|
9
|
-
import type { PageContext } from 'vike/types'
|
|
10
7
|
import './ChoiceGroup.css'
|
|
11
8
|
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
pkgManager: {
|
|
18
|
-
choices: ['npm', 'pnpm', 'Bun', 'Yarn'],
|
|
19
|
-
default: 'npm',
|
|
20
|
-
},
|
|
9
|
+
type TChoiceGroup = {
|
|
10
|
+
name: string
|
|
11
|
+
choices: string[]
|
|
12
|
+
default: string
|
|
13
|
+
disabled: string[]
|
|
21
14
|
}
|
|
22
15
|
|
|
23
16
|
function ChoiceGroup({
|
|
24
17
|
children,
|
|
25
|
-
|
|
18
|
+
choiceGroup,
|
|
26
19
|
lvl,
|
|
27
20
|
hide = false,
|
|
28
|
-
}: { children: React.ReactNode;
|
|
29
|
-
const
|
|
30
|
-
const choiceGroup = findChoiceGroup(pageContext, choices)
|
|
31
|
-
const [selectedChoice, setSelectedChoice] = useSelectedChoice(choiceGroup.name, choiceGroup.default!)
|
|
21
|
+
}: { children: React.ReactNode; choiceGroup: TChoiceGroup; lvl: number; hide: boolean }) {
|
|
22
|
+
const [selectedChoice, setSelectedChoice] = useSelectedChoice(choiceGroup.name, choiceGroup.default)
|
|
32
23
|
const prevPositionRef = useRestoreScroll([selectedChoice])
|
|
33
|
-
const isHidden = choices.length === 1 || !choices.includes(selectedChoice) || hide
|
|
34
24
|
|
|
35
25
|
return (
|
|
36
26
|
<div data-choice-group={choiceGroup.name} className="choice-group">
|
|
@@ -38,11 +28,11 @@ function ChoiceGroup({
|
|
|
38
28
|
name={`choicesFor-${choiceGroup.name}`}
|
|
39
29
|
value={selectedChoice}
|
|
40
30
|
onChange={onChange}
|
|
41
|
-
className={cls(['select-choice',
|
|
31
|
+
className={cls(['select-choice', hide && 'hidden'])}
|
|
42
32
|
style={{ '--lvl': lvl }}
|
|
43
33
|
>
|
|
44
34
|
{choiceGroup.choices.map((choice, i) => (
|
|
45
|
-
<option key={i} value={choice} disabled={
|
|
35
|
+
<option key={i} value={choice} disabled={choiceGroup.disabled.includes(choice)}>
|
|
46
36
|
{choice}
|
|
47
37
|
</option>
|
|
48
38
|
))}
|
|
@@ -57,38 +47,3 @@ function ChoiceGroup({
|
|
|
57
47
|
setSelectedChoice(el.value)
|
|
58
48
|
}
|
|
59
49
|
}
|
|
60
|
-
|
|
61
|
-
function findChoiceGroup(pageContext: PageContext, choices: string[]) {
|
|
62
|
-
const { choices: choicesConfig } = pageContext.globalContext.config.docpress
|
|
63
|
-
const choicesAll = { ...CHOICES_BUILT_IN, ...choicesConfig }
|
|
64
|
-
|
|
65
|
-
const groupName = Object.keys(choicesAll).find((key) => {
|
|
66
|
-
// get only the values that exist in both choices and choicesAll[key].choices
|
|
67
|
-
const relevantChoices = choicesAll[key]!.choices.filter((choice) => choices.includes(choice))
|
|
68
|
-
// if nothing exists, skip this key
|
|
69
|
-
if (relevantChoices.length === 0) return false
|
|
70
|
-
|
|
71
|
-
// check order
|
|
72
|
-
let i = 0
|
|
73
|
-
for (const choice of choices) {
|
|
74
|
-
if (choice === relevantChoices[i]) i++
|
|
75
|
-
}
|
|
76
|
-
assertUsage(
|
|
77
|
-
i === relevantChoices.length,
|
|
78
|
-
`Choices exist for key "${key}" but NOT in order. Expected order: [${relevantChoices}], got: [${choices}]`,
|
|
79
|
-
)
|
|
80
|
-
|
|
81
|
-
return true
|
|
82
|
-
})
|
|
83
|
-
assertUsage(groupName, `Missing group name for [${choices}]. Define it in +docpress.choices.`)
|
|
84
|
-
|
|
85
|
-
const mergedChoices = [...new Set([...choices, ...choicesAll[groupName]!.choices])]
|
|
86
|
-
|
|
87
|
-
const choiceGroup = {
|
|
88
|
-
name: groupName,
|
|
89
|
-
...choicesAll[groupName]!,
|
|
90
|
-
choices: mergedChoices,
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
return choiceGroup
|
|
94
|
-
}
|
|
@@ -6,11 +6,8 @@ import { visit } from 'unist-util-visit'
|
|
|
6
6
|
import { assertUsage } from '../utils/assert.js'
|
|
7
7
|
import { parseMetaString } from './rehypeMetaToProps.js'
|
|
8
8
|
import { generateChoiceGroupCode } from './utils/generateChoiceGroupCode.js'
|
|
9
|
+
import { transform as detype } from 'detype'
|
|
9
10
|
import pc from '@brillout/picocolors'
|
|
10
|
-
import module from 'node:module'
|
|
11
|
-
// Cannot use `import { transform } from 'detype'` as it results in errors,
|
|
12
|
-
// and the package has no default export. Using `module.createRequire` instead.
|
|
13
|
-
const { transform: detype } = module.createRequire(import.meta.url)('detype') as typeof import('detype')
|
|
14
11
|
|
|
15
12
|
const prettierOptions: NonNullable<Parameters<typeof detype>[2]>['prettierOptions'] = {
|
|
16
13
|
semi: false,
|
|
@@ -1,21 +1,30 @@
|
|
|
1
1
|
export { remarkPkgManager }
|
|
2
2
|
|
|
3
3
|
import type { Code, Root } from 'mdast'
|
|
4
|
+
import type { VFile } from '@mdx-js/mdx/internal-create-format-aware-processors'
|
|
4
5
|
import { visit } from 'unist-util-visit'
|
|
5
6
|
import convert_ from 'npm-to-yarn'
|
|
6
7
|
import { parseMetaString } from './rehypeMetaToProps.js'
|
|
7
8
|
import { generateChoiceGroupCode } from './utils/generateChoiceGroupCode.js'
|
|
9
|
+
import { assertUsage } from '../utils/assert.js'
|
|
10
|
+
import pc from '@brillout/picocolors'
|
|
8
11
|
// @ts-expect-error The type of npm-to-yarn doesn't work with `"moduleResolution": "Node16"`
|
|
9
12
|
const convert: (str: string, to: 'npm' | 'yarn' | 'pnpm' | 'bun') => string = convert_
|
|
10
13
|
|
|
11
14
|
const PKG_MANAGERS = ['pnpm', 'Bun', 'Yarn'] as const
|
|
12
15
|
|
|
13
16
|
function remarkPkgManager() {
|
|
14
|
-
return function (tree: Root) {
|
|
17
|
+
return function (tree: Root, file: VFile) {
|
|
15
18
|
visit(tree, 'code', (node, index, parent) => {
|
|
16
19
|
if (!parent || typeof index === 'undefined') return
|
|
17
|
-
if (!['sh', 'shell'].includes(node.lang || '')) return
|
|
18
|
-
|
|
20
|
+
if (!['bash', 'sh', 'shell'].includes(node.lang || '')) return
|
|
21
|
+
assertUsage(
|
|
22
|
+
!node.value.includes('pnpm'),
|
|
23
|
+
`Found a 'pnpm' command in the code block at: ${pc.bold(pc.blue(file.path))}, line ${
|
|
24
|
+
node.position?.start.line
|
|
25
|
+
}. Replace it with the equivalent 'npm' command for the package manager toggle to work.`,
|
|
26
|
+
)
|
|
27
|
+
if (!node.value.includes('npm ') && !node.value.includes('npx ')) return
|
|
19
28
|
let choice: string | undefined = undefined
|
|
20
29
|
const nodes = new Map<string, Code>()
|
|
21
30
|
|
|
@@ -1,21 +1,49 @@
|
|
|
1
1
|
export { generateChoiceGroupCode }
|
|
2
2
|
export type { ChoiceNode }
|
|
3
3
|
|
|
4
|
+
import type { VikeConfig } from 'vike/types'
|
|
4
5
|
import type { BlockContent, DefinitionContent, Parent } from 'mdast'
|
|
5
6
|
import type { MdxJsxAttribute, MdxJsxFlowElement } from 'mdast-util-mdx-jsx'
|
|
7
|
+
import { getVikeConfig } from 'vike/plugin'
|
|
8
|
+
import { assertUsage } from '../../utils/assert.js'
|
|
9
|
+
import { valueToEstree } from 'estree-util-value-to-estree'
|
|
6
10
|
|
|
7
11
|
type ChoiceNode = {
|
|
8
12
|
choiceValue: string
|
|
9
13
|
children: (BlockContent | DefinitionContent)[]
|
|
10
14
|
}
|
|
11
15
|
|
|
16
|
+
const CHOICES_BUILT_IN: Record<string, { choices: string[]; default: string }> = {
|
|
17
|
+
codeLang: {
|
|
18
|
+
choices: ['JavaScript', 'TypeScript'],
|
|
19
|
+
default: 'JavaScript',
|
|
20
|
+
},
|
|
21
|
+
pkgManager: {
|
|
22
|
+
choices: ['npm', 'pnpm', 'Bun', 'Yarn'],
|
|
23
|
+
default: 'npm',
|
|
24
|
+
},
|
|
25
|
+
}
|
|
26
|
+
|
|
12
27
|
function generateChoiceGroupCode(choiceNodes: ChoiceNode[], parent?: Parent): MdxJsxFlowElement {
|
|
28
|
+
const vikeConfig = getVikeConfig()
|
|
29
|
+
const choices = choiceNodes.map((choiceNode) => choiceNode.choiceValue)
|
|
30
|
+
const choiceGroup = findChoiceGroup(vikeConfig, choices)
|
|
31
|
+
|
|
32
|
+
const mergedChoiceNodes = choiceGroup.choices.map((choice) => {
|
|
33
|
+
const node = choiceNodes.find((n) => n.choiceValue === choice)
|
|
34
|
+
|
|
35
|
+
return {
|
|
36
|
+
choiceValue: choice,
|
|
37
|
+
children: node?.children ?? [],
|
|
38
|
+
}
|
|
39
|
+
})
|
|
40
|
+
|
|
13
41
|
const attributes: MdxJsxAttribute[] = []
|
|
14
42
|
const children: MdxJsxFlowElement[] = []
|
|
15
43
|
|
|
16
44
|
attributes.push({
|
|
17
45
|
type: 'mdxJsxAttribute',
|
|
18
|
-
name: '
|
|
46
|
+
name: 'choiceGroup',
|
|
19
47
|
value: {
|
|
20
48
|
type: 'mdxJsxAttributeValueExpression',
|
|
21
49
|
value: '',
|
|
@@ -25,23 +53,18 @@ function generateChoiceGroupCode(choiceNodes: ChoiceNode[], parent?: Parent): Md
|
|
|
25
53
|
sourceType: 'module',
|
|
26
54
|
comments: [],
|
|
27
55
|
body: [
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
expression: {
|
|
31
|
-
type: 'ArrayExpression',
|
|
32
|
-
// @ts-ignore: Missing properties in type definition
|
|
33
|
-
elements: choiceNodes.map((choiceNode) => ({
|
|
34
|
-
type: 'Literal',
|
|
35
|
-
value: choiceNode.choiceValue,
|
|
36
|
-
})),
|
|
37
|
-
},
|
|
38
|
-
},
|
|
56
|
+
// @ts-ignore: Missing properties in type definition
|
|
57
|
+
{ type: 'ExpressionStatement', expression: valueToEstree(choiceGroup) },
|
|
39
58
|
],
|
|
40
59
|
},
|
|
41
60
|
},
|
|
42
61
|
},
|
|
43
62
|
})
|
|
44
63
|
|
|
64
|
+
if (choiceNodes.length === 1) {
|
|
65
|
+
attributes.push({ type: 'mdxJsxAttribute', name: 'hide' })
|
|
66
|
+
}
|
|
67
|
+
|
|
45
68
|
let initLvl: number
|
|
46
69
|
|
|
47
70
|
switch (parent?.type) {
|
|
@@ -59,7 +82,7 @@ function generateChoiceGroupCode(choiceNodes: ChoiceNode[], parent?: Parent): Md
|
|
|
59
82
|
|
|
60
83
|
attributes.push({ type: 'mdxJsxAttribute', name: 'lvl', value: `${initLvl}` })
|
|
61
84
|
|
|
62
|
-
for (const choiceNode of
|
|
85
|
+
for (const choiceNode of mergedChoiceNodes) {
|
|
63
86
|
const choiceChildren: (BlockContent | DefinitionContent)[] = []
|
|
64
87
|
if (choiceNode.children.every((node) => node.type === 'containerDirective')) {
|
|
65
88
|
choiceChildren.push(...choiceNode.children.flatMap((node) => [...node.children]))
|
|
@@ -87,6 +110,30 @@ function generateChoiceGroupCode(choiceNodes: ChoiceNode[], parent?: Parent): Md
|
|
|
87
110
|
}
|
|
88
111
|
}
|
|
89
112
|
|
|
113
|
+
function findChoiceGroup(vikeConfig: VikeConfig, choices: string[]) {
|
|
114
|
+
const { choices: choicesConfig } = vikeConfig.config
|
|
115
|
+
const choicesAll = { ...CHOICES_BUILT_IN, ...choicesConfig }
|
|
116
|
+
|
|
117
|
+
const groupName = Object.keys(choicesAll).find((key) => {
|
|
118
|
+
// get only the values that exist in both choices and choicesAll[key].choices
|
|
119
|
+
const existsChoices = choicesAll[key]!.choices.filter((choice) => choices.includes(choice))
|
|
120
|
+
// if nothing exists, skip this key
|
|
121
|
+
if (existsChoices.length === 0) return false
|
|
122
|
+
return true
|
|
123
|
+
})
|
|
124
|
+
assertUsage(groupName, `Missing group name for [${choices}]. Define it in +docpress.choices.`)
|
|
125
|
+
|
|
126
|
+
const disabled = choicesAll[groupName]!.choices.filter((choice) => !choices.includes(choice))
|
|
127
|
+
|
|
128
|
+
const choiceGroup = {
|
|
129
|
+
name: groupName,
|
|
130
|
+
...choicesAll[groupName]!,
|
|
131
|
+
disabled,
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
return choiceGroup
|
|
135
|
+
}
|
|
136
|
+
|
|
90
137
|
function increaseLvl(node: BlockContent | DefinitionContent) {
|
|
91
138
|
if (node.type === 'mdxJsxFlowElement' && node.name === 'ChoiceGroup') {
|
|
92
139
|
const attribute = node.attributes.find(
|
package/dist/+config.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
export { config as default };
|
|
2
|
-
import type { Config as DocpressConfig } from './types/Config.js';
|
|
2
|
+
import type { Config as DocpressConfig, ChoicesConfig } from './types/Config.js';
|
|
3
3
|
import type { PageSection } from './parsePageSections.js';
|
|
4
4
|
import type { Resolved } from './resolvePageContext.js';
|
|
5
5
|
declare const config: {
|
|
@@ -24,6 +24,14 @@ declare const config: {
|
|
|
24
24
|
};
|
|
25
25
|
global: true;
|
|
26
26
|
};
|
|
27
|
+
choices: {
|
|
28
|
+
env: {
|
|
29
|
+
server: true;
|
|
30
|
+
client: true;
|
|
31
|
+
config: true;
|
|
32
|
+
};
|
|
33
|
+
global: true;
|
|
34
|
+
};
|
|
27
35
|
};
|
|
28
36
|
prefetch: {
|
|
29
37
|
staticAssets: "hover";
|
|
@@ -39,9 +47,11 @@ declare global {
|
|
|
39
47
|
}
|
|
40
48
|
interface Config {
|
|
41
49
|
docpress?: DocpressConfig;
|
|
50
|
+
choices?: ChoicesConfig;
|
|
42
51
|
}
|
|
43
52
|
interface ConfigResolved {
|
|
44
53
|
docpress: DocpressConfig;
|
|
54
|
+
choices: ChoicesConfig;
|
|
45
55
|
pageSectionsExport: PageSection[] | undefined;
|
|
46
56
|
}
|
|
47
57
|
}
|
package/dist/+config.js
CHANGED
|
@@ -3,11 +3,8 @@ import { visit } from 'unist-util-visit';
|
|
|
3
3
|
import { assertUsage } from '../utils/assert.js';
|
|
4
4
|
import { parseMetaString } from './rehypeMetaToProps.js';
|
|
5
5
|
import { generateChoiceGroupCode } from './utils/generateChoiceGroupCode.js';
|
|
6
|
+
import { transform as detype } from 'detype';
|
|
6
7
|
import pc from '@brillout/picocolors';
|
|
7
|
-
import module from 'node:module';
|
|
8
|
-
// Cannot use `import { transform } from 'detype'` as it results in errors,
|
|
9
|
-
// and the package has no default export. Using `module.createRequire` instead.
|
|
10
|
-
const { transform: detype } = module.createRequire(import.meta.url)('detype');
|
|
11
8
|
const prettierOptions = {
|
|
12
9
|
semi: false,
|
|
13
10
|
singleQuote: true,
|
|
@@ -1,3 +1,4 @@
|
|
|
1
1
|
export { remarkPkgManager };
|
|
2
2
|
import type { Root } from 'mdast';
|
|
3
|
-
|
|
3
|
+
import type { VFile } from '@mdx-js/mdx/internal-create-format-aware-processors';
|
|
4
|
+
declare function remarkPkgManager(): (tree: Root, file: VFile) => void;
|
|
@@ -3,17 +3,20 @@ import { visit } from 'unist-util-visit';
|
|
|
3
3
|
import convert_ from 'npm-to-yarn';
|
|
4
4
|
import { parseMetaString } from './rehypeMetaToProps.js';
|
|
5
5
|
import { generateChoiceGroupCode } from './utils/generateChoiceGroupCode.js';
|
|
6
|
+
import { assertUsage } from '../utils/assert.js';
|
|
7
|
+
import pc from '@brillout/picocolors';
|
|
6
8
|
// @ts-expect-error The type of npm-to-yarn doesn't work with `"moduleResolution": "Node16"`
|
|
7
9
|
const convert = convert_;
|
|
8
10
|
const PKG_MANAGERS = ['pnpm', 'Bun', 'Yarn'];
|
|
9
11
|
function remarkPkgManager() {
|
|
10
|
-
return function (tree) {
|
|
12
|
+
return function (tree, file) {
|
|
11
13
|
visit(tree, 'code', (node, index, parent) => {
|
|
12
14
|
if (!parent || typeof index === 'undefined')
|
|
13
15
|
return;
|
|
14
|
-
if (!['sh', 'shell'].includes(node.lang || ''))
|
|
16
|
+
if (!['bash', 'sh', 'shell'].includes(node.lang || ''))
|
|
15
17
|
return;
|
|
16
|
-
|
|
18
|
+
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
|
+
if (!node.value.includes('npm ') && !node.value.includes('npx '))
|
|
17
20
|
return;
|
|
18
21
|
let choice = undefined;
|
|
19
22
|
const nodes = new Map();
|
|
@@ -1,10 +1,33 @@
|
|
|
1
1
|
export { generateChoiceGroupCode };
|
|
2
|
+
import { getVikeConfig } from 'vike/plugin';
|
|
3
|
+
import { assertUsage } from '../../utils/assert.js';
|
|
4
|
+
import { valueToEstree } from 'estree-util-value-to-estree';
|
|
5
|
+
const CHOICES_BUILT_IN = {
|
|
6
|
+
codeLang: {
|
|
7
|
+
choices: ['JavaScript', 'TypeScript'],
|
|
8
|
+
default: 'JavaScript',
|
|
9
|
+
},
|
|
10
|
+
pkgManager: {
|
|
11
|
+
choices: ['npm', 'pnpm', 'Bun', 'Yarn'],
|
|
12
|
+
default: 'npm',
|
|
13
|
+
},
|
|
14
|
+
};
|
|
2
15
|
function generateChoiceGroupCode(choiceNodes, parent) {
|
|
16
|
+
const vikeConfig = getVikeConfig();
|
|
17
|
+
const choices = choiceNodes.map((choiceNode) => choiceNode.choiceValue);
|
|
18
|
+
const choiceGroup = findChoiceGroup(vikeConfig, choices);
|
|
19
|
+
const mergedChoiceNodes = choiceGroup.choices.map((choice) => {
|
|
20
|
+
const node = choiceNodes.find((n) => n.choiceValue === choice);
|
|
21
|
+
return {
|
|
22
|
+
choiceValue: choice,
|
|
23
|
+
children: node?.children ?? [],
|
|
24
|
+
};
|
|
25
|
+
});
|
|
3
26
|
const attributes = [];
|
|
4
27
|
const children = [];
|
|
5
28
|
attributes.push({
|
|
6
29
|
type: 'mdxJsxAttribute',
|
|
7
|
-
name: '
|
|
30
|
+
name: 'choiceGroup',
|
|
8
31
|
value: {
|
|
9
32
|
type: 'mdxJsxAttributeValueExpression',
|
|
10
33
|
value: '',
|
|
@@ -14,22 +37,16 @@ function generateChoiceGroupCode(choiceNodes, parent) {
|
|
|
14
37
|
sourceType: 'module',
|
|
15
38
|
comments: [],
|
|
16
39
|
body: [
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
expression: {
|
|
20
|
-
type: 'ArrayExpression',
|
|
21
|
-
// @ts-ignore: Missing properties in type definition
|
|
22
|
-
elements: choiceNodes.map((choiceNode) => ({
|
|
23
|
-
type: 'Literal',
|
|
24
|
-
value: choiceNode.choiceValue,
|
|
25
|
-
})),
|
|
26
|
-
},
|
|
27
|
-
},
|
|
40
|
+
// @ts-ignore: Missing properties in type definition
|
|
41
|
+
{ type: 'ExpressionStatement', expression: valueToEstree(choiceGroup) },
|
|
28
42
|
],
|
|
29
43
|
},
|
|
30
44
|
},
|
|
31
45
|
},
|
|
32
46
|
});
|
|
47
|
+
if (choiceNodes.length === 1) {
|
|
48
|
+
attributes.push({ type: 'mdxJsxAttribute', name: 'hide' });
|
|
49
|
+
}
|
|
33
50
|
let initLvl;
|
|
34
51
|
switch (parent?.type) {
|
|
35
52
|
case 'root':
|
|
@@ -43,7 +60,7 @@ function generateChoiceGroupCode(choiceNodes, parent) {
|
|
|
43
60
|
break;
|
|
44
61
|
}
|
|
45
62
|
attributes.push({ type: 'mdxJsxAttribute', name: 'lvl', value: `${initLvl}` });
|
|
46
|
-
for (const choiceNode of
|
|
63
|
+
for (const choiceNode of mergedChoiceNodes) {
|
|
47
64
|
const choiceChildren = [];
|
|
48
65
|
if (choiceNode.children.every((node) => node.type === 'containerDirective')) {
|
|
49
66
|
choiceChildren.push(...choiceNode.children.flatMap((node) => [...node.children]));
|
|
@@ -70,6 +87,26 @@ function generateChoiceGroupCode(choiceNodes, parent) {
|
|
|
70
87
|
children,
|
|
71
88
|
};
|
|
72
89
|
}
|
|
90
|
+
function findChoiceGroup(vikeConfig, choices) {
|
|
91
|
+
const { choices: choicesConfig } = vikeConfig.config;
|
|
92
|
+
const choicesAll = { ...CHOICES_BUILT_IN, ...choicesConfig };
|
|
93
|
+
const groupName = Object.keys(choicesAll).find((key) => {
|
|
94
|
+
// get only the values that exist in both choices and choicesAll[key].choices
|
|
95
|
+
const existsChoices = choicesAll[key].choices.filter((choice) => choices.includes(choice));
|
|
96
|
+
// if nothing exists, skip this key
|
|
97
|
+
if (existsChoices.length === 0)
|
|
98
|
+
return false;
|
|
99
|
+
return true;
|
|
100
|
+
});
|
|
101
|
+
assertUsage(groupName, `Missing group name for [${choices}]. Define it in +docpress.choices.`);
|
|
102
|
+
const disabled = choicesAll[groupName].choices.filter((choice) => !choices.includes(choice));
|
|
103
|
+
const choiceGroup = {
|
|
104
|
+
name: groupName,
|
|
105
|
+
...choicesAll[groupName],
|
|
106
|
+
disabled,
|
|
107
|
+
};
|
|
108
|
+
return choiceGroup;
|
|
109
|
+
}
|
|
73
110
|
function increaseLvl(node) {
|
|
74
111
|
if (node.type === 'mdxJsxFlowElement' && node.name === 'ChoiceGroup') {
|
|
75
112
|
const attribute = node.attributes.find((attribute) => attribute.type === 'mdxJsxAttribute' && attribute.name === 'lvl');
|
package/dist/types/Config.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export type { Config };
|
|
1
|
+
export type { Config, ChoicesConfig };
|
|
2
2
|
import type { HeadingDefinition, HeadingDetachedDefinition } from './Heading.js';
|
|
3
3
|
type Config = {
|
|
4
4
|
name: string;
|
|
@@ -37,7 +37,6 @@ type Config = {
|
|
|
37
37
|
navLogoStyle?: React.CSSProperties;
|
|
38
38
|
navLogoTextStyle?: React.CSSProperties;
|
|
39
39
|
globalNote?: React.ReactNode;
|
|
40
|
-
choices?: Record<string, Choice>;
|
|
41
40
|
};
|
|
42
41
|
/** Order in Algolia search results */
|
|
43
42
|
type Category = string | {
|
|
@@ -45,6 +44,7 @@ type Category = string | {
|
|
|
45
44
|
/** Hide from Algolia search */
|
|
46
45
|
hide?: boolean;
|
|
47
46
|
};
|
|
47
|
+
type ChoicesConfig = Record<string, Choice>;
|
|
48
48
|
type Choice = {
|
|
49
49
|
choices: string[];
|
|
50
50
|
default: string;
|
|
@@ -20,13 +20,13 @@ function DocSearchInstall() {
|
|
|
20
20
|
maxResultsPerGroup={Infinity}
|
|
21
21
|
searchParameters={{
|
|
22
22
|
filters: 'is_available:true',
|
|
23
|
+
hitsPerPage: 50,
|
|
23
24
|
}}
|
|
24
25
|
transformItems={(hits) => {
|
|
25
26
|
hits.map((hit) => {
|
|
26
|
-
if (hit.type === 'lvl1')
|
|
27
|
-
hit.url = hit.url.split('#')[0]!
|
|
28
|
-
}
|
|
27
|
+
if (hit.type === 'lvl1') hit.url = hit.url.split('#')[0]!
|
|
29
28
|
})
|
|
29
|
+
hits.sort((a, b) => Number(a.url.includes('#')) - Number(b.url.includes('#')))
|
|
30
30
|
return hits
|
|
31
31
|
}}
|
|
32
32
|
/>
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@brillout/docpress",
|
|
3
|
-
"version": "0.16.
|
|
3
|
+
"version": "0.16.16",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"dependencies": {
|
|
6
6
|
"@brillout/picocolors": "^1.0.10",
|
|
@@ -11,7 +11,8 @@
|
|
|
11
11
|
"@mdx-js/rollup": "3.0.1",
|
|
12
12
|
"@shikijs/transformers": "1.2.0",
|
|
13
13
|
"@vitejs/plugin-react-swc": "^3.10.2",
|
|
14
|
-
"detype": "^
|
|
14
|
+
"detype": "^2.0.2",
|
|
15
|
+
"estree-util-value-to-estree": "^3.5.0",
|
|
15
16
|
"npm-to-yarn": "^3.0.1",
|
|
16
17
|
"rehype-pretty-code": "0.13.0",
|
|
17
18
|
"remark-directive": "^4.0.0",
|
package/types/Config.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export type { Config }
|
|
1
|
+
export type { Config, ChoicesConfig }
|
|
2
2
|
|
|
3
3
|
import type { HeadingDefinition, HeadingDetachedDefinition } from './Heading.js'
|
|
4
4
|
|
|
@@ -48,7 +48,6 @@ type Config = {
|
|
|
48
48
|
navLogoTextStyle?: React.CSSProperties
|
|
49
49
|
|
|
50
50
|
globalNote?: React.ReactNode
|
|
51
|
-
choices?: Record<string, Choice>
|
|
52
51
|
}
|
|
53
52
|
|
|
54
53
|
/** Order in Algolia search results */
|
|
@@ -60,6 +59,8 @@ type Category =
|
|
|
60
59
|
hide?: boolean
|
|
61
60
|
}
|
|
62
61
|
|
|
62
|
+
type ChoicesConfig = Record<string, Choice>
|
|
63
|
+
|
|
63
64
|
type Choice = {
|
|
64
65
|
choices: string[]
|
|
65
66
|
default: string
|