Skip to content

Commit

Permalink
fix: markdown generate to merge near text style.
Browse files Browse the repository at this point in the history
For fix: `**He****llo**` -> `**Hello**`
  • Loading branch information
huacnlee committed Jan 31, 2024
1 parent 689a65e commit 2eb3cdc
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 21 deletions.
35 changes: 19 additions & 16 deletions feishu-docx/src/markdown_renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ export class MarkdownRenderer extends Renderer {
const inline = block.elements.length > 1;

block.elements?.forEach((el) => {
buf.write(this.parseTextElement(el, inline));
this.parseTextElement(buf, el, inline);
});

if (buf.length > 0) {
Expand Down Expand Up @@ -306,10 +306,9 @@ export class MarkdownRenderer extends Renderer {
return buf;
}

parseTextElement(el: TextElement, inline: boolean): Buffer | string {
const buf = new Buffer();
parseTextElement(buf: Buffer, el: TextElement, inline: boolean) {
if (el.text_run) {
buf.write(this.parseTextRun(el.text_run));
this.parseTextRun(buf, el.text_run);
} else if (el.equation) {
let symbol = inline ? '$' : '$$';
buf.write(symbol);
Expand All @@ -319,36 +318,34 @@ export class MarkdownRenderer extends Renderer {
const node_token = decodeURIComponent(el.mention_doc.token);
buf.write(`[${el.mention_doc.title}](${node_token})`);
}

return buf;
}

parseTextRun(textRun: TextRun): Buffer | string {
const buf = new Buffer();
parseTextRun(buf: Buffer, textRun: TextRun) {
let preWrite = '';
let postWrite = '';

let style = textRun.text_element_style;
let escape = true;
if (style) {
if (style.bold) {
buf.write('**');
preWrite = '**';
postWrite = '**';
} else if (style.italic) {
buf.write('_');
preWrite = '_';
postWrite = '_';
} else if (style.strikethrough) {
buf.write('~~');
preWrite = '~~';
postWrite = '~~';
} else if (style.underline) {
buf.write('<u>');
preWrite = '<u>';
postWrite = '</u>';
} else if (style.inline_code) {
buf.write('`');
preWrite = '`';
postWrite = '`';
escape = false;
} else if (style.link) {
const unescapeURL = decodeURIComponent(style.link.url);
buf.write(`[`);
preWrite = `[`;
postWrite = `](${unescapeURL})`;
}
}
Expand All @@ -362,10 +359,16 @@ export class MarkdownRenderer extends Renderer {
plainText = escapeHTMLTags(plainText);
}

// If the previus style is same as current, we can merge them.
// For example:
// Last is: **He**
// Current is: **llo**
// Then we can merge them to **Hello**
if (!buf.trimLastIfEndsWith(preWrite)) {
buf.write(preWrite);
}
buf.write(plainText);
buf.write(postWrite);

return buf;
}

parseImage(image: ImageBlock): Buffer | string {
Expand Down
24 changes: 24 additions & 0 deletions feishu-docx/src/string_buffer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,30 @@ export class Buffer {
}
}

/**
* Remove last string if ends with part
* @param part
* @returns
*/
trimLastIfEndsWith(part: string): boolean {
if (part.length == 0) {
return false;
}

const lastIdx = this.buffer.length - 1;
const lastStr = this.buffer[lastIdx];
if (!lastStr) {
return false;
}
if (lastStr.endsWith(part)) {
this.buffer[lastIdx] = lastStr.slice(0, -part.length);
this.length -= part.length;
return true;
}

return false;
}

toString() {
return this.buffer.join('');
}
Expand Down
6 changes: 3 additions & 3 deletions feishu-docx/tests/fixtures/case3.expect.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ yarn add feishu-pages
</colgroup>
<thead>
<tr>
<th><p>Name</p></th><th><p>Description</p></th><th><p>Required</p></th><th><p>Default</p></th></tr>
<th><p><strong>名称</strong></p></th><th><p>Description</p></th><th><p>Required</p></th><th><p>Default</p></th></tr>
</thead>
<tbody>
<tr>
Expand All @@ -82,7 +82,7 @@ yarn add feishu-pages
### **创建飞书应用并开通权限**

1. 请访问 [https://open.feishu.cn/app](https://open.feishu.cn/app) 创建一个新应用,并获得:
- `App ``ID`
- `App ID`
- `App Secret` - 请注意保管 App Secret,不要泄露到互联网。

2. 为应用开启 `机器人` 应用能力。
Expand All @@ -91,7 +91,7 @@ yarn add feishu-pages
5. 在飞书 IM 中创建新群 `Feishu Pages`,将应用添加为该群机器人,知识库管理员在「知识空间设置」-&gt; 「权限设置」-&gt;「添加管理员」中添加,把这个 `Feishu Pages` 群加成 **管理员**
- 否则会遇到 `permission denied: wiki space permission denied` 错误。 [ref](https://open.feishu.cn/document/server-docs/docs/wiki-v2/wiki-qa)

### **获取飞书知识库 ****space_id**
### **获取飞书知识库 space_id**

我们需要配置 `FEISHU_SPACE_ID` 的环境变量,这个为飞书知识库的 `space_id`,你可以访问知识库设置界面,从 URL 中获取。

Expand Down
16 changes: 14 additions & 2 deletions feishu-docx/tests/fixtures/case3.raw.json
Original file line number Diff line number Diff line change
Expand Up @@ -1224,9 +1224,21 @@
"elements": [
{
"text_run": {
"content": "Name",
"content": "",
"text_element_style": {
"bold": false,
"bold": true,
"inline_code": false,
"italic": false,
"strikethrough": false,
"underline": false
}
}
},
{
"text_run": {
"content": "",
"text_element_style": {
"bold": true,
"inline_code": false,
"italic": false,
"strikethrough": false,
Expand Down

0 comments on commit 2eb3cdc

Please sign in to comment.