Skip to content

Commit

Permalink
Use TS consistently in index pages
Browse files Browse the repository at this point in the history
  • Loading branch information
chgeo committed Feb 23, 2024
1 parent 7dd8e84 commit 7aef81b
Show file tree
Hide file tree
Showing 13 changed files with 33 additions and 27 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
const { base, themeConfig: { sidebar }} = global.VITEPRESS_CONFIG.site
import { join } from 'node:path'
import { ContentData, DefaultTheme } from 'vitepress'

export default (pages, basePath) => {
type ContentDataCustom = ContentData & {
title?:string
}

type SBItem = DefaultTheme.SidebarItem

export default (pages:ContentDataCustom[], basePath:string):ContentDataCustom[] => {
let items = findInItems(basePath, sidebar) || []
items = items.map(item => { return { ...item, link: item.link?.replace(/\.md$/, '') }})
const itemLinks = items.map(item => join(base, item.link||''))
Expand All @@ -13,29 +20,28 @@ export default (pages, basePath) => {
return p
})
.filter(p => {
const item = items.find(item => p.url.endsWith(item.link))
const item = items.find(item => item.link && p.url.endsWith(item.link))
if (item) p.title = item.text
return !!item
})
.filter(p => !p.url.endsWith(basePath))
.sort((p1, p2) => itemLinks.indexOf(p1.url) - itemLinks.indexOf(p2.url))
.map(p => {
// this data is inlined in each index page, so sparsely construct the final object
return {
url: p.url,
title: p.title,
frontmatter: {
synopsis: p.frontmatter.synopsis
}
}
})
.map(p => ({
url: p.url,
title: p.title,
frontmatter: {
synopsis: p.frontmatter.synopsis
},
// this data is inlined in each index page, so omit unnecessary data
src:undefined, html:undefined, excerpt:undefined
}))
}

function findInItems(url, items=[]) {
function findInItems(url:string, items:SBItem[]=[]):SBItem[]|undefined {
let res = items.find(item => item.link?.includes(url))
if (res) return res.items
for (const item of items) {
res = findInItems(url, item.items)
if (res) return res
const result = findInItems(url, item.items)
if (result) return result
}
}
2 changes: 1 addition & 1 deletion advanced/index.data.js → advanced/index.data.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { basename } from 'node:path'
import { createContentLoader } from 'vitepress'
import filter from '../.vitepress/theme/components/indexFilter.js'
import filter from '../.vitepress/theme/components/indexFilter.ts'

const basePath = basename(__dirname)
export default createContentLoader([
Expand Down
2 changes: 1 addition & 1 deletion advanced/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ This section provides information on advanced topics and concepts that are not c


<script setup>
import { data as pages } from './index.data.js'
import { data as pages } from './index.data.ts'
</script>

<br>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { basename } from 'node:path'
import { createContentLoader } from 'vitepress'
import filter from '../../.vitepress/theme/components/indexFilter.js'
import filter from '../../.vitepress/theme/components/indexFilter.ts'

const basePath = basename(__dirname)
export default createContentLoader([`**/${basePath}/*.md`], {
Expand Down
2 changes: 1 addition & 1 deletion advanced/publishing-apis/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ synopsis: How to publish APIs in different formats


<script setup>
import { data as pages } from './index.data.js'
import { data as pages } from './index.data.ts'
</script>

<br>
Expand Down
2 changes: 1 addition & 1 deletion cds/index.data.js → cds/index.data.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { basename } from 'node:path'
import { createContentLoader } from 'vitepress'
import filter from '../.vitepress/theme/components/indexFilter.js'
import filter from '../.vitepress/theme/components/indexFilter.ts'

const basePath = basename(__dirname)
export default createContentLoader(`**/${basePath}/*.md`, {
Expand Down
2 changes: 1 addition & 1 deletion cds/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ CDS models are plain JavaScript objects complying to the _[Core Schema Notation
[See the Nature of Models for more details](models){.learn-more}

<script setup>
import { data as pages } from './index.data.js'
import { data as pages } from './index.data.ts'
</script>

<br>
Expand Down
2 changes: 1 addition & 1 deletion guides/index.data.js → guides/index.data.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { basename } from 'node:path'
import { createContentLoader } from 'vitepress'
import filter from '../.vitepress/theme/components/indexFilter.js'
import filter from '../.vitepress/theme/components/indexFilter.ts'

const basePath = basename(__dirname)

Expand Down
2 changes: 1 addition & 1 deletion guides/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ The following figure illustrates a walkthrough of the most prominent tasks withi
![The graphic groups topics into three phases: Development, Deploy, Use. The development phase covers topics like domain modeling, sing and providing services, databases and frontends. The deploy phase covers the deployment as well as CI/CD, monitoring and publishing APIs and packages for reuse. The use phase is about the subscription flow of multitenant applications and about customizing and extending those applications. ](assets/cookbook-overview.drawio.svg)

<script setup>
import { data as pages } from './index.data.js'
import { data as pages } from './index.data.ts'
</script>

<br>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { basename } from 'node:path'
import { createContentLoader } from 'vitepress'
import filter from '../../.vitepress/theme/components/indexFilter.js'
import filter from '../../.vitepress/theme/components/indexFilter.ts'

const basePath = basename(__dirname)

Expand Down
2 changes: 1 addition & 1 deletion guides/security/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ This guide addresses
of CAP applications who need to understand how to develop, deploy and operate CAP applications in a secure way.

<script setup>
import { data as pages } from './index.data.js'
import { data as pages } from './index.data.ts'
</script>

<br>
Expand Down
2 changes: 1 addition & 1 deletion java/index.data.js → java/index.data.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { basename } from 'node:path'
import { createContentLoader } from 'vitepress'
import filter from '../.vitepress/theme/components/indexFilter.js'
import filter from '../.vitepress/theme/components/indexFilter.ts'

const basePath = basename(__dirname)

Expand Down
2 changes: 1 addition & 1 deletion java/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import { useData } from 'vitepress'
const { theme } = useData()
const { versions } = theme.value.capire

import { data as pages } from './index.data.js'
import { data as pages } from './index.data.ts'
</script>

<br>
Expand Down

0 comments on commit 7aef81b

Please sign in to comment.