Skip to content
This repository has been archived by the owner on Jun 17, 2023. It is now read-only.

Commit

Permalink
feat: get a list of ref for markdown
Browse files Browse the repository at this point in the history
  • Loading branch information
gantrol committed Apr 3, 2023
1 parent 1d9811a commit 15cf143
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 12 deletions.
10 changes: 5 additions & 5 deletions test/parser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,23 +58,23 @@ const qasp = new QasJSON2MarkdownParser(simpleDemoJSON);

describe("test simple json", () => {
it("answer", () => {
expect(qasp.answer(simpleDemoJSON[0].answers[0])).toBe("Bing: Hello");
expect(qasp.answer(simpleDemoJSON[0].answers[0])).toBe("Hello");
});
it("answers", () => {
expect(qasp.answers(simpleDemoJSON[0].answers)).toBe("Bing: Hello\n\nBing: Hello");
expect(qasp.answers(simpleDemoJSON[0].answers)).toBe("Hello\n\nHello");
expect(qasp.answers(undefined)).toBe("");
});
it("questions", () => {
expect(qasp.questions(
simpleDemoJSON[simpleDemoJSON.length - 1].questions))
.toBe("Q: Goodbye\n\nQ: Bye");
.toBe("> Goodbye\n\n> Bye");
expect(qasp.questions(undefined)).toBe("");
});
it("turn", () => {
expect(qasp.turn(simpleDemoJSON[1]))
.toBe(
"Q: What date is it today?\n\n" +
"Bing: Hello, this is Bing. Today is Wednesday, February 15, 2023.");
"> What date is it today?\n\n" +
"Hello, this is Bing. Today is Wednesday, February 15, 2023.");
});
it("turns", () => {
// TODO: read a file to match
Expand Down
2 changes: 0 additions & 2 deletions todo.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
- [ ] record & content: copy text, copy code
- [ ] refract: components the chat
- [ ] add `github repo` to link? or footer?
- [ ] 尝试往导出加一个微软图标
- [ ] add to zip when download multiply
- [ ] welcome page cancel hidden the "对话样式",或者添加子选项?

## TODO
Expand Down
21 changes: 16 additions & 5 deletions utils/md/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ import TurndownService from "turndown";
import { genTitle } from "~utils/filename";


/**
* MarkdownParser for Question and answer JSON
*/
export class QasJSON2MarkdownParser {
private qas: any;
private toMD: TurndownService;
Expand Down Expand Up @@ -41,7 +44,6 @@ export class QasJSON2MarkdownParser {
// `## ${i + 1}${this.sep}${turn_md_list[i]}`
// );
// }
console.log(sections);
let partOfFirstQuestion = "";
if (sections.length > 0 && sections[0] && sections[0].length > this.quesPrefix.length) {
partOfFirstQuestion = sections[0].substring(this.quesPrefix.length).trim();
Expand Down Expand Up @@ -72,8 +74,6 @@ export class QasJSON2MarkdownParser {
} else {
return result;
}
// TODO: get a list of ref?
// Add to discuss
}

answers(objs) {
Expand All @@ -86,14 +86,25 @@ export class QasJSON2MarkdownParser {
}

// {"body": "Hello", "html": "<p>Hello</p>"} => Hello
// {"body": "Hello", "html": "<p>Hello</p>", "ref": [...]} => Hello
answer(obj) {
return `${this.ansPrefix}${this.toMD.turndown(obj.html)}`;
const body = `${this.ansPrefix}${this.toMD.turndown(obj.html)}`;
const refs = obj.refs;
if (refs && refs.length > 0) {
const refs_md = refs.map(ref => {
return `1. [${ref.title}](${ref.href})`;
}).join("\n");
const refsText = refs_md;
return `${body}${this.sep}${refsText}`
} else {
return body;
}
}

questions(objs) {
if (objs) {
const question_list = objs.map(obj => {
return `${obj.text.split('\n').map(line => `${this.quesPrefix} ${line}`).join('\n')}`;
return `${obj.text.split('\n').map(line => `${this.quesPrefix}${line}`).join('\n')}`;
});
return question_list.join(this.sep);
} else {
Expand Down

0 comments on commit 15cf143

Please sign in to comment.