Skip to content

Commit

Permalink
Merge pull request #6 from Zuoqiu-Yingyi/dev
Browse files Browse the repository at this point in the history
chore: release v0.1.1
  • Loading branch information
Zuoqiu-Yingyi committed Sep 5, 2023
2 parents e892fa1 + 02d443e commit 7806302
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/jupyter/import.ts
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,7 @@ export class IpynbImport {
markdowns.push(markdown);
break;
case "stderr":
const lines = markdown.split("\n{2,}");
const lines = markdown.split(/\n{2,}/);
for (let j = 0; j < lines.length; ++j) {
markdowns.push(lines[j]);
markdowns.push(createIAL({ style: CONSTANTS.styles.error }));
Expand Down
26 changes: 23 additions & 3 deletions src/jupyter/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,30 @@ export function parseText(
* @param data 数据
* @param metadata 元数据
* @param options 解析选项
* @param split 解析后的块是否分割
* @returns 解析后的文本
*/
export async function parseData(
client: InstanceType<typeof Client>,
options: IJupyterParserOptions,
data: IData,
metadata?: Record<string, string>,
): Promise<string> {
split?: false,
): Promise<string>;
export async function parseData(
client: InstanceType<typeof Client>,
options: IJupyterParserOptions,
data: IData,
metadata?: Record<string, string>,
split?: true,
): Promise<string[]>;
export async function parseData(
client: InstanceType<typeof Client>,
options: IJupyterParserOptions,
data: IData,
metadata?: Record<string, string>,
split: boolean = false,
): Promise<string | string[]> {
let filedata: string;
const markdowns = new PriorityQueue<string>();
for (const [mime, datum] of Object.entries(data)) {
Expand All @@ -95,7 +111,7 @@ export async function parseData(
markdowns.enqueue(parseText(text, options), 0);
break;
case "html":
markdowns.enqueue(`<div>${text}</div>`, 1);
markdowns.enqueue(`<div>${text.replaceAll(/\n+/g, "\n")}</div>`, 1);
break;
case "markdown":
markdowns.enqueue(text, 1);
Expand Down Expand Up @@ -183,5 +199,9 @@ export async function parseData(
break;
}
}
return markdowns.items.map((item) => item.value).join("\n");

const blocks = markdowns.items.map((item) => item.value);
return split
? blocks
: blocks.join("\n\n");
}
15 changes: 9 additions & 6 deletions src/workers/jupyter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -733,19 +733,22 @@ async function handleExecuteResultMessage(
message: KernelMessage.IExecuteResultMsg,
context: IExecuteContext,
): Promise<void> {
const kramdown = await parseData(
const kramdowns = await parseData(
client,
context.output.options,
message.content.data as IData,
message.content.metadata as Record<string, string>,
true,
);

context.output.hrs.execute_result.used = true;
await client.insertBlock({
nextID: context.output.hrs.execute_result.id,
data: kramdown,
dataType: "markdown",
});
for (const kramdown of kramdowns) {
await client.insertBlock({
nextID: context.output.hrs.execute_result.id,
data: kramdown,
dataType: "markdown",
});
}
}

/**
Expand Down

0 comments on commit 7806302

Please sign in to comment.