chatgpt-to-markdown 1.3.0 → 1.5.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,8 @@ git push --follow-tags
97
97
 
98
98
  ## Release notes
99
99
 
100
+ - 1.5.0: 28 Nov 2023. Handle `tether_browsing_display`, `tether_quote` and `system_error`
101
+ - 1.4.0: 29 Oct 2023. Handle multi-modal text from Dall-E
100
102
  - 1.3.0: 29 Sep 2023. Set create and update dates from chat
101
103
  - 1.2.0: 28 Sep 2023. Added test cases
102
104
  - 1.1.0: 26 Sep 2023. Add date format option
package/index.js CHANGED
@@ -33,27 +33,19 @@ function wrapHtmlTagsInBackticks(text) {
33
33
  function indent(str) {
34
34
  return str
35
35
  .split("\n")
36
- .map((v) => ` ${v}`)
37
- .join("\n");
36
+ .map((v) => ` ${v}\n`)
37
+ .join("");
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,20 @@ 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
+ : content.content_type == "tether_browsing_display"
100
+ ? "```\n" + content.result + "\n```"
101
+ : content.content_type == "tether_quote"
102
+ ? "```\n" + `${content.title} (${content.url})\n\n${content.text}` + "\n```"
103
+ : content.content_type == "system_error"
104
+ ? `${content.name}\n\n${content.text}\n\n`
99
105
  : content;
100
106
  // Ignore empty content
101
107
  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,13 +40,14 @@ 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
 
48
48
  Hello
49
49
 
50
+
50
51
  `);
51
52
  });
52
53
 
@@ -129,6 +130,82 @@ describe("chatgptToMarkdown", () => {
129
130
  expect(fileContent).not.toContain("## user (John)");
130
131
  });
131
132
 
133
+ it("should handle tether_browsing_display content", async () => {
134
+ const json = [
135
+ {
136
+ title: "Test Conversation",
137
+ create_time: 1630454400,
138
+ update_time: 1630458000,
139
+ mapping: {
140
+ 0: {
141
+ message: {
142
+ author: { role: "tool", name: "browser" },
143
+ content: { content_type: "tether_browsing_display", result: "L0: x" },
144
+ },
145
+ },
146
+ },
147
+ },
148
+ ];
149
+ await chatgptToMarkdown(json, tempDir);
150
+ const fileContent = await fs.readFile(path.join(tempDir, "Test Conversation.md"), "utf8");
151
+ expect(fileContent).toContain("```\nL0: x\n```");
152
+ });
153
+
154
+ it("should handle tether_quote content", async () => {
155
+ const json = [
156
+ {
157
+ title: "Test Conversation",
158
+ create_time: 1630454400,
159
+ update_time: 1630458000,
160
+ mapping: {
161
+ 0: {
162
+ message: {
163
+ author: { role: "tool", name: "browser" },
164
+ content: { content_type: "tether_quote", url: "x.com", domain: "x.com", title: "T", text: "X" },
165
+ },
166
+ },
167
+ },
168
+ },
169
+ ];
170
+ await chatgptToMarkdown(json, tempDir);
171
+ const fileContent = await fs.readFile(path.join(tempDir, "Test Conversation.md"), "utf8");
172
+ expect(fileContent).toContain("```\nT (x.com)\n\nX\n```");
173
+ });
174
+
175
+ it("should handle multimodal_text content", async () => {
176
+ const json = [
177
+ {
178
+ title: "Test Conversation",
179
+ create_time: 1630454400,
180
+ update_time: 1630458000,
181
+ mapping: {
182
+ 0: {
183
+ message: {
184
+ author: { role: "tool", name: "dalle.text2im" },
185
+ content: {
186
+ content_type: "multimodal_text",
187
+ parts: [
188
+ {
189
+ content_type: "image_asset_pointer",
190
+ width: 1024,
191
+ height: 1024,
192
+ metadata: { dalle: { prompt: "Photo" } },
193
+ },
194
+ { content_type: "image_asset_pointer", width: 1024, height: 1024 },
195
+ { content_type: "some_other_type" },
196
+ ],
197
+ },
198
+ },
199
+ },
200
+ },
201
+ },
202
+ ];
203
+ await chatgptToMarkdown(json, tempDir);
204
+ const fileContent = await fs.readFile(path.join(tempDir, "Test Conversation.md"), "utf8");
205
+ expect(fileContent).toContain("Image (1024x1024): Photo\n");
206
+ expect(fileContent).toContain("some_other_type\n");
207
+ });
208
+
132
209
  it("should indent messages with tool role that contain ``` fenced code blocks", async () => {
133
210
  const json = [
134
211
  {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "chatgpt-to-markdown",
3
- "version": "1.3.0",
3
+ "version": "1.5.0",
4
4
  "description": "Convert ChatGPT exported conversations.json to Markdown",
5
5
  "main": "index.js",
6
6
  "type": "module",