Skip to content

Commit

Permalink
修复导航错误状态
Browse files Browse the repository at this point in the history
  • Loading branch information
qianmoQ committed Dec 29, 2024
1 parent 2704993 commit d6e5432
Show file tree
Hide file tree
Showing 9 changed files with 418 additions and 188 deletions.
14 changes: 13 additions & 1 deletion docs/content/docs/create-site/index.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
title: 创建你的网站
title: 创建您的网站
---

安装 PageForge 后,你可以使用它来创建你的网站。您需要跳转到需要创建网站的文件夹,该文件夹必须是一个空文件夹。然后,您可以使用 PageForge 的命令行工具来创建网站。
Expand Down Expand Up @@ -61,3 +61,15 @@ pageforge serve
将浏览器指向 [localhost:3000](http://localhost:3000 "localhost:3000" "_blank"),您应该会看到:

![img.png](img.png)

## 编译你的项目

---

完成编辑后,您可以使用以下方法从 Markdown 文件构建静态网站:

```bash
pageforge build
```

此目录的内容构成了您的项目文档。无需操作数据库或服务器,因为它是完全独立的。该站点可以托管在 GitHub Pages、GitLab Pages、您选择的 CDN 或您的私有 Web 空间上。
69 changes: 69 additions & 0 deletions docs/content/docs/customization.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
---
title: 自定义您的网站
---

PageForge 提供了一些可选的自定义选项,以满足不同的需求。

## 自定义 CSS

---

您可以在 `pageforge.yaml` 文件中指定自定义的 CSS 文件,以便覆盖默认的 CSS 样式。

```yaml
theme:
extra_css:
- /assets/extra.css
```
## 自定义 JavaScript
---
您可以在 `pageforge.yaml` 文件中指定自定义的 JavaScript 文件,以便覆盖默认的 JavaScript 脚本。

```yaml
theme:
extra_js:
- /assets/extra.js
```

## 自定义模板

---

您可以在项目根目录创建 `templates` 文件夹,并在其中创建自定义的模板文件,以便覆盖默认的模板文件。

```bash
templates
├── components
│   ├── a.js
│   ├── block-code.js
│   ├── checkbox.js
│   ├── h1.js
│   ├── h2.js
│   ├── h3.js
│   ├── h4.js
│   ├── h5.js
│   ├── h6.js
│   ├── hr.js
│   ├── image.js
│   ├── inline-code.js
│   ├── list.js
│   ├── p.js
│   └── space.js
├── includes
│   ├── footer.ejs
│   ├── header.ejs
│   ├── nav.ejs
│   ├── sidebar.ejs
│   └── toc.ejs
└── layouts
├── base.ejs
├── content.ejs
└── page.ejs
```

按照文件的路径,您可以覆盖默认的模板文件。

如果自定义模版,可以直接在 `templates/layouts` 文件夹中创建自定义的布局文件,文件名便是模版名称。
71 changes: 71 additions & 0 deletions docs/content/docs/publish-site.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
---
title: 发布您的网站
---

在 git 存储库中托管项目文档的好处是能够在推送新更改时自动部署它。PageForge 将帮助您让这一切变得非常简单。

## GitHub Pages

---

如果您已经在 GitHub 上托管了您的代码,GitHub Pages 无疑是发布项目文档的最便捷方式。它是免费的,而且很容易设置。

### 使用 GitHub Actions

使用 GitHub Actions,您可以自动部署项目文档。在仓库的根目录中,创建一个新的 GitHub Actions 工作流程,例如 `.github/workflows/ci.yml`,然后复制并粘贴以下内容:

```yaml
name: ci
on:
push:
branches:
- master
- main
permissions:
contents: write
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- name: Use Node.js 18.x
uses: actions/setup-node@v3
with:
node-version: 18.x

- name: Install pnpm
uses: pnpm/action-setup@v2
with:
version: 8

- name: Install dependencies
run: pnpm install

- name: Build site
run: |
pageforge build
- name: Deploy to GitHub Pages
uses: peaceiris/actions-gh-pages@v3
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
publish_dir: ./dist
```
### 使用 PageForge
---
您也可以使用 PageForge 的命令行工具来自动部署项目文档。PageForge 会在项目根目录中 `pageforge.yaml` 文件,读取配置信息:

```yaml
deploy:
github:
repository: devlive-community/pageforge
branch: gh-pages
cname: pageforge.devlive.community
buildDir: dist
username: devlive-community
email: support@devlive.org
```
11 changes: 11 additions & 0 deletions docs/content/setup/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
title: 设置
---

PageForge 材料提供了多种自定义文档的选项。在本节中,我们将解释如何为您的网站创建有意义的结构、更改外观和感觉、添加博客和评论系统以及构建高度优化的网站。

## 网站结构

---

通过根据您的喜好配置页眉和页脚、在多种导航模式中进行选择、设置站点搜索等来设置和自定义文档的结构。
21 changes: 5 additions & 16 deletions docs/pageforge.yaml
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
source_path: content
output_path: dist
template_path: templates # 可选,不创建也能工作
assets_path: assets

site:
title:
description: PageForge 是一款现代化的静态页面生成与部署平台,旨在帮助用户快速创建精美的静态网站,并一键部署到 GitHub Pages。 无论是个人博客、项目文档还是企业官网,PageForge 都能让你轻松实现高效构建、智能部署和即时上线。
Expand All @@ -22,14 +17,8 @@ repo:
url: https://github.com/devlive-community/pageforge

nav:
- text: 首页
link: /
- text: 文档
link: /docs/index

sidebar:
default:
- text: 快速上手
link: /docs/get-started
- text: 创建你的网站
link: /docs/create-site/index
- 快速开始:
- /docs/get-started
- /docs/create-site/index
- /docs/publish-site
- /docs/customization
96 changes: 90 additions & 6 deletions lib/site-generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,7 @@ class SiteGenerator {

this.config.templatePath = path.join(path.join(__dirname, '../templates'));
this.assetBundler = new AssetBundler(this.config);

this.defaultNav = [
{url: '/', text: '首页'},
{url: '/about', text: '关于'}
];
this.pages = new Map();
}

// 重新加载配置
Expand Down Expand Up @@ -68,6 +64,86 @@ class SiteGenerator {
: path.resolve(process.cwd(), relativePath);
}

async preloadPages(sourceDir, baseDir = '') {
const files = fs.readdirSync(sourceDir);

for (const file of files) {
const sourcePath = path.join(sourceDir, file);
const relativePath = path.join(baseDir, file);
const stat = fs.statSync(sourcePath);

if (stat.isDirectory()) {
// 递归处理子目录
await this.preloadPages(sourcePath, relativePath);
}
else if (file.endsWith('.md')) {
// 处理 Markdown 文件
const {metadata} = this.fileProcessor.processMarkdown(sourcePath);
const pageUrlPath = this.getPagePath(relativePath);
this.pages.set(pageUrlPath, metadata);
}
}
}

appendHtml(path) {
// 如果是字符串,替换 .md 为 .html
if (typeof path === 'string') {
return path.endsWith('.md')
? path.replace('.md', '.html') // 如果以 .md 结尾,替换为 .html
: path.endsWith('.html') // 如果已经是 .html 结尾,保持不变
? path
: `${path}.html`; // 其他情况,添加 .html
}

// 如果是数组,处理第一个元素
if (Array.isArray(path)) {
return this.appendHtml(path[0]);
}

// 如果是对象,处理第一个值
if (typeof path === 'object' && path !== null) {
const firstKey = Object.keys(path)[0];
return this.appendHtml(path[firstKey]);
}

return path;
}

transformNavigation(navigation) {
const processItem = (item) => {
// 如果是字符串类型的链接(如 "/docs/get-started")
if (typeof item === 'string') {
// 直接使用原始 URL 作为 href
const pageData = this.pages.get(item);
if (pageData) {
return {
title: pageData.title,
href: this.appendHtml(item)
};
}
// 如果在 pages 中找不到,使用路径最后一段作为标题
return {
title: item.split('/').pop(),
href: this.appendHtml(item.replace(/\.(md|html)$/, ''))
};
}

// 如果是对象(有子项的导航)
if (typeof item === 'object') {
const [[title, items]] = Object.entries(item);
return {
title,
href: this.appendHtml(items[0]),
items: Array.isArray(items) ? items.map(processItem) : []
};
}

return item;
};

return navigation.map(processItem);
}

// 递归处理目录
async processDirectory(sourceDir, baseDir = '') {
const files = fs.readdirSync(sourceDir);
Expand All @@ -87,6 +163,9 @@ class SiteGenerator {
// 处理 Markdown 文件
const {metadata, content} = this.fileProcessor.processMarkdown(sourcePath);

const pageUrlPath = this.getPagePath(relativePath);
this.pages.set(pageUrlPath, metadata);

// 构建输出路径,保持目录结构
const outputDir = path.join(this.config.outputPath, baseDir);
if (!fs.existsSync(outputDir)) {
Expand All @@ -104,7 +183,7 @@ class SiteGenerator {
page: {
...metadata,
// 添加路径信息
path: pagePath,
path: this.appendHtml(pagePath),
// 原始文件路径
sourcePath: relativePath,
// 输出路径
Expand All @@ -119,6 +198,8 @@ class SiteGenerator {
...metadata,
...this.config
};
pageData.nav = this.transformNavigation(pageData.nav);
// console.log(JSON.stringify(pageData.nav))

// 渲染页面
const html = await this.templateEngine.renderWithLayout('layouts/page', pageData);
Expand Down Expand Up @@ -218,6 +299,9 @@ class SiteGenerator {
fs.mkdirSync(this.config.outputPath, {recursive: true});
}

// 预加载所有页面数据
await this.preloadPages(this.config.sourcePath);

// 先处理资源打包和复制
await this.copyAssets();

Expand Down
Loading

0 comments on commit d6e5432

Please sign in to comment.