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