Skip to content

Commit

Permalink
Fix internal links and images replace.
Browse files Browse the repository at this point in the history
  • Loading branch information
huacnlee committed Nov 13, 2023
1 parent 0bc9b5d commit e3bafb2
Show file tree
Hide file tree
Showing 7 changed files with 172 additions and 94 deletions.
4 changes: 2 additions & 2 deletions feishu-pages/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"@types/node": "^20.5.7",
"axios": "^1.5.0",
"dotenv": "^16.3.1",
"feishu-docx": "0.5.2",
"feishu-docx": "0.5.4",
"mime-types": "^2.1.35",
"typescript": "^5.2.2"
},
Expand All @@ -33,4 +33,4 @@
"jest": "^29.6.4",
"ts-jest": "^29.1.1"
}
}
}
13 changes: 8 additions & 5 deletions feishu-pages/src/doc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,16 +82,19 @@ export const generateFileMeta = (
sidebar_position: position,
};

// Replace double quote to avoid YAML parse error
meta.title = meta.title.replace(/"/g, '\\"');

let output = `---\n`;
for (const key in meta) {
const val = meta[key];
let val = meta[key];
if (val === null || val === undefined) {
continue;
}
output += `${key}: "${val}"\n`;

// Replace double quote to avoid YAML parse error
if (typeof val === 'string') {
val = val.replace(/"/g, '\\"');
val = `"${val}"`;
}
output += `${key}: ${val}\n`;
}
output += `---\n`;

Expand Down
18 changes: 12 additions & 6 deletions feishu-pages/src/feishu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,9 @@ export const feishuDownload = async (fileToken: string, localPath: string) => {
fs.mkdirSync(CACHE_DIR, { recursive: true });

let res: any = {};
let hasCache = false;
if (fs.existsSync(cacheFilePath) && fs.existsSync(cacheFileMetaPath)) {
hasCache = true;
res.data = fs.readFileSync(cacheFilePath);
res.headers = JSON.parse(fs.readFileSync(cacheFileMetaPath, 'utf-8'));
console.info(' -> Cache hit:', fileToken);
Expand Down Expand Up @@ -261,18 +263,22 @@ export const feishuDownload = async (fileToken: string, localPath: string) => {

if (res.data) {
let extension = mime.extension(res.headers['content-type']);
console.info(
' =>',
res.headers['content-type'],
humanizeFileSize(res.data.length)
);
if (!hasCache) {
console.info(
' =>',
res.headers['content-type'],
humanizeFileSize(res.data.length)
);
}

if (extension) {
localPath = localPath + '.' + extension;
}
const dir = path.dirname(localPath);
fs.mkdirSync(dir, { recursive: true });
console.info(' -> Writing file:', localPath);
if (!hasCache) {
console.info(' -> Writing file:', localPath);
}
fs.writeFileSync(localPath, res.data);
}

Expand Down
21 changes: 13 additions & 8 deletions feishu-pages/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
fetchTenantAccessToken,
} from './feishu';
import { FileDoc, generateSummary, prepareDocSlugs } from './summary';
import { cleanupDocsForJSON, humanizeFileSize } from './utils';
import { cleanupDocsForJSON, humanizeFileSize, replaceLinks } from './utils';
import { fetchAllDocs } from './wiki';

// App entry
Expand Down Expand Up @@ -88,12 +88,15 @@ const fetchDocAndWriteFile = async (

let { content, fileTokens } = doc;

// TODO: Replace link's node_token into slug
// Replace node_token to slug
for (const node_token in slugMap) {
// Replace Markdown link and HTML link
const re = new RegExp(`${node_token}`, 'gm');
const newLink = `${BASE_URL}${slugMap[node_token]}`;
content = content.replace(re, newLink);
if (slugMap[node_token]) {
content = replaceLinks(
content,
node_token,
`${BASE_URL}${slugMap[node_token]}`
);
}
}

const metaInfo = generateFileMeta(doc, doc.slug, doc.position);
Expand Down Expand Up @@ -141,8 +144,10 @@ const downloadFiles = async (

const extension = path.extname(filePath);

const re = new RegExp(`${fileToken}`, 'gm');
content = content.replace(re, './assets/' + fileToken + extension);
let assetURL = `./assets/${fileToken}${extension}`;

// Replase Markdown image
content = replaceLinks(content, fileToken, assetURL);
}

return content;
Expand Down
54 changes: 39 additions & 15 deletions feishu-pages/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { FileDoc } from "./summary";
import { FileDoc } from './summary';

export const normalizeSlug = (slug: string | number) => {
// force convert slug into string
Expand Down Expand Up @@ -37,19 +37,19 @@ export const humanizeFileSize = (bytes, dp = 1) => {
};

const allowKeys = [
"depth",
"title",
"slug",
"filename",
"node_token",
"parent_node_token",
"children",
"obj_create_time",
"obj_edit_time",
"obj_token",
"has_child",
"meta",
"position"
'depth',
'title',
'slug',
'filename',
'node_token',
'parent_node_token',
'children',
'obj_create_time',
'obj_edit_time',
'obj_token',
'has_child',
'meta',
'position',
];

export function cleanupDocsForJSON(docs: FileDoc[]) {
Expand All @@ -58,7 +58,7 @@ export function cleanupDocsForJSON(docs: FileDoc[]) {
for (let idx = 0; idx < docs.length; idx++) {
const doc = docs[idx];

Object.keys(doc).forEach(key => {
Object.keys(doc).forEach((key) => {
if (!allowKeys.includes(key)) {
delete doc[key];
}
Expand All @@ -78,3 +78,27 @@ export function cleanupDocsForJSON(docs: FileDoc[]) {
docs.splice(nodesToDelete[i], 1);
}
}

export function replaceLinks(
content: string,
node_token: string,
newLink?: string
): string {
if (!newLink) {
return content;
}

/*
match all
1 - ]( or src=" or href="
2 - https://ywh1bkansf.feishu.cn/wiki/aabbdd
3 - node_token
4 - ) or "
*/
const re = new RegExp(
`(]\\(|src="|href=")(http[s]?:\\\/\\\/[\\w]+\\.(feishu\\.cn|larksuite\.com)\\\/.*)?(${node_token}.*)(\\)|")`,
'gm'
);
return content.replace(re, `$1${newLink}$5`);
}
8 changes: 4 additions & 4 deletions feishu-pages/tests/doc.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ import { generateFileMeta } from '../src/doc';
describe('Doc', () => {
test('generateFileMeta', () => {
let doc: any = {
title: 'hello world',
title: 'Docs: "hello world"',
};

const urlPath = '/hello/world';
const position = 1;

let expected = `---
title: hello world
slug: /hello/world
title: "Docs: \\"hello world\\""
slug: "/hello/world"
sidebar_position: 1
---
`;
Expand All @@ -24,7 +24,7 @@ sidebar_position: 1
title: null,
};
expected = `---
slug: /hello/world
slug: "/hello/world"
sidebar_position: 1
---
`;
Expand Down
Loading

0 comments on commit e3bafb2

Please sign in to comment.