Dropbox Shared Link to MD Clickable Image BD39

Dropbox to MD BD39

DROPBOX SHARED LINK:

<https://www.dropbox.com/scl/fi/dvaxrii8zfuw6qpq99mwa/2025-12-14-17.29.44.jpg?rlkey=9qgxwnzzbbvmju447ut1mcc19&st=8vjessxn&dl=0>  

MARKDOWN LINK:

[![2025-12-14-17.29.44.jpg][202512141730]][202512141730]  

[202512141730]: https://dl.dropboxusercontent.com/scl/fi/dvaxrii8zfuw6qpq99mwa/2025-12-14-17.29.44.jpg?rlkey=9qgxwnzzbbvmju447ut1mcc19  
// Dropbox to MD BD39  
// For all files: [![filename][id]][id]\n[id]: {directUrl}  
// This embeds images (clickable to full) and provides preview/link for PDFs/videos/docs where supported  
  
// Function to convert Dropbox shared URL to direct URL  
function convertToDirectDropboxUrl(url) {  
  // Replace host  
  url = url.replace('www.dropbox.com', 'dl.dropboxusercontent.com');  
  
  // Find the query part  
  let parts = url.split('?');  
  if (parts.length < 2) return parts[0]; // No query  
  
  let base = parts[0];  
  let query = parts[1];  
  
  // Split query into params  
  let params = query.split('&');  
  let newParams = [];  
  for (let param of params) {  
    if (!param.startsWith('st=') && !param.startsWith('dl=')) {  
      newParams.push(param);  
    }  
  }  
  
  // Rejoin  
  if (newParams.length > 0) {  
    return base + '?' + newParams.join('&');  
  } else {  
    return base;  
  }  
}  
  
// Function to extract filename from Dropbox shared URL (string-based for robustness)  
function extractFilename(url) {  
  // Trim any extra whitespace (though input is already trimmed)  
  url = url.trim();  
  
  // Remove query string to focus on path  
  let queryIndex = url.indexOf('?');  
  if (queryIndex !== -1) {  
    url = url.substring(0, queryIndex);  
  }  
  
  // Find the last slash and extract the segment after it  
  let lastSlash = url.lastIndexOf('/');  
  if (lastSlash !== -1 && lastSlash < url.length - 1) {  
    let filename = url.substring(lastSlash + 1);  
  
    // If empty or just extension-like but no name, treat as invalid  
    if (filename && filename.length > 0) {  
      return filename;  
    }  
  }  
  return null;  
}  
  
// Function to generate a unique ID based on current timestamp (YYYYMMDDHHMM)  
function generateId() {  
  let now = new Date();  
  let yyyy = now.getFullYear();  
  let mm = (now.getMonth() + 1).toString().padStart(2, '0');  
  let dd = now.getDate().toString().padStart(2, '0');  
  let hh = now.getHours().toString().padStart(2, '0');  
  let min = now.getMinutes().toString().padStart(2, '0');  
  return yyyy + mm + dd + hh + min;  
}  
  
// Get input: selected text, current line, or clipboard  
let input = editor.getSelectedText();  
if (!input || input.trim() === "") {  
  // No selection, try current line  
  let range = editor.getSelectedLineRange();  
  input = editor.getTextInRange(range[0], range[1]);  
  if (!input || input.trim() === "") {  
    // No current line content, try clipboard  
    input = app.getClipboard();  
  }  
}  
  
// Trim and validate input  
input = input.trim();  
if (!input) {  
  app.displayErrorMessage("No Dropbox link found in selection, current line, or clipboard.");  
  context.cancel();  
}  
  
// Validate it's a Dropbox URL  
if (!input.startsWith('https://www.dropbox.com/')) {  
  app.displayErrorMessage("Invalid Dropbox link.");  
  context.cancel();  
}  
  
// Convert to direct URL  
let directUrl = convertToDirectDropboxUrl(input);  
if (!directUrl) {  
  app.displayErrorMessage("Could not convert Dropbox link to direct URL.");  
  context.cancel();  
}  
  
// Extract filename  
let filename = extractFilename(input);  
if (!filename) {  
  app.displayErrorMessage("Could not extract filename from Dropbox link.");  
  context.cancel();  
}  
  
// Generate unique ID for reference  
let id = generateId();  
  
// Generate Markdown using reference style for clickable preview/image  
let markdown = `[![${filename}][${id}]][${id}]\n[${id}]: ${directUrl}`;  
  
// Insert on the next line  
let currentLineRange = editor.getSelectedLineRange();  
let nextLinePos = currentLineRange[0] + currentLineRange[1];  
editor.setTextInRange(nextLinePos, 0, "\n" + markdown);  
editor.activate();  
  
let message = "Markdown clickable preview/link inserted on next line.";  
app.displayInfoMessage(message);  
  
// <!-- 202512051753 -->  
1 Like

Nice! I’d be interested in hearing more about your workflow with this…