Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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({});
Expand Down
37 changes: 37 additions & 0 deletions src/content.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -816,6 +852,7 @@
case "copyText":
case "copyHTML":
case "copyStyled":
case "copyMarkdown":
case "copyCSV":
if(selection)
doCopy(message.command);
Expand Down
1 change: 1 addition & 0 deletions src/popup.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
<a data-command="copyHTML">HTML</a>
<a data-command="copyStyled">Styled</a>
<a data-command="copyCSV">CSV</a>
<a data-command="copyMarkdown">Markdown</a>
<a data-command="copyText">Text</a>
</p>
<p id="mFind">
Expand Down