forked from raycast/script-commands
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclipboard-to-markdown.js
executable file
·101 lines (80 loc) · 2.25 KB
/
clipboard-to-markdown.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#!/usr/bin/env node
// Dependency: This script requires Nodejs.
// Install Node: https://nodejs.org/en/download/
// Required parameters:
// @raycast.schemaVersion 1
// @raycast.title Clipboard to Markdown
// @raycast.mode silent
// Optional parameters:
// @raycast.icon 📋
// @raycast.packageName Conversions
// Documentation:
// @raycast.description Automatically take the content found in the clipboard and turn it into Markdown
// @raycast.author Alessandra Pereyra
// @raycast.authorURL https://github.com/alessandrapereyra
// Based on the code from
// https://github.com/raycast/script-commands/blob/master/commands/conversions/create-markdown-table.js
const child_process = require("child_process");
function pbpaste() {
return child_process.execSync("pbpaste").toString();
}
function pbcopy(data) {
return new Promise(function (resolve, reject) {
const child = child_process.spawn("pbcopy");
child.on("error", function (err) {
reject(err);
});
child.on("close", function (err) {
resolve(data);
});
child.stdin.write(data);
child.stdin.end();
});
}
function processLine(content) {
if (
content.startsWith("* ") ||
content.startsWith("- ") ||
content.startsWith("+ ")
) {
return "* " + processContent(content.substring(2));
}
return processContent(content);
}
function processLines(content) {
const lines = content.split("\n");
const markdownLines = lines.map((line) => {
return processLine(line);
});
return markdownLines.join("\n");
}
function processImageURL(content) {
return `![Image](${content})`;
}
function processURL(content) {
return `[${content}](${content})`;
}
function processText(content) {
return content;
}
function processContent(content) {
if (content.startsWith("http")) {
if (content.match(/\.(jpeg|jpg|gif|png|bmp|tiff)$/) != null) {
return processImageURL(content);
} else {
return processURL(content);
}
} else {
return processText(content);
}
}
function processStoredContent(content) {
if (content.includes("\n")) {
return processLines(content);
} else {
return processContent(content);
}
}
const storedClipboardContent = pbpaste();
let markdown = processStoredContent(storedClipboardContent);
pbcopy(markdown);