From fb351c3a1736e83091054493b28da241234c6af3 Mon Sep 17 00:00:00 2001 From: Pierre Hennequart Date: Tue, 22 Nov 2016 13:21:55 +0100 Subject: [PATCH] Add markdown format --- src/background.js | 3 +++ src/content.js | 37 +++++++++++++++++++++++++++++++++++++ src/popup.html | 1 + 3 files changed, 41 insertions(+) diff --git a/src/background.js b/src/background.js index 067208f..73d91f5 100644 --- a/src/background.js +++ b/src/background.js @@ -14,6 +14,7 @@ menu.copy = chrome.contextMenus.create({ "title": "Copy as...", "par menu.copyHTML = chrome.contextMenus.create({ "title": "HTML", "parentId": menu.copy, "enabled": false, "contexts": ctx, "onclick": function() { menuClick("copyHTML") }}); menu.copyStyled = chrome.contextMenus.create({ "title": "Styled HTML", "parentId": menu.copy, "enabled": false, "contexts": ctx, "onclick": function() { menuClick("copyStyled") }}); menu.copyCSV = chrome.contextMenus.create({ "title": "CSV", "parentId": menu.copy, "enabled": false, "contexts": ctx, "onclick": function() { menuClick("copyCSV") }}); +menu.copyMarkdown = chrome.contextMenus.create({ "title": "Markdown", "parentId": menu.copy, "enabled": false, "contexts": ctx, "onclick": function() { menuClick("copyMarkdown") }}); menu.copyText = chrome.contextMenus.create({ "title": "Text-Only", "parentId": menu.copy, "enabled": false, "contexts": ctx, "onclick": function() { menuClick("copyText") }}); // Send a command to tabs. @@ -44,6 +45,7 @@ chrome.runtime.onMessage.addListener(function(message, sender, sendResponse) { case "copyHTML": case "copyStyled": case "copyCSV": + case "copyMarkdown": var t = document.getElementById("___copytables_clipboard___"); t.value = message.content; t.focus(); @@ -85,6 +87,7 @@ chrome.runtime.onMessage.addListener(function(message, sender, sendResponse) { chrome.contextMenus.update(menu.copyHTML, {enabled:s.canCopy}); chrome.contextMenus.update(menu.copyStyled, {enabled:s.canCopy}); chrome.contextMenus.update(menu.copyCSV, {enabled:s.canCopy}); + chrome.contextMenus.update(menu.copyMarkdown, {enabled:s.canCopy}); chrome.contextMenus.update(menu.copyText, {enabled:s.canCopy}); sendResponse({}); diff --git a/src/content.js b/src/content.js index edf87dc..b1ac920 100644 --- a/src/content.js +++ b/src/content.js @@ -342,6 +342,37 @@ }); }; + // Return selected cells (`all=false`) or the whole table (`all=true') as markdown matrix. + var selectedMarkdownMatrix = function(table, all) { + var useSeparator = false, + headerSeparator = []; + var m = tableMatrix(table).map(function(row, rowIndex) { + return row.map(function(cell, cellIndex) { + if (cell.td && (all || isSelected(cell.td))) { + var text = strip(cell.td.innerText.replace(/[\r\n]+/g, " ")); + if (0 === rowIndex && "TH" === cell.td.tagName) + useSeparator = true; + if (!headerSeparator[cellIndex] || headerSeparator[cellIndex].length < text.length) + headerSeparator[cellIndex] = "-".repeat(text.length); + return text; + } + return ""; + }); + }); + if(useSeparator) + m.splice(1, 0, headerSeparator); + + return trimMatrix(m, function(cell) { + return cell.length > 0; + }).map(function(row) { + return row.map(function(cell, cellIndex) { + if(headerSeparator[cellIndex] && headerSeparator[cellIndex].length > cell.length) + cell = cell + " ".repeat(headerSeparator[cellIndex].length - cell.length); + return cell; + }); + }); + }; + // Return selected cells (`all=false`) or the whole table (`all=true') as text-only matrix. var selectedTextMatrix = function(table, all) { var m = tableMatrix(table).map(function(row) { @@ -728,6 +759,11 @@ return m.map(function(row) { return rstrip(row.join("\t")); }).join("\n"); + case "copyMarkdown": + var m = selectedMarkdownMatrix(selection.table, !anySelected); + return m.map(function(row) { + return rstrip(row.join(" | ")); + }).join("\n"); case "copyCSV": var m = selectedTextMatrix(selection.table, !anySelected); return m.map(function(row) { @@ -816,6 +852,7 @@ case "copyText": case "copyHTML": case "copyStyled": + case "copyMarkdown": case "copyCSV": if(selection) doCopy(message.command); diff --git a/src/popup.html b/src/popup.html index 929f020..7a313d4 100755 --- a/src/popup.html +++ b/src/popup.html @@ -12,6 +12,7 @@ HTML Styled CSV + Markdown Text