Skip to content

Commit

Permalink
Add URL_STYLE env option to config the URL structure on export.
Browse files Browse the repository at this point in the history
  • Loading branch information
huacnlee committed Nov 23, 2023
1 parent 1a31cb8 commit bd4ea6a
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 12 deletions.
21 changes: 11 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,16 +75,17 @@ yarn add feishu-pages

> 如果你想简单一些,也可以用 `.env` 文件来配置环境变量,注意避免 `FEISHU_APP_SECRET` 泄露到互联网。
| Name | Description | Required | Default |
| ------------------- | ------------------------------------------------------------------------------------------------------------ | -------- | ---------------------- |
| `FEISHU_ENDPOINT` | 飞书 API 节点,如用 LarkSuite 可以通过这个配置 API 地址 | NO | https://open.feishu.cn |
| `FEISHU_APP_ID` | 飞书应用 ID | YES | |
| `FEISHU_APP_SECRET` | 飞书应用 Secret | YES | |
| `FEISHU_SPACE_ID` | 飞书知识库 ID | YES | |
| `OUTPUT_DIR` | 输出目录 | NO | `./dist` |
| `ROOT_NODE_TOKEN` | 根节点,导出节点以下(不含此节点)的所有内容。 | NO | |
| `BASE_URL` | 自定义文档里面相关文档输出的 URL 前缀,例如:`/docs/`,默认为 `/`,建议采用完整 URL 避免相对路径的各类问题。 | NO | `/` |
| `ROOT_NODE_TOKEN` | 从哪个节点 (node_token) 开始导出,例如:`6992046856314306562`,默认为空,走根节点开始。 | NO | |
| Name | Description | Required | Default |
| ------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------- | -------- | ---------------------- |
| `FEISHU_ENDPOINT` | 飞书 API 节点,如用 LarkSuite 可以通过这个配置 API 地址 | NO | https://open.feishu.cn |
| `FEISHU_APP_ID` | 飞书应用 ID | YES | |
| `FEISHU_APP_SECRET` | 飞书应用 Secret | YES | |
| `FEISHU_SPACE_ID` | 飞书知识库 ID | YES | |
| `OUTPUT_DIR` | 输出目录 | NO | `./dist` |
| `ROOT_NODE_TOKEN` | 根节点,导出节点以下(不含此节点)的所有内容。 | NO | |
| `BASE_URL` | 自定义文档里面相关文档输出的 URL 前缀,例如:`/docs/`,默认为 `/`,建议采用完整 URL 避免相对路径的各类问题。 | NO | `/` |
| `ROOT_NODE_TOKEN` | 从哪个节点 (node_token) 开始导出,例如:`6992046856314306562`,默认为空,走根节点开始。 | NO | |
| `URL_STYLE` | 导出的文档 URL 风格。<br/><br/>- `nested` - 采用层级的 URL 结构,如 `/foo/bar/dar`。<br/>- `original` - 采用一层的 URL 结构,如 `/X80QwaYvjiMWZrk399YcK4q8nCc`| NO | `relative` |

## Usage

Expand Down
8 changes: 8 additions & 0 deletions feishu-pages/src/feishu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@ export const TMP_DIR = path.resolve(
process.env.TMP_DIR || path.join(OUTPUT_DIR, '.tmp')
);

/**
* "original" | "nested", default: "nested"
*
* raw: /G5JwdLWUkopngoxfQtIc7EFSnIg
* nested: /slug1/slug2/slug3
*/
export const URL_STYLE = process.env.URL_STYLE || 'nested';

const feishuConfig = {
endpoint: process.env.FEISHU_ENDPOINT || 'https://open.feishu.cn',
/**
Expand Down
1 change: 1 addition & 0 deletions feishu-pages/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ const fetchDocAndWriteFile = async (
let content = fs.readFileSync(cotnent_file, 'utf-8');

// Replace node_token to slug

for (const node_token in slugMap) {
if (slugMap[node_token]) {
content = replaceLinks(
Expand Down
10 changes: 8 additions & 2 deletions feishu-pages/src/summary.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import path from 'path';
import { Doc } from './feishu';
import { Doc, URL_STYLE } from './feishu';
import { normalizeSlug } from './utils';

export interface FileDoc extends Doc {
Expand All @@ -26,7 +26,13 @@ export const prepareDocSlugs = (
for (let i = 0; i < docs.length; i++) {
const doc = docs[i];
const fileKey = normalizeSlug(doc.meta?.slug || doc.node_token);
const fileSlug = path.join(parentSlug, fileKey);
let fileSlug = path.join(parentSlug, fileKey);

// Use Feishu original URL style, use node_token as URL slug.
// https://your-host.com/Rd52wbrZ1ifWmXkEUQpcXnf4ntT
if (URL_STYLE === 'original') {
fileSlug = doc.node_token;
}

doc.slug = fileSlug;
doc.position = i;
Expand Down

0 comments on commit bd4ea6a

Please sign in to comment.