Skip to content

Commit

Permalink
fix: fix bug where autoSetSeries does not work
Browse files Browse the repository at this point in the history
ISSUES CLOSED: #278
BREAKING CHANGE: documents sery should be placed in the `/series` file folder (mandatory)
  • Loading branch information
recoluan committed May 11, 2024
1 parent f2aad4e commit 689a9d8
Show file tree
Hide file tree
Showing 14 changed files with 95 additions and 83 deletions.
2 changes: 1 addition & 1 deletion docs/docs/guide/folder-specification.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ date: 2022-08-18 23:24:45
---

:::warning
reco 主题实现了博客的功能,但是 vuepress 没有办法区分博客或是文档,导致文档也会出现在博客区域,所以主题对博客和文档的存放位置进行了约束,博客文章请放在 `/blogs` 文件夹中(强制),文档请放在 `/docs` 中(不强制)。
reco 主题实现了博客的功能,但是 vuepress 没有办法区分博客或是文档,导致文档也会出现在博客区域,所以主题对博客和文档的存放位置进行了约束,博客文章请放在 `/blogs` 文件夹中(强制),文档系列请放在 `/series` 文件夹中(强制),普通文档请放在 `/docs` 中(不强制)。
:::
4 changes: 2 additions & 2 deletions docs/docs/theme/auto-set-series.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ date: 2023-02-17

## 介绍

