chatgpt-to-markdown 1.0.0 → 1.1.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 +5 -2
- package/index.js +28 -3
- package/package.json +1 -1
package/README.md
CHANGED
@@ -65,13 +65,15 @@ const json = [
|
|
65
65
|
];
|
66
66
|
|
67
67
|
const sourceDir = "./output";
|
68
|
-
|
68
|
+
const options = {
|
69
|
+
dateFormat: (d) => d.toLocaleString(),
|
70
|
+
};
|
69
71
|
chatgptToMarkdown(json, sourceDir);
|
70
72
|
```
|
71
73
|
|
72
74
|
## Options
|
73
75
|
|
74
|
-
|
76
|
+
- `dateFormat`: A function that takes a `Date` object and returns a string. Defaults to a string format like "1 Jan 2023 11:30 PM".
|
75
77
|
|
76
78
|
## Contributing
|
77
79
|
|
@@ -95,6 +97,7 @@ git push --follow-tags
|
|
95
97
|
|
96
98
|
## Release notes
|
97
99
|
|
100
|
+
- 1.0.1: 26 Sep 2023.
|
98
101
|
- 1.0.0: 26 Sep 2023. Initial release
|
99
102
|
|
100
103
|
## License
|
package/index.js
CHANGED
@@ -37,12 +37,37 @@ 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
|
+
}
|
57
|
+
|
40
58
|
/**
|
41
59
|
* Converts a JSON object to markdown and saves it to a file.
|
42
60
|
* @param {Object[]} json - The JSON object to convert.
|
43
61
|
* @param {string} sourceDir - The directory to save the markdown files in.
|
62
|
+
* @param {Object} [options] - The options object.
|
63
|
+
* @param {Function} [options.dateFormat] - The function to format dates with.
|
64
|
+
* @returns {Promise<void>} - A promise that resolves when the file is saved.
|
65
|
+
* @example
|
66
|
+
* const json = [ ... ];
|
67
|
+
* await convertToMarkdown(json, "./output");
|
68
|
+
* //=> Creates a markdown file for each conversation in the output directory
|
44
69
|
*/
|
45
|
-
async function chatgptToMarkdown(json, sourceDir) {
|
70
|
+
async function chatgptToMarkdown(json, sourceDir, { dateFormat } = { dateFormat: formatDate }) {
|
46
71
|
if (!Array.isArray(json)) {
|
47
72
|
throw new TypeError("The first argument must be an array.");
|
48
73
|
}
|
@@ -56,8 +81,8 @@ async function chatgptToMarkdown(json, sourceDir) {
|
|
56
81
|
const filePath = path.join(sourceDir, fileName);
|
57
82
|
const title = `# ${wrapHtmlTagsInBackticks(conversation.title)}\n`;
|
58
83
|
const metadata = [
|
59
|
-
`- Created: ${new Date(conversation.create_time * 1000)
|
60
|
-
`- Updated: ${new Date(conversation.update_time * 1000)
|
84
|
+
`- Created: ${dateFormat(new Date(conversation.create_time * 1000))}\n`,
|
85
|
+
`- Updated: ${dateFormat(new Date(conversation.update_time * 1000))}\n`,
|
61
86
|
].join("");
|
62
87
|
const messages = Object.values(conversation.mapping)
|
63
88
|
.map((node) => {
|