chatgpt-to-markdown 1.3.0 → 1.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -97,6 +97,7 @@ git push --follow-tags
97
97
 
98
98
  ## Release notes
99
99
 
100
+ - 1.4.0: 29 Oct 2023. Handle multi-modal text from Dall-E
100
101
  - 1.3.0: 29 Sep 2023. Set create and update dates from chat
101
102
  - 1.2.0: 28 Sep 2023. Added test cases
102
103
  - 1.1.0: 26 Sep 2023. Add date format option
package/index.js CHANGED
@@ -37,23 +37,15 @@ function indent(str) {
37
37
  .join("\n");
38
38
  }
39
39
 
40
- /**
41
- * Formats a date to a string like "1 Jan 2021".
42
- * @param {Date} date - The date to format.
43
- * @returns {string} - The formatted date.
44
- * @example
45
- * formatDate(new Date());
46
- * //=> "1 Jan 2021"
47
- */
48
- function formatDate(date) {
49
- const month = date.toLocaleString("en", { month: "short" });
50
- const min = date.getMinutes().toString().padStart(2, "0");
51
- let hours = date.getHours();
52
- let ampm = hours >= 12 ? "PM" : "AM";
53
- hours = hours % 12;
54
- hours = hours ? hours : 12; // the hour '0' should be '12'
55
- return `${date.getDate()} ${month} ${date.getFullYear()} ${hours}:${min} ${ampm}`;
56
- }
40
+ const dateFormat = new Intl.DateTimeFormat("en-US", {
41
+ day: "numeric",
42
+ month: "short",
43
+ year: "numeric",
44
+ hour: "numeric",
45
+ minute: "2-digit",
46
+ hour12: true,
47
+ });
48
+ export const formatDate = (date) => dateFormat.format(date);
57
49
 
58
50
  /**
59
51
  * Converts a JSON object to markdown and saves it to a file.
@@ -96,6 +88,14 @@ async function chatgptToMarkdown(json, sourceDir, { dateFormat } = { dateFormat:
96
88
  ? "```" + content.language.replace("unknown", "") + "\n" + content.text + "\n```"
97
89
  : content.content_type == "execution_output"
98
90
  ? "```\n" + content.text + "\n```"
91
+ : content.content_type == "multimodal_text"
92
+ ? content.parts
93
+ .map((part) =>
94
+ part.content_type === "image_asset_pointer"
95
+ ? `Image (${part.width}x${part.height}): ${part?.metadata?.dalle?.prompt}\n\n`
96
+ : `${part.content_type}\n\n`,
97
+ )
98
+ .join("")
99
99
  : content;
100
100
  // Ignore empty content
101
101
  if (!body.trim()) return "";
package/index.test.js CHANGED
@@ -3,7 +3,7 @@
3
3
  import { promises as fs } from "fs";
4
4
  import path from "path";
5
5
  import os from "os";
6
- import chatgptToMarkdown from "./index";
6
+ import { default as chatgptToMarkdown, formatDate } from "./index";
7
7
 
8
8
  describe("chatgptToMarkdown", () => {
9
9
  let tempDir;
@@ -40,8 +40,8 @@ describe("chatgptToMarkdown", () => {
40
40
 
41
41
  expect(fileContent).toBe(`# Test Conversation
42
42
 
43
- - Created: 1 Sep 2021 5:30 AM
44
- - Updated: 1 Sep 2021 6:30 AM
43
+ - Created: ${formatDate(1630454400 * 1000)}
44
+ - Updated: ${formatDate(1630458000 * 1000)}
45
45
 
46
46
  ## user (John)
47
47
 
@@ -129,6 +129,40 @@ describe("chatgptToMarkdown", () => {
129
129
  expect(fileContent).not.toContain("## user (John)");
130
130
  });
131
131
 
132
+ it("should handle multimodal_text content", async () => {
133
+ const json = [
134
+ {
135
+ title: "Test Conversation",
136
+ create_time: 1630454400,
137
+ update_time: 1630458000,
138
+ mapping: {
139
+ 0: {
140
+ message: {
141
+ author: { role: "tool", name: "dalle.text2im" },
142
+ content: {
143
+ content_type: "multimodal_text",
144
+ parts: [
145
+ {
146
+ content_type: "image_asset_pointer",
147
+ width: 1024,
148
+ height: 1024,
149
+ metadata: { dalle: { prompt: "Photo" } },
150
+ },
151
+ { content_type: "image_asset_pointer", width: 1024, height: 1024 },
152
+ { content_type: "some_other_type" },
153
+ ],
154
+ },
155
+ },
156
+ },
157
+ },
158
+ },
159
+ ];
160
+ await chatgptToMarkdown(json, tempDir);
161
+ const fileContent = await fs.readFile(path.join(tempDir, "Test Conversation.md"), "utf8");
162
+ expect(fileContent).toContain("Image (1024x1024): Photo\n");
163
+ expect(fileContent).toContain("some_other_type\n");
164
+ });
165
+
132
166
  it("should indent messages with tool role that contain ``` fenced code blocks", async () => {
133
167
  const json = [
134
168
  {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "chatgpt-to-markdown",
3
- "version": "1.3.0",
3
+ "version": "1.4.0",
4
4
  "description": "Convert ChatGPT exported conversations.json to Markdown",
5
5
  "main": "index.js",
6
6
  "type": "module",