为了节约用户的时间成本,主题可以自动将 `docs` 文件夹下的文档,按照文件夹嵌套关系生成系列的配置。
为了节约用户的时间成本,主题可以自动将 `series` 文件夹下的文档,按照文件夹嵌套关系生成系列的配置。
```
/
└─ .vuepress
└─ docs
└─ series
└─ 系列1
└─ 文档1.md
└─ 文档2.md
Expand Down
2 changes: 1 addition & 1 deletion docs/en/docs/guide/folder-specification.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ date: 2022-08-18 23:24:45
---

:::warning
The reco theme implements the function of the blog, but vuepress has no way to distinguish blogs or documents, so the documents will also appear in the blog area, so the theme restricts the storage location of blogs and documents, blog articles should be placed in the `/blogs` file folder (mandatory), documents should be placed in `/docs` (not mandatory).
The reco theme implements the function of the blog, but vuepress has no way to distinguish blogs or documents, so the documents will also appear in the blog area, so the theme restricts the storage location of blogs and documents, blog articles should be placed in the `/blogs` file folder (mandatory), documents sery should be placed in the `/series` file folder (mandatory), ordinary documents should be placed in `/docs` (not mandatory).
:::
4 changes: 2 additions & 2 deletions docs/en/docs/theme/auto-set-series.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ date: 2023-02-17

## Introduction

In order to save users' time cost, the theme can automatically generate a series of configurations for the documents under the `docs` folder according to the folder nesting relationship.
In order to save users' time cost, the theme can automatically generate a series of configurations for the documents under the `series` folder according to the folder nesting relationship.

```
/
└─ .vuepress
└─ docs
└─ series
└─ sery 1
└─ doc1.md
└─ doc2.md
Expand Down
38 changes: 19 additions & 19 deletions example/.vuepress/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,25 +57,25 @@ export default defineUserConfig({
gitBranch: 'main',
sourceDir: 'example',
lastUpdatedText: '',
series: {
'/group/': [
{
text: 'group',
icon: 'Language',
children: ['/group/bar.md', '/group/foo.md'],
},
],
'/blogs/': [
{
text: 'blogs1',
children: [
'/blogs/category1/blog1.md',
'/blogs/category1/blog2.md',
'/blogs/category1/blog3.md',
],
},
],
},
// series: {
// '/group/': [
// {
// text: 'group',
// icon: 'Language',
// children: ['/group/bar.md', '/group/foo.md'],
// },
// ],
// '/blogs/': [
// {
// text: 'blogs1',
// children: [
// '/blogs/category1/blog1.md',
// '/blogs/category1/blog2.md',
// '/blogs/category1/blog3.md',
// ],
// },
// ],
// },
navbar: [
{
text: '博客',
Expand Down
3 changes: 3 additions & 0 deletions example/series/sery1/aaa2.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
title: aaa2
---
3 changes: 3 additions & 0 deletions example/series/sery1/b/bbb1.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
title: bbb1
---
3 changes: 3 additions & 0 deletions example/series/sery1/b/bbb2.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
title: bbb2
---
3 changes: 3 additions & 0 deletions example/series/sery1/测试.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
title: aaa
---
3 changes: 3 additions & 0 deletions example/series/sery2/ccc.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
title: ccc
---
3 changes: 3 additions & 0 deletions example/series/sery2/ccc2.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
title: ccc2
---
75 changes: 35 additions & 40 deletions packages/@vuepress-reco/plugin-page/src/node/PageCreater.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,6 @@ export default class PageCreater {
}

parse() {
this._parsePageOptions()

this._setBlogsToCategoryPageData()

this._createExtendedPages()

/**
* The name of the file is changed in the develop environment,
* and bug of 404 appears during hot updates.
Expand All @@ -109,6 +103,12 @@ export default class PageCreater {
if (this.app.env.isBuild) {
this._parseChineseInPagePathToPinyin()
}

this._parsePageOptions()

this._setBlogsToCategoryPageData()

this._createExtendedPages()
}

// 将 path 中的中文转换成拼音
Expand Down Expand Up @@ -211,7 +211,7 @@ export default class PageCreater {

this.__blogs_to_be_released__ = blogsToBeReleased

blogsToBeReleased.forEach((page: ReleasedPage, index) => {
blogsToBeReleased.forEach((page: ReleasedPage) => {
const categoryKeysOfFrontmatter = Object.keys(page.frontmatter)
.filter((key: string) => {
return this.__category_keys__.includes(key)
Expand Down Expand Up @@ -320,45 +320,40 @@ export default class PageCreater {

// 设置系列
private _setSeries(page) {
let docSeries = ((page.filePath || '') as string).match(
/.+\/docs\/(.+)\/(.+)\/(.+)\.md$/
let matches = ((page.filePath || '') as string).match(
/\/series\/([^/]+)(\S*?)\/([^/.]+)\.md/
)

if (docSeries) {
const group = docSeries[2]
const series = `/docs/${docSeries[1]}/`
const filePath = `${series}${group}/${docSeries[3]}.md`

if (!this.__series__?.[series]) {
this.__series__[`/${series}/`] = [{
text: group,
children: [filePath],
}]
} else if ( !this.__series__[series].some((groupItem) => groupItem?.text === group)) {
this.__series__[series].push({
text: group,
children: [filePath],
if (!matches) return

const [filePath, sery, dirStr] = matches
const seryKey = `/series/${sery}/`

if (!this.__series__?.[seryKey]) {
this.__series__[seryKey] = []
}

const dirs = dirStr.split('/').splice(1)
const dirLen = dirs.length
if (dirLen === 0) {
this.__series__[seryKey].push(filePath)
return
}
dirs.reduce((total, current, index) => {
if (!total.some((groupItem) => groupItem?.text === current)) {
total.push({
text: current,
children: index === dirLen - 1 ? [filePath] : [],
})
} else {
this.__series__[series]
.find((groupItem) => groupItem?.text === group)
.children.push(filePath)
total.find((groupItem) => groupItem?.text === current).children.push(index === dirLen - 1 ? filePath : {
text: current,
children: index === dirLen - 1 ? [filePath] : [],
})
}
} else {
docSeries = ((page.filePath || '') as string).match(
/.+\/docs\/(.+)\/(.+)\.md$/
)
if (docSeries) {
const series = `/docs/${docSeries[1]}/`
const filePath = `${series}${docSeries[2]}.md`

if (!this.__series__?.[series]) {
this.__series__[series] = [filePath]
} else {
this.__series__[series].push(filePath)
}
}
}
return total
}, this.__series__[seryKey])
}
}

Expand Down
8 changes: 0 additions & 8 deletions packages/vuepress-theme-reco/src/client/clientSetup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,16 @@ import {
usePageData,
catalogSymbol,
headersToCatalog,
seriesItemsSymbol,
resolveSeriesItems,
useThemeLocaleData,
usePageFrontmatter,
} from '@composables/index.js'
import { useExtendPageData } from '@vuepress-reco/vuepress-plugin-page/composables'

export function applyClientSetup() {
// we need to access series items in multiple components
// so we make it global computed
const themeLocal = useThemeLocaleData()
const frontmatter = usePageFrontmatter()
const route = useRoute()
const { series } = useExtendPageData()
const seriesItems = computed(() =>
resolveSeriesItems(frontmatter.value, themeLocal.value, route, series)
)
provide(seriesItemsSymbol, seriesItems)

const page = usePageData()
const catalog = computed(() => headersToCatalog(page.value.headers))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import { inject } from 'vue'
import { computed } from 'vue'
import type { ComputedRef, InjectionKey } from 'vue'
import type { RouteLocationNormalizedLoaded } from 'vuepress/client'
import { useRoute, usePageFrontmatter, type RouteLocationNormalizedLoaded } from 'vuepress/client'
import { isPlainObject, isString, resolveLocalePath } from 'vuepress/shared'
import { useExtendPageData } from '@vuepress-reco/vuepress-plugin-page/composables'


import { useThemeLocaleData } from '@composables/index.js'

import type {
RecoThemeData,
Expand Down Expand Up @@ -37,19 +41,22 @@ export interface ResolvedSeriesItem extends Partial<NavLink> {

export type SeriesItemsRef = ComputedRef<ResolvedSeriesItem[]>

export const seriesItemsSymbol: InjectionKey<SeriesItemsRef> = Symbol(
'seriesItems'
)

export const useSeriesItems = (): SeriesItemsRef => {
const seriesItems = inject(seriesItemsSymbol)
const route = useRoute()
const { series } = useExtendPageData()
const themeLocal = useThemeLocaleData()
const frontmatter = usePageFrontmatter<RecoThemeNormalPageFrontmatter>()
const seriesItems = computed(() =>
resolveSeriesItems(frontmatter.value, themeLocal.value, route, series)
)

if (!seriesItems) {
throw new Error('useSeriesItems() is called without provider.')
}
return seriesItems
}

export const resolveSeriesItems = (
const resolveSeriesItems = (
frontmatter: RecoThemeNormalPageFrontmatter,
themeLocal: RecoThemeData,
route: RouteLocationNormalizedLoaded,
Expand Down Expand Up @@ -80,7 +87,7 @@ export const resolveSeriesItems = (
/**
* Resolve series items if the config is an array
*/
export const resolveArraySeriesItems = (seriesPath: string, seriesConfig: SeriesConfigArray): ResolvedSeriesItem[] => {
const resolveArraySeriesItems = (seriesPath: string, seriesConfig: SeriesConfigArray): ResolvedSeriesItem[] => {
const handleChildItem = (
item: ResolvedSeriesItem | SeriesGroup | SeriesItem | string,
): ResolvedSeriesItem => {
Expand Down Expand Up @@ -114,7 +121,7 @@ export const resolveArraySeriesItems = (seriesPath: string, seriesConfig: Series
/**
* Resolve series items if the config is a key -> value (path-prefix -> array) object
*/
export const resolveMultiSeriesItems = (
const resolveMultiSeriesItems = (
seriesConfig: SeriesConfigObject,
route: RouteLocationNormalizedLoaded
): ResolvedSeriesItem[] => {
Expand Down

0 comments on commit 689a9d8

Please sign in to comment.