Skip to content

Commit 02ecd28

Browse files
committed
dataPieces within same page should be rendered with stable order
fix: #80
1 parent f1f492b commit 02ecd28

File tree

4 files changed

+48
-26
lines changed

4 files changed

+48
-26
lines changed

packages/create-project/template-lib/docs/vite.config.ts

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,17 @@ module.exports = {
2828
if (!match) throw new Error('unexpected file: ' + absolute)
2929
const [_, componentName, demoName] = match
3030
const pageId = `/components/demos/${componentName}`
31-
// set page data
32-
const runtimeDataPaths = api.getRuntimeData(pageId)
33-
// the ?demo query will wrap the module with useful demoInfo
34-
runtimeDataPaths[demoName] = `${absolute}?demo`
31+
// register page data
32+
api.addPageData({
33+
pageId,
34+
key: demoName,
35+
// register demo runtime data path
36+
// the ?demo query will wrap the module with useful demoInfo
37+
// that will be consumed by theme-doc
38+
dataPath: `${absolute}?demo`,
39+
// register demo static data
40+
staticData: await helpers.extractStaticData(file),
41+
})
3542
}
3643
)
3744
}
@@ -46,12 +53,14 @@ module.exports = {
4653
if (!match) throw new Error('unexpected file: ' + absolute)
4754
const [_, componentName] = match
4855
const pageId = `/components/${componentName}`
49-
// set page data
50-
const runtimeDataPaths = api.getRuntimeData(pageId)
51-
runtimeDataPaths.main = absolute
52-
// set page staticData
53-
const staticData = api.getStaticData(pageId)
54-
staticData.main = await helpers.extractStaticData(file)
56+
// register page data
57+
api.addPageData({
58+
pageId,
59+
// register demo runtime data path
60+
dataPath: absolute,
61+
// register demo static data
62+
staticData: await helpers.extractStaticData(file),
63+
})
5564
}
5665
)
5766
},

packages/create-project/template-lib/src/Button/demos/demo1.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/**
22
* @title Button Demo1 Title
33
* @description Button demo1 description
4+
* @order 1
45
*/
56

67
import React from 'react'

packages/create-project/template-lib/src/Button/demos/demo2.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/**
22
* @title Button Demo2 Title
33
* @description Button demo2 description
4+
* @order 2
45
*/
56

67
import React from 'react'

packages/theme-doc/src/index.tsx

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -52,22 +52,33 @@ export function createTheme(themeConfig: ThemeConfig): React.FC<ThemeProps> {
5252
}
5353

5454
const pageStaticData = staticData[loadState.routePath]
55-
const body = Object.entries(pageData).map(([key, dataPart], idx) => {
56-
const ContentComp = (dataPart as any).default
57-
const pageStaticDataPart = pageStaticData?.[key]
58-
const content = (() => {
59-
if (pageStaticDataPart?.sourceType === 'md')
60-
return (
61-
<MDX>
62-
<ContentComp />
63-
</MDX>
64-
)
65-
if (dataPart?.isDemo)
66-
return <Demo style={{ margin: '16px 45px' }} {...dataPart} />
67-
return <ContentComp />
68-
})()
69-
return <React.Fragment key={key}>{content}</React.Fragment>
70-
})
55+
56+
const body = Object.entries(pageData)
57+
.map(([key, dataPart], idx) => {
58+
const ContentComp = (dataPart as any).default
59+
const pageStaticDataPart = pageStaticData?.[key]
60+
const content = (() => {
61+
if (pageStaticDataPart?.sourceType === 'md')
62+
return (
63+
<MDX>
64+
<ContentComp />
65+
</MDX>
66+
)
67+
if (dataPart?.isDemo)
68+
return <Demo style={{ margin: '16px 45px' }} {...dataPart} />
69+
return <ContentComp />
70+
})()
71+
const result = <React.Fragment key={key}>{content}</React.Fragment>
72+
let order = Number(pageStaticDataPart?.order || 1)
73+
if (Number.isNaN(order)) order = 1
74+
return { key, result, order }
75+
})
76+
.sort((a, b) => {
77+
// sort by (order, key)
78+
if (a.order !== b.order) return a.order - b.order
79+
return a.key.localeCompare(b.key)
80+
})
81+
.map(({ result }) => result)
7182

7283
return <AppLayout>{body}</AppLayout>
7384
}

0 commit comments

Comments
 (0)